2

I've Foo class with some values and use the same Foo oject to deserialize.

public class Foo {
        public String name;
        public Integer age;
        public Goo g;
}
static public class Goo {

        public Long id;
        public Integer level;
}

assume // String jsonStr = {"name" : "alan"; "g": {"level":6}}
Foo f = new Foo();
f.name = "dummy";
f.age = 99;
//asssume f.g.id = 3
objectMapper.readerForUpdating(f).readValue(jsonStr);
// f object toString
output {"name" : "alan"; "age" : 99, "g":{"id": null, "level":6}}

how to configure such that it wont upate any or default value on the field which have value on the missing field?

like below output instead

output {"name" : "alan"; "age" : 99, "g":{"id": 3, "level":6}}
drategon
  • 73
  • 1
  • 8

1 Answers1

2

You can configure the mapper manually or annotate the Foo class.

Manually:

objectMapper.setSerializationInclusion(Include.NON_NULL);

Or put an annotation on the class (or even on the attribute):

@JsonInclude(Include.NON_NULL)
public class Foo {
        public String name;
        public Integer age;
}

EDIT: this answers the question if you want to ignore nulls, but as a comment on the question say, you might have an error in your code. You re-assign your variable before reading, obviously

Aleksandar Stojadinovic
  • 4,851
  • 1
  • 34
  • 56
  • mayber my question not clear. Anyway, it is not the answer i'm looking. found answer from here http://stackoverflow.com/questions/12518618/deserialize-json-into-existing-object-java – drategon Feb 06 '15 at 02:16