discomarathon / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

Don't include empty ENUMs in serialized output #202

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I have a few classes with 

private final EnumSet<MyEnum> myflags = EnumSet.noneOf(MyEnum.class);

the JSON-serialized output contains
"myflags":[]

which is surprising given that from the docs
"This implementation handles nulls correctly, While serialization, a null
field is skipped from the output"

Yes, the field isn't null.  But it is empty.  Is there a way to "skip empty
collections" in the output?

It would be a huge space saver in the generated JSON file if there was a
way to skip empty EnumSet (or even better, any empty collection) -
especially for outputs that contain a LOT of instances of objects with
flags in them.

Latest gson release (1.4)

Original issue reported on code.google.com by benjamin...@gmail.com on 15 Apr 2010 at 2:44

GoogleCodeExporter commented 9 years ago
Treating empty as null will change the semantics of Java, so there needs to be 
a much better rationale for doing 
this. In the interim, you can do this by registering a custom serializer for 
your collections that treats empty as 
null. I will still advise against doing so though.

Original comment by inder123 on 16 Apr 2010 at 6:17

GoogleCodeExporter commented 9 years ago
hi! We do not want to change semantic of java or more general of the set 
theory. Yet we all used to live with those issues.

I also miss exactly this GSON feature: to treat empty collection as nulls (and 
optionally omitting in the serialization like already possible). Please let 
this decision be taken by the developer, if he wants empty fields not to be 
serialized.

an API on the GsonBuilder would be fine for the first time and maybe, extended 
by annotations in the future.

thanks

Original comment by eplisc...@gmail.com on 2 Apr 2013 at 2:03