Search code examples
javagsonxml-deserialization

java gson - is it possible to deserialize an object from a string without having all of its fields


Lets say I am adding a field to the object and I still get the same string . Can I de-Json it into an object with the missing fields set as null? what if I have primitive variables there ? 10x


Solution

  • Well, depends on how the deserialization is done. If the object is first created with a no-arg constructor and then the fields are set via setters or reflection, I'd say this should work. In that case, every reference to other objects would be null whereas primitive types get their default values (for numbers 0, for boolean false, etc.)