哪个 JSON 序列化库适合以下情况?
我有以下案例: 我想序列化使用 java.util.UUID 类型的 var 扩展父类的 Scala 案例类。 此案例类的序列化应该在没有任何配置的情况下进行 - 没有注释和自定义格式的定义。任何序列化提示都可以位于父类中。
我尝试了 sjson,但是基于反射的序列化无法序列化 UUID 类型,并且基于类型的序列化迫使我为每个案例类定义格式。 哪个 json 序列化库最适合这种情况?
I've got following case:
I'd like to serialize Scala case classes that extend parent class with var of type java.util.UUID.
Serialization of this case classes should happen without any configuration of them - no annotations and definition of custom formats. Any serialization hints may be situated in parent class.
I tried sjson, but Reflection based serialization can't serialize UUID types and type based serialization forces me to define formats for every case class.
Which json serialization library would best fit this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一种使用 Lift JSON 的解决方案。
它打印:
另一种解决方案,它使用父类型的自定义序列化程序。
Here's one solution with Lift JSON.
It prints:
Another solution which uses a custom serializer for Parent type.
如果类型很重要,您应该查看 YAML。
http://www.google.fr/search?q=java+yaml
它是 json 的一个子集,具有改进的功能,例如变量类型。
If the type is important, you should take a look at YAML.
http://www.google.fr/search?q=java+yaml
It's a subset of json with improved stuff, like variable typing.
你可以尝试 jerkson: https://github.com/codahale/jerkson
它对我的使用效果很好,但这主要是列表/映射结构。如果它支持您的需求,也不会感到惊讶。
编辑:尝试使用以下示例(受到另一个答案中的电梯示例的启发)。似乎工作正常。
You could try jerkson: https://github.com/codahale/jerkson
Its working good for my use, but that is mostly list/map structures. Would not be surprised if it supports your needs though..
Edit: Tried it with the following example (inspired by the lift example in another answer). Seems to work fine.
尝试使用包含 JSON 支持的 XStream 库。我已经在几个项目中成功地使用了它。它有许多默认转换器,其中包括一个用于 java.util.UUID 的转换器。默认转换器的完整列表位于此处:http://x-stream.github.io/converters .html。
有关使用 XStream 进行 JSON 读写的简要教程位于:http://x- Stream.github.io/json-tutorial.html。本教程代码是为 Java 编写的,但它对于 Scala 的工作原理应该是相同的,因为反射是在幕后使用的。
请记住,使用此库并不总是可以序列化然后反序列化任意对象图。特别是,无法处理数据中的循环,即您的数据必须是纯粹的分层树。考虑到 JSON 格式的意图,这是一个合理的限制。
参考链接:
XStream JSON 教程: http://x-stream.github.io/json-tutorial.html
XStream 默认转换器: http://x-stream.github.io/converters.html
Try the XStream library which includes JSON support. I have used this successfully in a few projects. It has a number of default converters, including one for
java.util.UUID
. A full list of default converters is located here: http://x-stream.github.io/converters.html.A brief tutorial on using XStream for JSON reading and writing is located here: http://x-stream.github.io/json-tutorial.html. The tutorial code is written for Java but it should work just the same for Scala since reflection is being used behind the scenes.
Keep in mind that serializing and then deserializing arbitrary graphs of objects is not always possible with this library. In particular, loops in your data cannot be handled, i.e. your data must be a purely hierarchical tree. This is a reasonable limitation given the intentions of the JSON format.
Referenced links:
XStream JSON tutorial: http://x-stream.github.io/json-tutorial.html
XStream default converters: http://x-stream.github.io/converters.html