guochaiqi / google-gson

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

Map<String, ...> with empty strings (or strings consisting of whitespace) don't serialize #227

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
JsonObject has this precondition check for map properties:

!"".equals(property.trim())

(line 55 of JsonObject.java in my checkout)

This causes this map to fail to serialize:

Map<String, Integer> map = new HashMap();
map.put("", 1);
map.put("k", 1000);
map.put("m", 1000000);

Original issue reported on code.google.com by jsha...@google.com on 26 Aug 2010 at 8:56

GoogleCodeExporter commented 9 years ago
This is a bug. The JSON spec permits empty strings as property names.

Original comment by limpbizkit on 27 Aug 2010 at 3:39

GoogleCodeExporter commented 9 years ago
Fixed with r585.

FYI... this bug was making it difficult for our application to serialize 
certain data sets to JSON. In Caliper we use maps for pretty-printing display 
units. For example, Caliper may render "48KiB", "3s" or "5 thousand" in its 
table. The empty string is used for counted units, like the "5" in "5 instances 
allocated".
   { "ms": 0.001, "s": 1, " minutes": 60 }
   { "B": 1, "KiB": 1024, "MiB": 1048576, "GiB": 1073741824 }
   { "": 1, " thousand": 1000, " million": 1000000 }

Original comment by limpbizkit on 28 Aug 2010 at 5:11

GoogleCodeExporter commented 9 years ago
Issue 256 has been merged into this issue.

Original comment by limpbizkit on 31 Oct 2010 at 2:38