6

I have a class defined as below:

// Ignore all the unknown properties in input JSON
@JsonIgnoreProperties(ignoreUnknown = true)

// Only include non null values in deserialized Java object.
@JsonInclude(value = Include.NON_NULL)
public class Person {


@JsonProperty("person_id")
private String personId;

@JsonProperty("school")
private String school;

@JsonProperty("hobbies")
private Map<String, List<AttributeBag>> hobbies = new   HashMap<String, List<AttributeBag>>();

@JsonProperty("tasks")
private Map<String, Map<String, AttributeBag>> tasks = new HashMap<String, Map<String, AttributeBag>>();



 public Map<String, List<AttributeBag>> getHobbies() {
    return hobbies;
}


   public Person(String person_id, String school) {
    super();
    this.person_id = person_id;
    this.school = school;

}

When I use a JSON string below to deserialize from string to object,

 {
"person_id":"123",
"school":"stanford University"
}

From the object I got deserialized, the hobbies is create but empty, and the tasks is not created even. I am expecting the way like "tasks", if there is no corresponding field in JSON, it SHOULD NOT get deserialized in the object.

But the weird thing is : when I check object.getHobbies()!=null but the tasks part is null. I want both are null in the object if they were not present in JSON.

I have a constructor for the Person class but I did not initialize both hobbies and tasks part.

Thanks a lot

3
  • It might be useful to post your constructor code. Based on what you have there, both hobbies and tasks should both be non-null, empty maps Commented Mar 3, 2015 at 22:34
  • @MrWiggles constructor updated. Why they both should be non-null. What I am expecting is that, if they are not in the JSON, I DO NOT want them get deserialized in the object. How to do that ? Commented Mar 3, 2015 at 22:52
  • Related: stackoverflow.com/a/70669110/10544569 - @JsonSetter(Nulls.SKIP) Commented Nov 23, 2022 at 9:00

2 Answers 2

3
@JsonProperty("hobbies")
private Map<String, List<AttributeBag>> hobbies = new   HashMap<String, List<AttributeBag>>();

@JsonProperty("tasks")
private Map<String, Map<String, AttributeBag>> tasks = new HashMap<String, Map<String, AttributeBag>>();

from above code its clear that you are creating new objects for hobbies and tasks no matter what, I am not able to understand why your tasks are not created(it should get created as a empty map)

and to answer your question @JsonInclude(value = Include.NON_NULL) should do the trick!

Sign up to request clarification or add additional context in comments.

4 Comments

But the weird thing is that, tasks is not created when deserialzing from json string to object. any reason for that ?
If I dont put the @JsonProperty, how does the objectmapper know, which field i.e., "tasks" deserialized to which object, i.e., Map<String, Map<String, AttributeBag>>
i am not pointing to @JsonProperty I am saying that you are creating new HashMap... and assigning it to tasks. It means it will created no matter any value comes in json or not
Good point. But the weird thing is that, tasks does not got created but only for the hobbies. Why that ?
1

The JSON deserialiser will not attempt to set the fields that don't appear in the JSON structure, but these lines:

@JsonProperty("hobbies")
private Map<String, List<AttributeBag>> hobbies = new   HashMap<String, List<AttributeBag>>();

@JsonProperty("tasks")
private Map<String, Map<String, AttributeBag>> tasks = new HashMap<String, Map<String, AttributeBag>>();

are creating the values on object construction.

If you want them to be null then don't allocate them in the object, just leave the declaration:

@JsonProperty("hobbies")
private Map<String, List<AttributeBag>> hobbies;

@JsonProperty("tasks")
private Map<String, Map<String, AttributeBag>> tasks;

3 Comments

But the weird thing is that, tasks is not created when deserialzing from json string to object. any reason for that ?
If I dont put the @JsonProperty, how does the objectmapper know, which field i.e., "tasks" deserialized to which object, i.e., Map<String, Map<String, AttributeBag>>
So shouldn't you just use the "dynamic block", as shown on this guide section 3.2 gerardnico.com/lang/java/initialization_block to initialize those maps?

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.