Issue 512 in google-gson: Exclude empty array in Gson serialization

135 views
Skip to first unread message

googl...@googlecode.com

unread,
May 1, 2013, 1:25:39 PM5/1/13
to google-gson...@googlegroups.com
Status: New
Owner: ----
Labels: Type-Defect Priority-Medium

New issue 512 by chanda...@gmail.com: Exclude empty array in Gson
serialization
http://code.google.com/p/google-gson/issues/detail?id=512

What steps will reproduce the problem?
1. In Java class, define an attribute as List
private List<Score> Scores = new ArrayList<Score>();
2. If Score (ArrayList) is not populated or empty, while generating JSON
file, an empty array is getting generated

],
"scores": []
},

What is the expected output? What do you see instead?

Option 1 - If a collection or an array is empty, it should not generate an
empty array.

Example - com.google.gson.internal.bind.CollectionTypeAdapterFactory.write()
existing code - if (collection == null) {
expected - if (collection == null || collection.size() < 1) {

Option 2 - configuration property to enable/disable adding empty array,
similar to Jackon SerializationFeature.WRITE_EMPTY_JSON_ARRAYS
(http://wiki.fasterxml.com/JacksonFeaturesSerialization)

What version of the product are you using? On what operating system?
version 2.2.3
operating system - Mac OSX 10.8.3

Please provide any additional information below.

--
You received this message because this project is configured to send all
issue notifications to this address.
You may adjust your notification preferences at:
https://code.google.com/hosting/settings

googl...@googlecode.com

unread,
Jun 30, 2013, 10:26:48 PM6/30/13
to google-gson...@googlegroups.com

Comment #1 on issue 512 by Charles....@gmail.com: Exclude empty array in
Gson serialization
http://code.google.com/p/google-gson/issues/detail?id=512

This is very annoying to me.
We designed a format with many optional fields (lists in different types),
and it shows all these empty optional fields.

googl...@googlecode.com

unread,
Aug 19, 2013, 9:45:03 PM8/19/13
to google-gson...@googlegroups.com

Comment #2 on issue 512 by Ewerto...@gmail.com: Exclude empty array in Gson
serialization
http://code.google.com/p/google-gson/issues/detail?id=512

Possible solution?

public String toJson(Object src, Type typeOfSrc) {
StringWriter writer = new StringWriter();
toJson(src, typeOfSrc, writer);

String jsonObject = writer.toString();

if (src instanceof ArrayList) {
if(jsonObject.contains("[")){
int index = jsonObject.indexOf("[");
char nextChar = jsonObject.charAt(index + 1);

if(nextChar == ']'){
jsonObject = null;
}
}
}

return jsonObject;

googl...@googlecode.com

unread,
Dec 16, 2013, 5:08:58 AM12/16/13
to google-gson...@googlegroups.com

Comment #3 on issue 512 by undisput...@gmail.com: Exclude empty array in
Gson serialization
http://code.google.com/p/google-gson/issues/detail?id=512

I have a tangentially related issue. I feel that it is unnecessary to start
a new case so I'll write it here.

Sometimes I receive a -1 or 0 for when the Json array contains nothing. For
example, usually there might be a field "api_array":[1,2,3], but when the
array has nothing I get "api_array":-1 instead.

Currently I'm using a workaround as suggested by the folks on
StackOverflow, but I feel that there should be some elegant way of telling
Gson that "If the expected array has only 1 field or nothing, skip it or
otherwise mark it as blank".

Perhaps a flag in GsonBuilder?

googl...@googlecode.com

unread,
May 15, 2015, 9:45:01 AM5/15/15
to google-gson...@googlegroups.com

Comment #4 on issue 512 by jothi.ma...@gmail.com: Exclude empty array in
Gson serialization
https://code.google.com/p/google-gson/issues/detail?id=512

Hi,
I have the same issue. Here is how i solved it.

In the bean definition, make sure you didnt initialize the variable.

Like:

List<> scores =new ArrayList<>(); //Wrong way.

List<> scores; //Right way

It is too late to respond for this issue. still Someone might get solution
for this. :)
Reply all
Reply to author
Forward
0 new messages