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

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -