narayana1208 / google-gson

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

Support for different serializers for root and nested objects of same class #177

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Suppose i have some class like this one:
TreeNode {
  int id;
  TreeNode parent;
  List<TreeNode> children;
  String nodeName;
  String nodeValue;
}

I want to be able to get following JSON:
{"id":<id>, "parent":<parent id>, "nodeName":<name>, "nodeValue":<value>, 
"children":[<child 
1 id>, <child 2 id>...]}

Using serializer like this one:
    public JsonElement serialize(TreeNode src, Type typeOfSrc, JsonSerializationContext context) {
        return new JsonPrimitive(src.getId());
    }

For all objects except root one will solve this problem

Original issue reported on code.google.com by zlow...@gmail.com on 20 Nov 2009 at 3:48

GoogleCodeExporter commented 9 years ago
Can't you handle this at the application level? Detect if you're serializing 
the parent node?

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