使用 Simple 将 HashMap 序列化为根元素 [英] Serialize HashMap as root element with Simple
问题描述
我想用Simple序列化一个扩展HashMap的元素.
I want to serialize an element that extends HashMap with Simple.
@Root(name = "settings")
@ElementMap(entry="element", key="id", attribute=true, required=true, empty=true)
public class Settings extends HashMap<String, Object> {
...
每当我序列化它时,我都不会出错,但也会得到一个如下所示的空文件:
Whenever I serialize it, I get no errors, but I also get an empty file that looks like this:
<settings/>
有什么方法可以做到这一点,而无需创建内部对象,然后将所有方法委托给它?
Is there any way to do this without creating an inner object instead, and then having to delegate all of the methods to it?
推荐答案
我想原因是,Simple 不能转换 HashMaps.
I suppose the reason is, that Simple cant transform HashMaps.
如果我运行这段代码......
Map<String, Object> map = new HashMap<>();
map.put("a", "b");
map.put("c", 3);
map.put("d", new Date());
ser.write(map, new File("test2.xml"));
...我收到以下异常:
org.simpleframework.xml.transform.TransformException:不支持类 java.util.HashMap 的转换
现在,这是我为您的课程序列化所做的工作:
我写了一个 Converter 用于转换Settings
.
I've written a Converter which is used for transforming Settings
.
Converter
类:
The Converter
class:
public class SettingsConverter implements Converter<Settings>
{
private Transformer transformer;
public SettingsConverter()
{
this.transformer = new Transformer(new RegistryMatcher());
}
@Override
public Settings read(InputNode node) throws Exception
{
Settings settings = new Settings();
InputNode child = node.getNext();
while( child != null )
{
final String key = child.getAttribute("key").getValue();
final Class c = Class.forName(child.getAttribute("class").getValue());
settings.put(key, transformer.read(child.getAttribute("value").getValue(), c));
child = node.getNext();
}
return settings;
}
@Override
public void write(OutputNode node, Settings value) throws Exception
{
for( Map.Entry<String, Object> entry : value.entrySet() )
{
OutputNode child = node.getChild("setting");
child.setAttribute("key", entry.getKey());
child.setAttribute("class", entry.getValue().getClass().getName());
child.setAttribute("value", transformer.write(entry.getValue(), entry.getValue().getClass()));
}
}
}
Settings
类:
The Settings
class:
@Root()
@Convert(value=SettingsConverter.class)
public class Settings extends HashMap<String, Object>
{
// ...
}
测试:
final File testFile = new File("test.xml");
Settings settings = new Settings();
settings.put("a", "b");
settings.put("c", 3);
settings.put("d", new Date());
// Serialize - make shure you use an AnnotationStrategy here
Serializer ser = new Persister(new AnnotationStrategy());
ser.write(settings, testFile);
// Deserialize
Settings in = ser.read(Settings.class, testFile);
System.out.println(settings.equals(in));
文件 test.xml:
<settings>
<setting key="d" class="java.util.Date" value="2012-08-28 17:15:13.152 MESZ"/>
<setting key="c" class="java.lang.Integer" value="3"/>
<setting key="a" class="java.lang.String" value="b"/>
</settings>
<小时>
我想有更好的方法(如果不是更多)来做到这一点,但也许这会有所帮助.
I guess there's a better way (if not some more) to do this, but maybe this helps a bit.
还有一点是,@Default
Annotation 在这里不起作用(Exception),可能解决这个问题就可以解决整个问题
Another point is, @Default
Annotation doesn't work here (Exception), possibly solving this may solve the whole problem
这篇关于使用 Simple 将 HashMap 序列化为根元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!