我如何使用 XStream 为简单地图生成较小的输出?
我使用 xstream 序列化应用程序的数据。由于它应该由其他应用程序读取 - 甚至手动查看,所以我想修改 XML。
我有非常简单类型的大地图,例如 TreeMap
和 TreeMap
。我想让我的输出更加紧凑。说从:
<entry>
<int>56</int>
<double>38.0</double>
</entry>
到
不幸的是我使用 this.aliasAttribute(Entry.class, "key", "key") 进行的实验
因空指针异常而失败。
我无法理解如何编写/注册一个 Converter
来处理 TreeMap
但保留 TreeMap
的默认行为对象,MyComplexObject>
。
也许你可以给我一个解决方案,谢谢。
也许堆栈跟踪可以提供一些信息。
java.lang.NullPointerException
at com.thoughtworks.xstream.converters.reflection.FieldDictionary.buildMap(FieldDictionary.java:128)
at com.thoughtworks.xstream.converters.reflection.FieldDictionary.fieldOrNull(FieldDictionary.java:113)
at com.thoughtworks.xstream.converters.reflection.FieldDictionary.field(FieldDictionary.java:92)
at com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider.getField(PureJavaReflectionProvider.java:181)
at com.thoughtworks.xstream.mapper.AttributeMapper.addAttributeFor(AttributeMapper.java:169)
at com.thoughtworks.xstream.XStream.useAttributeFor(XStream.java:1226)
at com.thoughtworks.xstream.XStream.aliasAttribute(XStream.java:1192)
at myproject.XstreamSerializer.<init>(XstreamSerializer.java:43)
I serialize the data of my application with xstream. Since it should be read by other applications - or even viewed manuall I'd like to touch up the XML.
I have big maps of very simple types like TreeMap<Integer,Double>
and TreeMap<String,Double>
. I want to make my output more compact for these. Say from:
<entry>
<int>56</int>
<double>38.0</double>
</entry>
to <entry key="56">38.0</entry>
Unfortunately my experiments using this.aliasAttribute(Entry.class, "key", "key")
fail with null pointer exception.
And I'm having problems to understand how I would write/register an Converter
that would handle TreeMap<Integer,Double>
but keeps the default behaviour for TreeMap<Object,MyComplexObject>
.
Maybe you can point me to a solution, thanks.
maybe the stacktrace can give some information.
java.lang.NullPointerException
at com.thoughtworks.xstream.converters.reflection.FieldDictionary.buildMap(FieldDictionary.java:128)
at com.thoughtworks.xstream.converters.reflection.FieldDictionary.fieldOrNull(FieldDictionary.java:113)
at com.thoughtworks.xstream.converters.reflection.FieldDictionary.field(FieldDictionary.java:92)
at com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider.getField(PureJavaReflectionProvider.java:181)
at com.thoughtworks.xstream.mapper.AttributeMapper.addAttributeFor(AttributeMapper.java:169)
at com.thoughtworks.xstream.XStream.useAttributeFor(XStream.java:1226)
at com.thoughtworks.xstream.XStream.aliasAttribute(XStream.java:1192)
at myproject.XstreamSerializer.<init>(XstreamSerializer.java:43)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
XStream
中有一个TreeMapConverter
也许它可以帮助你。
您可以在此处进一步了解此内容
There is a
TreeMapConverter
inXStream
May be it can help you out.
You can further read about this here
这个: http://x-stream.github .io/javadoc/com/thoughtworks/xstream/io/xml/CompactWriter.html 和 CustomConverter 可以帮助您。
This: http://x-stream.github.io/javadoc/com/thoughtworks/xstream/io/xml/CompactWriter.html and a CustomConverter can help you.