java Jackson JSON 不包装嵌套对象的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6983067/
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-10-30 18:08:34  来源:igfitidea点击:

Hymanson JSON do not wrap attributes of nested object

javajsonspringserializationHymanson

提问by kurochenko

I've got following classes:

我有以下课程:

public class Container {
    private String name;
    private Data data;
}

public class Data {
    private Long id;
}

When I serialize Containerclass using Hymanson I get

当我Container使用 Hymanson序列化类时,我得到

{"name":"Some name","data":{"id":1}}

But I need the result to be:

但我需要的结果是:

{"name":"Some name","id":1}

Is it possible (without adding Container.getDataId()method)? If so, how to do it?

是否有可能(不添加Container.getDataId()方法)?如果是这样,该怎么做?



update

更新

I've tried to create custom JsonSerializer<Data>but the result was same as before

我尝试创建自定义,JsonSerializer<Data>但结果与以前相同

public class JsonDataSerializer extends JsonSerializer<Data> {

    private static Logger logger = Logger.getLogger(JsonDataSerializer.class);

    @Override
    public void serialize(Data value, JsonGenerator jgen, 
            SerializerProvider provider)
            throws IOException,JsonProcessingException {

        Long id = (value.getId() == null) ? 0l : value.getId();
        jgen.writeStartObject();
        jgen.writeNumberField("id", id);
        jgen.writeEndObject();
        logger.debug("Data id " + id + " serialized to JSON.");       
    }
}

I've also tried to add @JsonSerializeannotation above Dataclass, then above getter in Containerclass. As I mentioned before without any success. My serializer is used, logger logs message.

我还尝试在类上方添加@JsonSerialize注释Data,然后在Container类中的getter上方添加注释。正如我之前提到的,没有任何成功。使用了我的序列化程序,记录器记录消息。



update 2

更新 2

When I remove writeStartObject()and writeEndObject()then no JSON is returnesd, only HTTP Status 500error and no exception is thrown except of what I found in debug output.

当我删除writeStartObject(),并writeEndObject()再没有JSON是returnesd,只有HTTP Status 500错误并不会引发任何异常的,除了我在调试输出中。

DEBUG: org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver - Resolving exception from handler [com.example.DataController@16be8a0]: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Can not write a field name, expecting a value; nested exception is org.codehaus.Hymanson.JsonGenerationException: Can not write a field name, expecting a value
DEBUG: org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver - Resolving exception from handler [com.example.DataController@16be8a0]: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Can not write a field name, expecting a value; nested exception is org.codehaus.Hymanson.JsonGenerationException: Can not write a field name, expecting a value
DEBUG: org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Resolving exception from handler [com.example.DataController@16be8a0]: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Can not write a field name, expecting a value; nested exception is org.codehaus.Hymanson.JsonGenerationException: Can not write a field name, expecting a value

采纳答案by Amol Brid

Hymanson 1.9 has introduced JsonUnwrappedannotation which does what you are looking for.

Hymanson 1.9 引入了JsonUnwrapped可以满足您需求的注释。

回答by henrik_lundgren

I think you must create and register a custom serializer for the Data type.

我认为您必须为数据类型创建和注册自定义序列化程序。

You can use the @JsonSerialize annotation on the Data class.

您可以在 Data 类上使用 @JsonSerialize 注释。

Specify a serializer class:

指定一个序列化器类:

@JsonSerialize(using = MyDataSerializer.class)
public class Data {
    ...
}

public static class MyDataSerializer extends JsonSerializer<Data> {

    @Override
    public void serialize(Data value, JsonGenerator jgen,
            SerializerProvider provider) throws IOException,JsonProcessingException {
        jgen.writeNumberField("id", value.id);
    }