shau-lok / google-gson

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

Double braces initialization seems to break toJson #370

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Let's say I have this class:
public class Command {
  Map<String, String> parameters;
  String name;

  Command(String name) {
    this.name = name;
  }

  public void addParameter(String name, String value) {
    parameters.put(name, value);
  }

  public String getParameter(String name) {
    return parameters.get(name);
  }
}

If I use double braces initialization and try to dump it I get an empty output:
  Command cmd = new Command("run") {{ 
    addParameter("distance", "100");
  }}
  Gson gson = new Gson();
  System.out.println(gson.toJson(cmd)); // <-- Empty line is printed

If I do initialization in the usual way I get correct output:
  Command cmd = new Command("run");
  cmd.addParameter("distance", "100");
  Gson gson = new Gson();
  System.out.println(gson.toJson(cmd)); // <-- Evertyhing is ok

I use Oralce JDK 1.7.0 on Kubuntu Linux

Original issue reported on code.google.com by iu.biryu...@gmail.com on 18 Oct 2011 at 6:42

GoogleCodeExporter commented 9 years ago
Forgot to mention I use version 1.7.2 of gson.

Original comment by iu.biryu...@gmail.com on 18 Oct 2011 at 6:44

GoogleCodeExporter commented 9 years ago
We'll fix this. Regardless, you shouldn't use double brace initialization. It's 
inefficient and ugly.

Original comment by limpbizkit on 19 Oct 2011 at 4:30