我的问题非常基本:FasterXML/Jackson Java 库中是否有与new Gson().toJson(Object)等价的东西?
如果是的话,哪个性能更好?
谢谢
takipi 的人对几个 json 解析器进行了性能比较,包括 gson 和 jackson。事实证明,这取决于要解析的文档类型。Gson 处理较小的文档速度更快,而 jackson 处理大型文档的速度更快。第三种替代方案 json.simple 在大型和小型文档中提供更一致的性能。
正如@Zutty 建议的那样:
在杰克逊 new ObjectMapper().writeValueAsString(Object)
我试过了,但我得到了一个com.fasterxml.jackson.databind.JsonMappingException
com.fasterxml.jackson.databind.JsonMappingException: Direct self-reference leading to cycle (through reference chain: com.learnkeeper.shared.entities.RootBook["permissionRef"]->com.googlecode.objectify.impl.ref.LiveRef["key"]->com.googlecode.objectify.Key["root"])
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter._handleSelfReference(BeanPropertyWriter.java:781)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:569)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:666)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:156)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:575)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:666)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:156)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:575)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:666)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:156)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:129)
at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:3385)
at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:2779)
虽然使用new Gson().toJson(Object)我得到了一个不错的Json:
{"id":4512395720392704,"origin":"FRENCH","target":"ENGLISH","wordKeys":[],"name":"Test","creation":"Aug 11, 2015 8:43:02 PM","modification":"Aug 11, 2015 8:43:02 PM","permissionRef":{"key":{"raw":{"kind":"Permission","id":6737807255011328}}},"lastVersion":true}
附加信息:我想要记录的所有Java 对象都实现java.io.Serializable