The problem I am trying to solve involves the typical file/folder-
structure of a directory-tree. I have a structure of indeterminate
Nodes where each Node has an indeterminate number of Elements -- where
each Element can be a Leaf.
In other words, something like this:
* Node
* Node
* Node
* Leaf
* Leaf
* Leaf
* Leaf
Easy enough, I wrote a Java method that builds this exact structure
via recursion. In a test-method I feed it the Map of data I am given
and convert it to the tree-structure like this:
private Node convertMapToTree( Map<String, Object> map, String
parentNodeName ) {
if( parentNodeName == null ) parentNodeName = "Root";
Node parent = new Node( parentNodeName );
Set<String> keys = map.keySet();
for( String key : keys ) {
Object value = map.get(key);
if( value == null ) {
Node node = new Node(key);
parent.addChild(node);
} else if( value instanceof String ) {
String url = (String)value;
Leaf leaf = new Leaf(key, url);
parent.addChild(leaf);
} else if( value instanceof Map ) {
Map<String, Object> subMap = (Map<String,
Object>)value;
Node node = getTestDataStruct2(subMap, key);
parent.addChild(node);
}
}
return parent;
}
The result I get is exactly what I expect (a structure similar to the
one outlined above).
So then I attempt to convert that root-Node object into JSON using
Gson, like this:
Node rootNode= getTestDataStruct2( dataMap, null);
String treeJson= new Gson().toJson( rootNode );
But GSon is not giving me what I expect. The result I am getting from
GSon is that my rootNode and any of *it's* children-Elements are being
included -- but when any of those children-Elements are also Nodes,
*their* children-Elements are *NOT* being included.
????
My tree-objects are very typical... but, for reference, here they are:
public class Element {
private String id;
private String name;
public Element(){}
public Element( String id, String name ) {
if( id == null ) {
id = name.toLowerCase() + "_" + new Date().getTime();
}
this.id = id;
this.name = name;
}
@Override
public String toString() {
return
this.name;
}
}
----------
public class Leaf extends Element {
private String url;
public Leaf(){}
public Leaf( String id, String name, String url ) {
super( id, name );
this.url = url;
}
public Leaf( String name, String url) {
this( null, name, url );
}
@Override
public String toString() {
return super.toString() + "(LEAF) points to '" + this.url +
"'";
}
}
----------
public class Node extends Element {
private List<Element> children = new ArrayList<Element>();
public Node(){}
public Node( String id, String name ) {
super( id, name );
}
public Node( String name ) {
this( null, name );
}
public void addChild( Element e ) {
this.children.add(e);
}
public boolean hasChildren() {
if( children.isEmpty() ) return false;
return true;
}
public List<Element> getChildren() {
return children;
}
@Override
public String toString() {
String result = super.toString() + "(NODE) has ";
if( this.hasChildren() ) {
result += "children [";
for( int i = 0, j = children.size() ; i < j ; i++ ) {
result += children.get(i);
if( i < j-1 ) result += ", ";
}
result += "]";
} else {
result += "no children.";
}
return result;
}
}
Any help would be greatly appreciated.
Please tell me that GSon should have no problem converting an Object-A
which contains an Object-B where Object-B contains a different Object-
A.
THANK YOU!
:D