Java 使用 Jackson 将对象转储到 String

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9767131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:24:35  来源:igfitidea点击:

dump object to String with Hymanson

javaHymansongson

提问by turbanoff

I'm using Gson to generate debug ouput in my application

我正在使用 Gson 在我的应用程序中生成调试输出

Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls().create();
gson.toJson(myObject);

But Gson does complain about a circular reference error when attempting to serialize a data structure. Can this be done with Hymanson library?

但是 Gson 在尝试序列化数据结构时确实会抱怨循环引用错误。这可以用Hyman逊图书馆完成吗?

UPDGson 2.3.1: Released Nov 20, 2014

UPDGson 2.3.1:2014 年 11 月 20 日发布

Added support to serialize objects with self-referential fields. The self-referential field is set to null in JSON. Previous version of Gson threw a StackOverflowException on encountering any self-referential fields.
    The most visible impact of this is that Gson can now serialize Throwable (Exception and Error)

采纳答案by Bruno Grieder

To serialize with Hymanson:

与Hyman逊序列化:

public String serialize(Object obj, boolean pretty) {
    ObjectMapper mapper = new ObjectMapper();

    mapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);

    if (pretty) {
        return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
    }

    return mapper.writeValueAsString(obj);
}

回答by beerbajay

Hymanson can deal with cycles in the object graph with:

Hymanson 可以通过以下方式处理对象图中的循环:

  1. @JsonIgnore, where you omit the property entirely
  2. @JsonManagedReferenceand @JsonBackReference
  3. a custom Serializer which extends JsonSerializer
  1. @JsonIgnore,您完全省略了该属性
  2. @JsonManagedReference@JsonBackReference
  3. 扩展的自定义序列化程序 JsonSerializer

You would use JsonSerializerwhen you still wish to provide some information about the object but want to omit certain fields (the ones causing the cycle). For example:

JsonSerializer当您仍然希望提供有关对象的一些信息但希望省略某些字段(导致循环的字段)时,您可以使用。例如:

import org.codehaus.Hymanson.map.JsonSerializer
import org.codehaus.Hymanson.JsonGenerator;
import org.codehaus.Hymanson.JsonProcessingException;
import org.codehaus.Hymanson.map.SerializerProvider;

import java.io.IOException;

public class ParentReferenceSerializer extends JsonSerializer<Parent> {
    @Override
    public void serialize(Parent parent, JsonGenerator jgen,
        SerializerProvider provider)
            throws IOException, JsonProcessingException {
        jgen.writeStartObject();
        writeNumberField(jgen, "id", parent.getId());
        // ... other fields ...
        jgen.writeEndObject();
    }
}

Then in the class in which is being serialized, you'd use a @JsonSerializeannotation:

然后在正在序列化的类中,您将使用@JsonSerialize注释:

@JsonSerialize(using = ParentReferenceSerializer.class)
public Parent getParent() {
    return parent;
}