yunussasmaz / google-gson

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

Cannot convert Object to Json with complex datastructure #245

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
public class JSonNode {
    private String label;
    private String identifier;
    private List<Map<String, Object>> items = new ArrayList<Map<String, Object>>();
}
I have remove get set methods for simplicity.

public static void main(String [] args  ) {
        JSonNode node= new JSonNode();
        Gson g = new Gson();
        node.setIdentifier("name");
        node.setLabel("name");
        Map<String, Object> values = new HashMap<String, Object>();

        values.put("name", "Africa");
        values.put("type", "continent");

        List<JSonNode> children = new ArrayList<JSonNode>();
        JSonNode child1 = new JSonNode();
        Map<String, Object> values1 = new HashMap<String, Object>();
        values1.put("name", "Egypt");
        values1.put("type", "country");
        child1.getItems().add(values1);

        JSonNode child2 = new JSonNode();
        values1 = new HashMap<String, Object>();
        values1.put("name", "Kenya");
        values1.put("type", "country");
        child2.getItems().add(values1);

        JSonNode child3 = new JSonNode();
        values1 = new HashMap<String, Object>();
        values1.put("name", "Sudan");
        values1.put("type", "country");
        child3.getItems().add(values1);

        children.add(child1);
        children.add(child2);
        children.add(child3);

        values.put("children", children);

        node.getItems().add(values);

        System.out.println(g.toJson(node));

    }

What is the expected output? What do you see instead?
Expected O/P :
{"label":"name","identifier":"name","items":[{"name":"Africa","children":{"name"
:"Africa","children":[{"items":[{"name":"Egypt","type":"country"}]},{"items":[{"
name":"Kenya","type":"country"}]},{"items":[{"name":"Sudan","type":"country"}]}]
,"type":"continent"},"type":"continent"}]}
Acutal O/P :
{"label":"name","identifier":"name","items":[{"name":"Africa","children":{},"typ
e":"continent"}]}

What version of the product are you using? On what operating system?
Gson 1.5, Windows, with JDK 1.5

Please provide any additional information below.

Original issue reported on code.google.com by nis...@gmail.com on 30 Sep 2010 at 10:56

GoogleCodeExporter commented 9 years ago
You'll have success by replacing 'Object' in your map with the actual type of 
the value.

Original comment by limpbizkit on 4 Nov 2010 at 10:53