我如何使用 XStream 为简单地图生成较小的输出?

发布于 2025-01-04 02:50:17 字数 1528 浏览 1 评论 0原文

我使用 xstream 序列化应用程序的数据。由于它应该由其他应用程序读取 - 甚至手动查看,所以我想修改 XML。

我有非常简单类型的大地图,例如 TreeMapTreeMap。我想让我的输出更加紧凑。说从:

<entry>
    <int>56</int>
    <double>38.0</double>
</entry>

38.0

不幸的是我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七秒鱼° 2025-01-11 02:50:17

XStream中有一个TreeMapConverter
也许它可以帮助你。
您可以在此处进一步了解此内容

There is a TreeMapConverter in XStream
May be it can help you out.
You can further read about this here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文