{ "var1" : {}, "var2" : { "var2.1" : "foobar", ...}, ...}
In that case, it's nice to know that var1 wasn't null, when building
the resulting object out of that, you could instantiate an empty
object.
My issue centered around the insertion of an empty object in an array,
which probably isn't a terrible situation either - at least you know
the size of the array being (de)serialized. Now that we're discussing
it, I'm not sure a change is a good idea either, as you said, it
creates some loss, and does seem worse than instantiating an empty
object. After this discussion, I guess the most I would ask for now
is a setting to not write empty objects, so at least we can control it
if we find it helps in some ways (in my current implementation, the
loss won't be harmful).
And, of course, I already added the bug before your response. :)
http://code.google.com/p/google-gson/issues/detail?id=30
-Eric
I disagree that the index of each element matters in all cases. If
you simply have a collection and all that matters is you had a
collection, then the index of each element doesn't matter. If what
you are trying to accomplish is to build an exact identical
representation of an object in JSON notation, then it does matter.
However, that is not the only valid use case. If you are using JSON
notation as simply a way to transmit information and your API only
cares about representing a collection of objects, it seems valid to me
to not send empty objects. It seems that the
excludeFieldsWithoutExposeAnnotation indicates the desire to not
always serialize exact representations of objects, and only provide
the elements for a specific situation... if none of those elements are
non-null in the object, why would your system care about serializing
them?
In my case, I am serializing a collection of objects and it turns out
some of them have null values for the attributes my API cares about,
in which case, I'd rather not even send them to my clients via JSON.
Seems like a valid use case to me, and if I'm sending empty objects,
my client will need to know to do nothing with that object.
In the end, it's not really a huge deal, I have alternatives - I could
make a pass through the collection and remove the objects that aren't
pertinent. I could write an implementation of JsonSerializer for the
collection that weeds out the non-pertient objects and then I'm not
even iterating through the collection twice. It just seemed like a
useful setting to build into the library, though I understand your
hesitation.
-Eric
-Erice-gson?hl=en-~----------~----~----~----~------~----~------~--~---