java
java copied to clipboard
jsonstream.serialize how to skip null fields
Hi,
Is there a way to extend the serializer so that it will skip fields that are null?
I need this to save space.
thx Gerald
Hi Gerald, Did you find an answer? Thanks Sanchu
@mailsanchu @gsigmund
Hi; I actually ended up figuring this out tonight.. you can actually use Gson Compatibility Mode to do this since GSON omits nulls by default. Probably not ideal (I'm not sure of the exact performance implications); but it's better than nothing at this stage.
POJO pojo = JsonIterator.deserialize(json_str, POJO.class);
GsonCompatibilityMode config = new GsonCompatibilityMode.Builder().build();
System.out.println(JsonStream.serialize(config, pojo));
Adding @JsonProperty(defaultValueToOmit = "null")
seems to do the trick.