Re: [Simple-support] Serializing collections with both SimpleXML-annotated and not annotated types
Brought to you by:
niallg
|
From: Niall G. <gal...@ya...> - 2008-09-28 18:42:29
|
Hi,
Try the following:
package org.simpleframework.xml.load;
import java.util.HashMap;
import java.util.Map;
import junit.framework.TestCase;
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.ElementMap;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.Serializer;
public class MixTest extends TestCase {
@Root
private static class MixList {
@ElementMap(keyType=String.class, valueType=Object.class)
private Map<String, Object> map = new HashMap<String, Object>();
}
@Root
private class Entry {
@Attribute
private String name;
}
public void testMix() throws Exception {
MixList list = new MixList();
Serializer serializer = new Persister();
list.map.put("a", "b");
list.map.put("b", 1);
list.map.put("c", true);
serializer.write(list, System.out);
Entry entry = new Entry();
entry.name = "entry";
list.map.put("d", entry);
serializer.write(list, System.out);
}
}
Shared types in a map. It produces.
<mixList>
<map class="java.util.HashMap">
<entry>
<string>d</string>
<object class="org.simpleframework.xml.load.MixTest$Entry" name="entry"/>
</entry>
<entry>
<string>a</string>
<object class="java.lang.String"/>
</entry>
<entry>
<string>c</string>
<object class="java.lang.Boolean"/>
</entry>
<entry>
<string>b</string>
<object class="java.lang.Integer"/>
</entry>
</map>
</mixList>
Hope this helps
Niall
--- On Thu, 9/25/08, Stanislaw Osinski <sta...@ca...> wrote:
> From: Stanislaw Osinski <sta...@ca...>
> Subject: [Simple-support] Serializing collections with both SimpleXML-annotated and not annotated types
> To: sim...@li...
> Date: Thursday, September 25, 2008, 3:51 PM
> Hi there,
>
> First of all, thanks for sharing SimpleXML with the world,
> it makes working
> with XMLs a breeze!
>
> One usage scenario in my code is this: I have a map of
> objects (Map<String,
> Object>) that can contain instances of a number of JDK
> types (String,
> Integer, Double, Boolean etc.) but also some
> SimpleXML-annotated types. The
> problem obviously is that while the annotated types
> serialize / deserialize
> nicely, the JDK ones will not (unless the collection has
> the JDK type in its
> declaration, which is not possible in my case).
>
> I was wondering what was the recommended way of serializing
> / deserializing
> such collections. One approach that crossed my mind is
> replacing the JDK
> types with some sort of XML-annotated wrappers before
> serialization and
> reverting the mapping after deserialization, but maybe
> there is a better
> way? (after all, SimpleXML has mechanisms for serialization
> of e.g. a
> List<Integer>, so why not reuse them if possible?)
>
> Thanks,
>
> Staszek
>
> --
> http://www.carrot2.org
> -------------------------------------------------------------------------
> This SF.Net email is sponsored by the Moblin Your Move
> Developer's challenge
> Build the coolest Linux based applications with Moblin SDK
> & win great prizes
> Grand prize is a trip for two to an Open Source event
> anywhere in the world
> http://moblin-contest.org/redirect.php?banner_id=100&url=/_______________________________________________
> Simple-support mailing list
> Sim...@li...
> https://lists.sourceforge.net/lists/listinfo/simple-support
|