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

Terry
  • 855
  • 4
  • 12
  • 16
  • 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 – tddmonkey Mar 03 '15 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 ? – Terry Mar 03 '15 at 22:52
  • Related: https://stackoverflow.com/a/70669110/10544569 - `@JsonSetter(Nulls.SKIP)` – IndustryUser1942 Nov 23 '22 at 09:00

2 Answers2

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!

dev2d
  • 4,245
  • 3
  • 31
  • 54
  • But the weird thing is that, tasks is not created when deserialzing from json string to object. any reason for that ? – Terry Mar 03 '15 at 23:16
  • If I dont put the @JsonProperty, how does the objectmapper know, which field i.e., "tasks" deserialized to which object, i.e., Map> – Terry Mar 03 '15 at 23:18
  • 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 – dev2d Mar 03 '15 at 23:34
  • Good point. But the weird thing is that, tasks does not got created but only for the hobbies. Why that ? – Terry Mar 04 '15 at 01:08
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;
tddmonkey
  • 20,798
  • 10
  • 58
  • 67
  • 1
    But the weird thing is that, tasks is not created when deserialzing from json string to object. any reason for that ? – Terry Mar 03 '15 at 23:16
  • If I dont put the @JsonProperty, how does the objectmapper know, which field i.e., "tasks" deserialized to which object, i.e., Map> – Terry Mar 03 '15 at 23:18
  • So shouldn't you just use the "dynamic block", as shown on this guide section 3.2 https://gerardnico.com/lang/java/initialization_block to initialize those maps? – Marco Ottina Jul 21 '19 at 10:26