discomarathon / google-gson

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

Pretty Print generates invalid JSON #49

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Version: 1.2

The following code returns an invalid JSON string:

-----

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class App {
    public static void main(String[] args) {
        Node root = new Node("root");
        root.createChild("Pete");

        Gson gson = new GsonBuilder()
                .setPrettyPrinting()
                .create();

        System.out.println(gson.toJson(root));
    }
}

class Node {
    private final String name;
    private final List<Node> children = new ArrayList<Node>();

    public Node(String name) {
        this.name = name;
    }

    public Node createChild(String childName) {
        Node child = new Node(childName);
        children.add(child);
        return child;
    }
}

Original issue reported on code.google.com by b.richt...@gmail.com on 27 Sep 2008 at 5:54

GoogleCodeExporter commented 9 years ago
Below is the output from the above program:
{"name":"root","children":[{,"name":"Pete","children":[]}]}

Notice the extra "," instead the "children" array.

Original comment by joel.leitch@gmail.com on 27 Sep 2008 at 6:42

GoogleCodeExporter commented 9 years ago
Fix submitted in r251.

Original comment by joel.leitch@gmail.com on 27 Sep 2008 at 7:10