Serialize nulls for specific fields?

400 views
Skip to first unread message

Aleem Mawani

unread,
Sep 17, 2012, 7:41:11 PM9/17/12
to googl...@googlegroups.com
Hi,

Is it possible to serialize nulls just for specific fields? I'd like to either specify an annotation that will determine whether this field is serialized when null or not OR be able to specify when writing a value in a TypeAdapter whether nulls should be written. Is this possible currently?

Brandon Mintern

unread,
Sep 17, 2012, 7:56:22 PM9/17/12
to googl...@googlegroups.com
Tell me what you've tried so far.
> --
> You received this message because you are subscribed to the Google Groups
> "google-gson" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/google-gson/-/6O6t__oKeEsJ.
> To post to this group, send email to googl...@googlegroups.com.
> To unsubscribe from this group, send email to
> google-gson...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/google-gson?hl=en.

Aleem Mawani

unread,
Sep 17, 2012, 8:12:36 PM9/17/12
to googl...@googlegroups.com
I tried the following solutions:

1) Turning on/off serializeNulls() in my typeadapater while serializing certain fields like follows:

boolean current = out.getSerializeNulls();
out.setSerializeNulls(true);
out.name("fieldName").value(someValue);
out.setSerializeNulls(false);

This didn't work unfortunately.

2) Creating two different Gson's with serializeNulls setup differently. Then when serializing choosing between the two gson's based on what I was serializing. That littered my code and seems inefficient because of the reflection.

Maaartin

unread,
Sep 17, 2012, 11:42:06 PM9/17/12
to googl...@googlegroups.com
On Tuesday, September 18, 2012 1:41:11 AM UTC+2, Aleem Mawani wrote:
Hi,

Is it possible to serialize nulls just for specific fields? I'd like to either specify an annotation that will determine whether this field is serialized when null or not OR be able to specify when writing a value in a TypeAdapter whether nulls should be written. Is this possible currently?

Everything's possible, in the worst case you'll need to patch gson yourself. I don't understand why your trick with temporarily changing `serializeNulls` doesn't work as I'm using this trick in my (first) patch from


Assuming the field gets initialized to `null`, my patch should work for you directly. Otherwise you'd need to remove the part determining the initial value of a field. Maybe there's even a simpler solution.

I wonder what you need it for. Maybe your reason is similar to mine?

Aleem Mawani

unread,
Sep 21, 2012, 8:21:29 PM9/21/12
to googl...@googlegroups.com
Doing: 

boolean current = out.getSerializeNulls();
out.setSerializeNulls(true);
out.name("fieldName").value(someValue);
out.setSerializeNulls(false);

doesn't work because calling setSerializeNulls just sets a boolean in the writer and calling out.name().value() simply queues up the write but doesnt actually write. Then I turn off serialize nulls and when the write actually happens, the boolean serializeNulls in the writer has already been set back. 

The best case would be an annotation that could be used to specify a particular field to serialize with nulls but I don't think this is possible.

Brandon Mintern

unread,
Sep 21, 2012, 8:40:50 PM9/21/12
to googl...@googlegroups.com
What if you flush?

boolean current = out.getSerializeNulls();
out.flush();
out.setSerializeNulls(true);
out.name("fieldName").value(someValue);
out.flush();
out.setSerializeNulls(current);
> --
> You received this message because you are subscribed to the Google Groups
> "google-gson" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/google-gson/-/8BSrzm7vPd4J.

Maaartin

unread,
Sep 21, 2012, 8:45:07 PM9/21/12
to googl...@googlegroups.com
On Saturday, September 22, 2012 2:21:30 AM UTC+2, Aleem Mawani wrote:
Doing: 

boolean current = out.getSerializeNulls();
out.setSerializeNulls(true);
out.name("fieldName").value(someValue);
out.setSerializeNulls(false);

doesn't work because calling setSerializeNulls just sets a boolean in the writer and calling out.name().value() simply queues up the write but doesnt actually write.

I know... I've ran into this... but it must happen someday and IIRC it happens when the value gets written. I've just had a look at my patch... I'm using `writer.nullValue();` and it works.
 
Then I turn off serialize nulls and when the write actually happens, the boolean serializeNulls in the writer has already been set back. 

No idea what your `out` is, but with `com.google.gson.stream.JsonWriter` the trick works.
 
The best case would be an annotation that could be used to specify a particular field to serialize with nulls but I don't think this is possible.

I'm pretty sure it is! And it can't be any harder than what I've done. It's about the same, just a little bit simpler, as I wanted to ignore the default value (i.e., the one present after the constructor or the `InstanceCreator` got called) and you want to ignore `null`.


Reply all
Reply to author
Forward
0 new messages