discomarathon / google-gson

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

Empty Maps crash the json rendering when pretty printing is used - the next comma is missing from output #204

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Test Code:

HashMap<String, Object> hs = new HashMap<String, Object>();
HashMap<String, Object> details = new HashMap<String, Object>();

hs.put("id", 12);
hs.put("name", "Alejandro");
hs.put("details", details);

Boolean includeNullFields = true;
Boolean prettyRender = true;

try {

    GsonBuilder gsonBuilder = new GsonBuilder();

    if (includeNullFields && prettyRender)
        gsonBuilder.serializeNulls().setPrettyPrinting();
    else if (includeNullFields && !prettyRender)
        gsonBuilder.serializeNulls();
    else if (!includeNullFields && prettyRender)
        gsonBuilder.setPrettyPrinting();

    Gson gson = null;
    gson = gsonBuilder.create();
    String jsonResponse = gson.toJson(hs);

    System.out.println(jsonResponse);

} catch (Exception e) {
    e.printStackTrace();
}

The above code outputs: 
{"id":12,"details":{}"name":"Alejandro"}

If Pretty Printing is NOT used then the output is correct:
{"id":12,"details":{},"name":"Alejandro"}

Original issue reported on code.google.com by bobghera...@gmail.com on 21 Apr 2010 at 10:09

GoogleCodeExporter commented 9 years ago
The code in SVN works. I checked in a test case with r593.

Original comment by limpbizkit on 3 Sep 2010 at 6:45