[Simple-support] Deserializing Map<String, NotPrimitiveType> with "Simple XML"
Brought to you by:
niallg
|
From: Dzmitry K. <dzm...@gm...> - 2013-04-15 15:19:54
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body bgcolor="#ffffff" text="#000000">
Hi, <br>
<br>
Is it possible to deserialize Map<String, MyType> with <b>Simple
xml</b>? How can I do that?<br>
<br>
I checked examples section but it contains example only for
Map<String, SomePrimitiveType>.<br>
<br>
I tried to do it myself:<br>
<br>
Java classes:<br>
<br>
@Root<br>
public class Content {<br>
<br>
@ElementMap(entry="page", key="id", attribute=true, inline=true)<br>
//@ElementMap(entry="page", key="id", attribute=true, inline=true,
valueType=net.edyoucare.krisenkompass.Page.class)<br>
public HashMap<String, Page> pages;<br>
<br>
}<br>
<br>
@Root<br>
public class Page {<br>
<br>
@Attribute<br>
private String id;<br>
<br>
@Attribute<br>
private String layout;<br>
<br>
}<br>
<br>
Xml:<br>
<br>
<?xml version="1.0" ?> <br>
<content> <br>
<br>
<page id="root" layout="text_with_pdf_link">test</page><br>
<page id="root2" layout="text_with_pdf_link">test</page><br>
<br>
</content><br>
<br>
Deserializing code:<br>
<br>
Serializer serializer = new Persister();<br>
Content result = serializer.read(Content.class, br);<br>
<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 :(<br>
So the map in Content.pages field looks like {root=null, root2=null}.<br>
<br>
I don't have any ideas how to force it to create Page object, and map
the keys with that object.<br>
<br>
Thanks,<br>
Dzmitry<br>
<br>
<br>
<br>
<br>
</body>
</html>
|