I want to recursively create a JSON tree from multiple strings. A string might like this:
.this.is:0.a.test
The character string is interpreted as follows:
.is an object:is an array:0.is the first object in the array
The corresponding JSON looks like this
{
"this":{
"is":[
{
"a":{
"test":"testvalue"
}
}
]
}
}
My code looks like this:
public static JsonNode recursiveLimb(ArrayList<String> limb, Map<String, String> mappingBodyMap, BidiMap<String, String> bidiMap, JsonNode currentTree, String path) {
if (!limb.isEmpty() && limb.get(0).equals(".")) {
//object node
String firstElem = limb.remove(0);
String secondElem = limb.remove(0);
//call recursion to build rest of tree
JsonNode resultJN = recursiveLimb(limb, mappingBodyMap, bidiMap, currentTree, path + firstElem + secondElem);
return mapper.createObjectNode().set(secondElem, resultJN);
} else if (!limb.isEmpty() && limb.get(0).equals(":")) {
//array node
String firstElem = limb.remove(0);
String secondElem = limb.remove(0);
//call recursion to build rest of tree
JsonNode resultJN = recursiveLimb(limb, mappingBodyMap, bidiMap, currentTree, path + firstElem + secondElem);
return mapper.createArrayNode().add(resultJN);
} else {
//value node
String value = bidiMap.getKey(path);
return new TextNode(mappingBodyMap.get(value));
}
}
This code returns all individual links of the tree. The problem is, I'm having a hard time putting them together. Hopefully someone can help me. Does someone have an idea what I can do?