Try to serialize an instance and take a look at the structure. This should match the input. For below, you can only use inline with primitive types. For an object the value will be a child element.
From: Dzmitry Kavaliou [mailto:dzm...@gm...]
Sent: Tuesday, 16 April 2013 1:20 AM
To: sim...@li...
Subject: [Simple-support] Deserializing Map<String, NotPrimitiveType> with "Simple XML"
Hi,
Is it possible to deserialize Map<String, MyType> with Simple xml? How can I do that?
I checked examples section but it contains example only for Map<String, SomePrimitiveType>.
I tried to do it myself:
Java classes:
@Root
public class Content {
@ElementMap(entry="page", key="id", attribute=true, inline=true)
//@ElementMap(entry="page", key="id", attribute=true, inline=true, valueType=net.edyoucare.krisenkompass.Page.class)
public HashMap<String, Page> pages;
}
@Root
public class Page {
@Attribute
private String id;
@Attribute
private String layout;
}
Xml:
<?xml version="1.0" ?>
<content>
<page id="root" layout="text_with_pdf_link">test</page>
<page id="root2" layout="text_with_pdf_link">test</page>
</content>
Deserializing code:
Serializer serializer = new Persister();
Content result = serializer.read(Content.class, br);
The code above runs without any exceptions. It produces Content instance with a map which has two entries with correct keys. But these keys are map to the null values :(
So the map in Content.pages field looks like {root=null, root2=null}.
I don't have any ideas how to force it to create Page object, and map the keys with that object.
Thanks,
Dzmitry
|