Jackson JSON do not wrap attributes of nested object

14,382

Jackson 1.9 has introduced JsonUnwrapped annotation which does what you are looking for.

Share:
14,382
kurochenko
Author by

kurochenko

@kurochenko

Updated on July 20, 2022

Comments

  • kurochenko
    kurochenko almost 2 years

    I've got following classes:

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

    When I serialize Container class using Jackson I get

    {"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?


    update

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

    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 @JsonSerialize annotation above Data class, then above getter in Container class. As I mentioned before without any success. My serializer is used, logger logs message.


    update 2

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

    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.jackson.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.jackson.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.jackson.JsonGenerationException: Can not write a field name, expecting a value
    
  • kurochenko
    kurochenko over 12 years
    Thanks for reply! I've tried to do what you've written (see updated question), but unfortunately the result is same as before.