Re: [Simple-support] Customizing ElementMap serializer/deserializer
Brought to you by:
niallg
|
From: Niall G. <gal...@ya...> - 2008-12-11 10:14:58
|
Hi,
You can do it, however Simple currently binds elements to fields or methods using the element name. And these are typed strongly. There are two ways to do what you want to do. The first is to do something like.
@Root
private static class Value {
@Element(name="boolean", required=false)
private Boolean booleanValue;
@Element(name="byte", required=false)
private Byte byteValue;
@Element(name="double", required=false)
private Double doubleValue;
@Element(name="float", required=false)
private Float floatValue;
@Element(name="int", required=false)
private Integer intValue;
@Element(name="long", required=false)
private Long longValue;
@Element(name="short", required=false)
private Short shortValue;
@Element(name="dateTime", required=false)
private Date dateTime;
@Element(name="string", required=false)
private String string;
@Transient
private Object value;
@Validate
private void commit() {
if(booleanValue != null) {
value = booleanValue;
}
if(byteValue != null) {
value = byteValue;
}
if(doubleValue != null) {
value = doubleValue;
}
if(floatValue != null) {
value = floatValue;
}
if(intValue != null) {
value = intValue;
}
if(longValue != null) {
value = longValue;
}
if(shortValue != null) {
value = shortValue;
}
if(dateTime != null) {
value = dateTime;
}
if(string != null) {
value = string;
}
}
public Object get() {
return value;
}
public void set(Object value) {
this.value = value;
}
}
@Root
private static class Properties {
@ElementMap(key="key", value="value", inline=true)
private Map<String, Value> map;
public Object get(String name) {
return map.get(name).get();
}
The other is to create your own Strategy that will bind the type String.class to "string", Boolean.class to "boolean" and so on. This is more involved and there are examples of how to do it in the test cases bundled with the download.
Niall
________________________________
From: Kin Onn Low <ko...@ap...>
To: sim...@li...
Sent: Thursday, December 11, 2008 4:48:11 AM
Subject: [Simple-support] Customizing ElementMap serializer/deserializer
Hello,
I’m new to Simple and is currently evaluating it for
use in a project.
I have an existing XML and would like to deserialize it into
a map (Map<String,Object>):
<properties>
<entry>
<key>boolean-value</key>
<value>
<boolean>true</boolean>
</value>
</entry>
<entry>
<key>string-value</key>
<value>
<string>hello world</string>
</value>
</entry>
<entry>
<key>intvalue</key>
<value>
<int>42</int>
</value>
</entry>
</properties>
The value type can be one of the following:
<boolean>?</boolean>
<byte>?</byte>
<double>?</double>
<float>?</float>
<int>?</int>
<long>?</long>
<short>?</short>
<dateTime>?</dateTime>
<string>?</string>
So far, the closest I’ve been able to get to is this:
<properties
class="java.util.HashMap">
<entry>
<key>boolean-value</key>
<value
class="java.lang.Boolean">true</value>
</entry>
<entry>
<key>string-value</key>
<value
class="java.lang.String">hello world</value>
</entry>
<entry>
<key>intvalue</key>
<value
class="java.lang.Integer">42</value>
</entry>
</properties>
@ElementMap (inline=false,
key="key", value="value")
private Map<String, Object>
properties;
Is it possible to override default map
deserializer/serializer to handle the input XML which I have?
Thanks!
|