Do not include empty object to Jackson









up vote
-1
down vote

favorite












I am trying to create json with spring boot.



class:



public class Person 
private String name;
private PersonDetails details;

// getters and setters...



impletentation:



Person person = new Person();
person.setName("Apple");
person.setDetails(new PersonDetails());


So there is a instance of Person with empty details and this is exactly what Jackson is returning:



"person": 
"name": "Apple",
"details":



I want to have json without empty brackets :



"person": 
"name": "Apple"



This question's didn't helped me:



  • How to tell Jackson to ignore empty object during deserialization?

  • How to ignore "null" or empty properties in json, globally, using Spring configuration

Update 1:



I'm using Jackson 2.9.6










share|improve this question























  • Custom serializer/deserializer for ignoring your empty specific object sounds as good solution. So e.g. @JsonInclude(Include.NON_NULL) not working in this case because you haven't null object, you have object wich has no attribute filled. Why do you want to ignore empty object in JSON?
    – h0nzan
    Nov 10 at 11:38














up vote
-1
down vote

favorite












I am trying to create json with spring boot.



class:



public class Person 
private String name;
private PersonDetails details;

// getters and setters...



impletentation:



Person person = new Person();
person.setName("Apple");
person.setDetails(new PersonDetails());


So there is a instance of Person with empty details and this is exactly what Jackson is returning:



"person": 
"name": "Apple",
"details":



I want to have json without empty brackets :



"person": 
"name": "Apple"



This question's didn't helped me:



  • How to tell Jackson to ignore empty object during deserialization?

  • How to ignore "null" or empty properties in json, globally, using Spring configuration

Update 1:



I'm using Jackson 2.9.6










share|improve this question























  • Custom serializer/deserializer for ignoring your empty specific object sounds as good solution. So e.g. @JsonInclude(Include.NON_NULL) not working in this case because you haven't null object, you have object wich has no attribute filled. Why do you want to ignore empty object in JSON?
    – h0nzan
    Nov 10 at 11:38












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I am trying to create json with spring boot.



class:



public class Person 
private String name;
private PersonDetails details;

// getters and setters...



impletentation:



Person person = new Person();
person.setName("Apple");
person.setDetails(new PersonDetails());


So there is a instance of Person with empty details and this is exactly what Jackson is returning:



"person": 
"name": "Apple",
"details":



I want to have json without empty brackets :



"person": 
"name": "Apple"



This question's didn't helped me:



  • How to tell Jackson to ignore empty object during deserialization?

  • How to ignore "null" or empty properties in json, globally, using Spring configuration

Update 1:



I'm using Jackson 2.9.6










share|improve this question















I am trying to create json with spring boot.



class:



public class Person 
private String name;
private PersonDetails details;

// getters and setters...



impletentation:



Person person = new Person();
person.setName("Apple");
person.setDetails(new PersonDetails());


So there is a instance of Person with empty details and this is exactly what Jackson is returning:



"person": 
"name": "Apple",
"details":



I want to have json without empty brackets :



"person": 
"name": "Apple"



This question's didn't helped me:



  • How to tell Jackson to ignore empty object during deserialization?

  • How to ignore "null" or empty properties in json, globally, using Spring configuration

Update 1:



I'm using Jackson 2.9.6







java json spring-boot






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 0:15

























asked Nov 9 at 23:52









Marin

12




12











  • Custom serializer/deserializer for ignoring your empty specific object sounds as good solution. So e.g. @JsonInclude(Include.NON_NULL) not working in this case because you haven't null object, you have object wich has no attribute filled. Why do you want to ignore empty object in JSON?
    – h0nzan
    Nov 10 at 11:38
















  • Custom serializer/deserializer for ignoring your empty specific object sounds as good solution. So e.g. @JsonInclude(Include.NON_NULL) not working in this case because you haven't null object, you have object wich has no attribute filled. Why do you want to ignore empty object in JSON?
    – h0nzan
    Nov 10 at 11:38















Custom serializer/deserializer for ignoring your empty specific object sounds as good solution. So e.g. @JsonInclude(Include.NON_NULL) not working in this case because you haven't null object, you have object wich has no attribute filled. Why do you want to ignore empty object in JSON?
– h0nzan
Nov 10 at 11:38




Custom serializer/deserializer for ignoring your empty specific object sounds as good solution. So e.g. @JsonInclude(Include.NON_NULL) not working in this case because you haven't null object, you have object wich has no attribute filled. Why do you want to ignore empty object in JSON?
– h0nzan
Nov 10 at 11:38












1 Answer
1






active

oldest

votes

















up vote
1
down vote













Without a custom serializer, jackson will include your object.



Solution 1 : Replace new object with null



person.setDetails(new PersonDetails());


with



person.setDetails(null);


and add



@JsonInclude(Include.NON_NULL)
public class Person {


Solution 2: Custom serializer



public class PersonDetailsSerializer extends StdSerializer<PersonDetails> 

public PersonDetailsSerializer()
this(null);


public PersonDetailsSerializer(Class<PersonDetails> t)
super(t);


@Override
public void serialize(
PersonDetails personDetails, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException
// custom behavior if you implement equals and hashCode in your code
if(personDetails.equals(new PersonDetails())
return;

super.serialize(personDetails,jgen,provider);




and in your PersonDetails



public class Person 
private String name;
@JsonSerialize(using = PersonDetailsSerializer.class)
private PersonDetails details;






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234727%2fdo-not-include-empty-object-to-jackson%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    Without a custom serializer, jackson will include your object.



    Solution 1 : Replace new object with null



    person.setDetails(new PersonDetails());


    with



    person.setDetails(null);


    and add



    @JsonInclude(Include.NON_NULL)
    public class Person {


    Solution 2: Custom serializer



    public class PersonDetailsSerializer extends StdSerializer<PersonDetails> 

    public PersonDetailsSerializer()
    this(null);


    public PersonDetailsSerializer(Class<PersonDetails> t)
    super(t);


    @Override
    public void serialize(
    PersonDetails personDetails, JsonGenerator jgen, SerializerProvider provider)
    throws IOException, JsonProcessingException
    // custom behavior if you implement equals and hashCode in your code
    if(personDetails.equals(new PersonDetails())
    return;

    super.serialize(personDetails,jgen,provider);




    and in your PersonDetails



    public class Person 
    private String name;
    @JsonSerialize(using = PersonDetailsSerializer.class)
    private PersonDetails details;






    share|improve this answer
























      up vote
      1
      down vote













      Without a custom serializer, jackson will include your object.



      Solution 1 : Replace new object with null



      person.setDetails(new PersonDetails());


      with



      person.setDetails(null);


      and add



      @JsonInclude(Include.NON_NULL)
      public class Person {


      Solution 2: Custom serializer



      public class PersonDetailsSerializer extends StdSerializer<PersonDetails> 

      public PersonDetailsSerializer()
      this(null);


      public PersonDetailsSerializer(Class<PersonDetails> t)
      super(t);


      @Override
      public void serialize(
      PersonDetails personDetails, JsonGenerator jgen, SerializerProvider provider)
      throws IOException, JsonProcessingException
      // custom behavior if you implement equals and hashCode in your code
      if(personDetails.equals(new PersonDetails())
      return;

      super.serialize(personDetails,jgen,provider);




      and in your PersonDetails



      public class Person 
      private String name;
      @JsonSerialize(using = PersonDetailsSerializer.class)
      private PersonDetails details;






      share|improve this answer






















        up vote
        1
        down vote










        up vote
        1
        down vote









        Without a custom serializer, jackson will include your object.



        Solution 1 : Replace new object with null



        person.setDetails(new PersonDetails());


        with



        person.setDetails(null);


        and add



        @JsonInclude(Include.NON_NULL)
        public class Person {


        Solution 2: Custom serializer



        public class PersonDetailsSerializer extends StdSerializer<PersonDetails> 

        public PersonDetailsSerializer()
        this(null);


        public PersonDetailsSerializer(Class<PersonDetails> t)
        super(t);


        @Override
        public void serialize(
        PersonDetails personDetails, JsonGenerator jgen, SerializerProvider provider)
        throws IOException, JsonProcessingException
        // custom behavior if you implement equals and hashCode in your code
        if(personDetails.equals(new PersonDetails())
        return;

        super.serialize(personDetails,jgen,provider);




        and in your PersonDetails



        public class Person 
        private String name;
        @JsonSerialize(using = PersonDetailsSerializer.class)
        private PersonDetails details;






        share|improve this answer












        Without a custom serializer, jackson will include your object.



        Solution 1 : Replace new object with null



        person.setDetails(new PersonDetails());


        with



        person.setDetails(null);


        and add



        @JsonInclude(Include.NON_NULL)
        public class Person {


        Solution 2: Custom serializer



        public class PersonDetailsSerializer extends StdSerializer<PersonDetails> 

        public PersonDetailsSerializer()
        this(null);


        public PersonDetailsSerializer(Class<PersonDetails> t)
        super(t);


        @Override
        public void serialize(
        PersonDetails personDetails, JsonGenerator jgen, SerializerProvider provider)
        throws IOException, JsonProcessingException
        // custom behavior if you implement equals and hashCode in your code
        if(personDetails.equals(new PersonDetails())
        return;

        super.serialize(personDetails,jgen,provider);




        and in your PersonDetails



        public class Person 
        private String name;
        @JsonSerialize(using = PersonDetailsSerializer.class)
        private PersonDetails details;







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 8:15









        Adina

        1,1921210




        1,1921210



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234727%2fdo-not-include-empty-object-to-jackson%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo