java icon indicating copy to clipboard operation
java copied to clipboard

jsonstream.serialize how to skip null fields

Open gsigmund opened this issue 5 years ago • 3 comments

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

gsigmund avatar Jun 22 '19 16:06 gsigmund

Hi Gerald, Did you find an answer? Thanks Sanchu

mailsanchu avatar Mar 05 '20 19:03 mailsanchu

@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));

drewgatchell avatar Sep 26 '20 01:09 drewgatchell

Adding @JsonProperty(defaultValueToOmit = "null") seems to do the trick.

gmokki avatar Nov 19 '20 14:11 gmokki