在FasterXML/JacksonJava库中是否有与new Gson().toJson(Object)等效的函数?

bvn4nwqk  于 2022-11-06  发布在  Java
关注(0)|答案(3)|浏览(144)

我的问题很基本:FasterXML/Jackson Java库中是否有与new Gson().toJson(Object)等效项?
如果是,哪一个的性能更好?
谢谢

isr3a4wc

isr3a4wc1#

正如@Zutty所建议的:
在Jackson中新增了ObjectMapper().writeValueAsString(对象)
我试过了,但是得到一个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

bq9c1y66

bq9c1y662#

takipi的工作人员对几个json解析器进行了comparison性能测试,包括gson和Jackson,结果发现这取决于被解析的文档类型,Gson处理小文档的速度更快,jackson处理大文档的速度更快,第三种选择json.simple在大文档和小文档上都提供了更一致的性能。

r1zk6ea1

r1zk6ea13#

还有几个不同之处需要补充:

  • Gson使用字段值填充JSON字符串,而Jackson使用getter值。
  • 默认情况下Jackson使用公共Getter可见性来访问该值。但是我们可以使用以下配置:

属性访问器.GETTER,可见性. PROTECTED_AND_PUBLIC);
请参见示例:

public class ParentObj {
    private UUID id;    
    public ParentObj(UUID id) {
        this.id = id;       
    }   
    public UUID getUUID() {
        return id;
    }       
}
 Main.java
    ObjectMapper objectMapper = new ObjectMapper();
    System.out.println("ObjectMapper out > " + objectMapper.writeValueAsString(parentObj));

    Gson gson = new Gson();
    System.out.println("Gson out > " + gson.toJson(parentObj));

输出:

ObjectMapper out > {"uuid":"5d49e053-7954-4681-a3cc-1b5101d9712c"}
Gson out > {"id":"5d49e053-7954-4681-a3cc-1b5101d9712c"}

相关问题