Reputation: 1657
I'm trying to use JAXB to serialize a class in XML.
@XmlRootElement
class Foo
{
Hashtable<String, Hashtable<String, Integer>> table = new Hashtable<>();
public Hashtable<String, Hashtable<String, Integer>> getTable() {
return table;
}
public void setTable(Hashtable<String, Hashtable<String, Integer>> t) {
table = t;
}
}
However, this produces XML with empty values (I assure you that the values are actually present!)
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Foo>
<table>
<entry>
<key>Test key</key>
<value/>
</entry>
</table>
</Foo>
Is there a simple way to fix this? I don't really want to have to use @XmlJavaTypeAdapters
unless I really have to.
Using a normal hashtable works fine: Hashtable<String, Integer>> table = new Hashtable<>();
Upvotes: 1
Views: 490
Reputation: 2810
Sorry to disappoint you but currently there is no simple way to fix it. There is significant difference between outer and inner hashtable. Outer one is a property and is handled internally by com.sun.xml.bind.v2.runtime.property.SingleMapNodeProperty<BeanT, ValueT>
class. This class does some magic to represent maps as key/value entries.
However, in case of inner Hashtable it is not "static" property, rather dynamic one. That's why it is handled by generic com.sun.xml.bind.v2.model.impl.RuntimeClassInfoImpl
. This class does not find any JAXB properties in Hashtable (i.e. java bean properties - with both getter and setter). As a result you get empty value
element.
Because of the same reason the following dynamic Hashtable property won't work either:
@XmlRootElement
@XmlSeeAlso(Hashtable.class)
public static class TypeWithHashtableAsObject {
private Object property;
public Object getProperty() {
return property;
}
public void setProperty(Object property) {
this.property = property;
}
}
...
TypeWithHashtableAsObject foo = new TypeWithHashtableAsObject();
Hashtable<String, Integer> property = new Hashtable<>();
property.put("innerKey", 12);
foo.setProperty(property);
StringWriter writer = new StringWriter();
marshaller.marshal(foo, writer);
System.out.println(writer.toString());
Result:
<typeWithHashtableAsObject>
<property xsi:type="hashtable" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
</typeWithHashtableAsObject>
That is empty element.
In another answer you can find more examples how to marshal nested collections. Another solution would be to wrap Hashtable with another type. Like Table:
public class Table {
private Hashtable<String, Integer> table = new Hashtable<>();
public Table(Hashtable<String, Integer> table) {
this.table = table;
}
public Table() {
}
public Hashtable<String, Integer> getTable() {
return table;
}
public void setTable(Hashtable<String, Integer> table) {
this.table = table;
}
}
and change Foo.table
type to Hashtable<String, Table>
.
The result is more verbose than original one but IMHO quite consistent:
<foo>
<table>
<entry>
<key>key1</key>
<value>
<table>
<entry>
<key>innerKey</key>
<value>12</value>
</entry>
</table>
</value>
</entry>
</table>
</foo>
Upvotes: 1