Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Double brace initialization not working #346

Closed
GoogleCodeExporter opened this issue Mar 19, 2015 · 3 comments
Closed

Double brace initialization not working #346

GoogleCodeExporter opened this issue Mar 19, 2015 · 3 comments

Comments

@GoogleCodeExporter
Copy link

Map<String, List<String>> jsonData = new HashMap<String, List<String>>();
jsonData.put("test", new ArrayList<String>() {
    {
        add("1");
        add("2");
        add("3");
    }
});
System.out.println(new Gson().toJson(jsonData));
//OUTPUT = {}


Map<String, List<String>> jsonData = new HashMap<String, List<String>>();
ArrayList<String> list = new ArrayList<String>();
list.add("1");
list.add("2");
list.add("3");
jsonData.put("test", list);
System.out.println(new Gson().toJson(jsonData));

//OUTPUT = {"test":["1","2","3"]}

Original issue reported on code.google.com by neyas...@gmail.com on 13 Jul 2011 at 9:39

@GoogleCodeExporter
Copy link
Author

The fundamental problem is that you aren't supplying GSON with enough type 
information and so it has to infer the type from the value. You can work-around 
this by providing the type in your call to toJson():

  String json = gson.toJson(jsonData, new TypeToken<Map<String, List<String>>>() {}.getType()));

We still have a problem where GSON never serializes anonymous classes, which is 
an unfortunate policy in this instance.

Original comment by limpbizkit on 1 Oct 2011 at 4:40

  • Added labels: Priority-Low
  • Removed labels: Priority-Medium

@GoogleCodeExporter
Copy link
Author

Original comment by limpbizkit on 2 Oct 2011 at 3:38

  • Changed state: Duplicate

@GoogleCodeExporter
Copy link
Author

Issue 370 has been merged into this issue.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant