How to recursively serialize an object using reflection ?

Posted by Tony on Stack Overflow See other posts from Stack Overflow or by Tony
Published on 2010-04-12T15:12:58Z Indexed on 2010/04/12 15:33 UTC
Read the original article Hit count: 414

Filed under:
|
|

I want to navigate to the N-th level of an object, and serialize it's properties in String format. For Example:

class Animal {
   public String name;
   public int weight;
   public Animal friend;
   public Set<Animal> children = new HashSet<Animal>() ;
}

should be serialized like this:

{name:"Monkey",
 weight:200,
 friend:{name:"Monkey Friend",weight:300 ,children:{...if has children}},
 children:{name:"MonkeyChild1",weight:100,children:{... recursively nested}}
}

And you may probably notice that it is similar to serializing an object to json. I know there're many libs can do this, can you give me some instructive ideas on how to write this?

© Stack Overflow or respective owner

Related posts about java

Related posts about JSON