1

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}}}

2 Answers 2

1

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

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

results:

{
 "priorityOne":{},
 "priorityTwo":{},
  ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

It would appear that the question is trying to use Jackson. Please respect that intent.
0

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;
 }

3 Comments

Unfortunately I receive the same result. The problem persist even if I use Inclusion.NON_NULL.
Hey:<br>Can you list out exactly what you want to see in your output? And what the state of each of the 5 "Priority" instances in your "PriorityResponse" object.
Explain what you mean by "deselect".. It seems like you think you are making the attribute "Priority4" null, but you aren't really...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.