[Simple-support] ElementMap without entry wrapper?
Brought to you by:
niallg
|
From: Jason v. N. <ja...@vo...> - 2011-11-23 23:54:18
|
Hi folks, I am evaluating Simple for use as the configuration framework for
an application I am writing. I am currently using XStream but I prefer
Simple's explicit mapping vs. XStream's implicit mapping. I am trying to
figure out how to emit a Map that does not include the redundant <entry>
and key information.
Here is my test program:
==
package org.openpnp.app;
import java.io.StringWriter;
import java.util.HashMap;
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.ElementMap;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
public class Test {
public static void main(String[] args) throws Exception {
Serializer serializer = new Persister();
StringWriter writer = new StringWriter();
serializer.write(new Machine(), writer);
System.out.println(writer.toString());
}
@Root
public static class Machine {
@ElementMap(attribute=true, key="id")
private HashMap<String, Head> heads = new HashMap<String, Head>();
public Machine() {
heads.put("1", new Head("1"));
heads.put("2", new Head("2"));
}
}
public static class Head {
@Attribute
private String id;
public Head(String id) {
this.id = id;
}
}
}
==
And the output:
==
<machine>
<heads>
<entry id="2">
<head id="2"/>
</entry>
<entry id="1">
<head id="1"/>
</entry>
</heads>
</machine>
==
And what I would prefer the output to look like:
==
<machine>
<heads>
<head id="2"/>
<head id="1"/>
</heads>
</machine>
==
I expect this to be possible because I am telling Simple that the key field
is id. If it knows that, the entry element and it's id= attribute is
redundant information. I am able to do this quite easily with XStream but I
have not found a way to do it with Simple. Is it possible without writing
my own Map converter?
Thanks,
Jason
|