ruru
ruru

Reputation: 47

Convert object to JSON String with Jackson

I`m working on an application that communicates with the server, and send data to a website, respectively, receives data from that website. The data is about 5 timetable that are optional.

For e.g. if 3 timetable are set, and I want to deselect one of those, it should send to website only 2 timetable. Here is a problem.

I analyse my code, and I concluded that the problem is in the method that Convert Object to JSON String.

Here is the code:

public class PriorityResponse {

    @JsonProperty("priority1")
    public Priority priorityOne;
    @JsonProperty("priority2")
    public Priority priorityTwo;
    @JsonProperty("priority3")
    public Priority priorityThree;
    @JsonProperty("priority4")
    public Priority priorityFour;
    @JsonProperty("priority5")
    public Priority priorityFive;

    public Priority getPriorityOne() {
        return priorityOne;
    }

    public Priority getPriorityTwo() {
        return priorityTwo;
    }

    public Priority getPriorityThree() {
        return priorityThree;
    }

    public Priority getPriorityFour() {
        return priorityFour;
    }

    public Priority getPriorityFive() {
        return priorityFive;
    }
   public String toJsonObject(){

         String jsonInString = "";
         ObjectMapper mapper = new ObjectMapper();
       //Object to JSON in String
         try {
             jsonInString = mapper.writeValueAsString(this);
         } catch (JsonProcessingException e) {
             e.printStackTrace();
         }

         return jsonInString;
     }
}

At this line

jsonInString = mapper.writeValueAsString(this);

the string is null, but when return jsonInString, is sending 3 timetable, not 2. If I make other changes, it sending correct data, the problem is only when I want to deselect one or more timetable.

The output should be like this when I deselect Priority4:

{"priority5":{"number":null,"timeOut":null,"timeTable":{"time":{"fh":null,"fm":null,"th":null,"tm":null,"cl":false,"op":true},"fri":true,"mon":true,"sat":true,"sun":true,"thu":true,"tue":true,"wed":true}},"priority4":{"number":null,"timeOut":null,"timeTable":{"time":{"fh":null,"fm":null,"th":null,"tm":null,"cl":false,"op":true},"fri":true,"mon":true,"sat":true,"sun":true,"thu":true,"tue":true,"wed":true}},"priority1":{"number":"1234561231","timeOut":"9","timeTable":{"time":{"fh":null,"fm":null,"th":null,"tm":null,"cl":false,"op":true},"fri":true,"mon":true,"sat":false,"sun":false,"thu":true,"tue":true,"wed":true}},"priority3":{"number":"21515211545","timeOut":"","timeTable":{"time":{"fh":null,"fm":null,"th":null,"tm":null,"cl":true,"op":false},"fri":false,"mon":true,"sat":false,"sun":false,"thu":true,"tue":true,"wed":false}},"priority2":{"number":"789123156421","timeOut":"","timeTable":{"time":{"fh":"04","fm":"02","th":"13","tm":"07","cl":false,"op":false},"fri":true,"mon":true,"sat":true,"sun":true,"thu":true,"tue":true,"wed":true}}}

but, it seems that, the JSON string does not refresh when I deselect one priority, because is sent that same priority that I get initial. Here is what is sent:

{"priority5":{"number":null,"timeOut":null,"timeTable":{"time":{"fh":null,"fm":null,"th":null,"tm":null,"cl":false,"op":true},"fri":true,"mon":true,"sat":true,"sun":true,"thu":true,"tue":true,"wed":true}},"priority4":{"number":"12233456545","timeOut":"6","timeTable":{"time":{"fh":null,"fm":null,"th":null,"tm":null,"cl":false,"op":true},"fri":true,"mon":true,"sat":false,"sun":false,"thu":true,"tue":true,"wed":true}},"priority1":{"number":"1234561231","timeOut":"9","timeTable":{"time":{"fh":null,"fm":null,"th":null,"tm":null,"cl":false,"op":true},"fri":true,"mon":true,"sat":false,"sun":false,"thu":true,"tue":true,"wed":true}},"priority3":{"number":"21515211545","timeOut":"5","timeTable":{"time":{"fh":null,"fm":null,"th":null,"tm":null,"cl":true,"op":false},"fri":false,"mon":true,"sat":false,"sun":false,"thu":true,"tue":true,"wed":false}},"priority2":{"number":"789123156421","timeOut":"5","timeTable":{"time":{"fh":"04","fm":"02","th":"13","tm":"07","cl":false,"op":false},"fri":true,"mon":true,"sat":true,"sun":true,"thu":true,"tue":true,"wed":true}}}

Upvotes: 1

Views: 3631

Answers (2)

Daniel Schmidt
Daniel Schmidt

Reputation: 401

Does setting the serializationInclusion to NON_NULL give you what you want?

public String toJsonObject() throws IOException{

     String jsonInString = "";
     ObjectMapper mapper = new ObjectMapper();
   //Object to JSON in String
     try {
         // Will only include non null objects
         mapper.setSerializationInclusion(Inclusion.NON_NULL);
         jsonInString = mapper.writeValueAsString(this);
     } catch (JsonProcessingException e) {
         e.printStackTrace();
     }

     return jsonInString;
 }

Upvotes: 0

Arthur C Stapassoli
Arthur C Stapassoli

Reputation: 47

try to use com.google.gson.Gson If any attribute is null the parser is

String jsonInString = new Gson().toJson(yourObject);

results:

{
 "priorityOne":{},
 "priorityTwo":{},
  ...
}

Upvotes: 1

Related Questions