How to recursively serialize an object using reflection ?
- by Tony
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?