java - how to avoid gson tojson recursion -
i have simple class:
myobject: - string index; - myobject parent; - list<myobject> childs;
i want print stored information json. use tojson function of gson library. due every child has link parent object face infinite loop recursion. there way define gson shall print parent index every child instead of dumping full information?
you need use @expose annotation.
public class myobject{ @expose string index; myobject parent; @expose list<myobject> children; }
then generate json using
gson gson = new gsonbuilder().excludefieldswithoutexposeannotation().create(); jsonstring = gson.tojson(data);
edit: can dfs parse when convert object. make method like:
public void setparents(myobj patent){ this.parent=parent; for(myobj o:children){ o.setparent(this); } }
and call root object.
Comments
Post a Comment