* @param re Buffer to serialize to
* @param name Result requested
*/
// TODO: refactor
protected void resultToJSON(JSONBuffer re, String name) {
ResultHierarchy hier = result.getHierarchy();
// Find requested result
String[] parts = name.split("/");
Result cur = result;
int partpos = 0;
{
for(; partpos < parts.length; partpos++) {
// FIXME: handle name collisions. E.g. type_123?
boolean found = false;
for(Result child : hier.getChildren(cur)) {
// logger.debug("Testing result: " + child.getShortName() + " <-> " +
// parts[partpos]);
if(child.getLongName().equals(parts[partpos]) || child.getShortName().equals(parts[partpos])) {
cur = child;
found = true;
break;
}
}
if(!found) {
break;
}
}
if(cur == null) {
re.appendKeyValue("error", "result not found.");
return;
}
}
// logger.debug(FormatUtil.format(parts, ",") + " " + partpos + " " + cur);
// Result structure discovery via "children" parameter.
if(parts.length == partpos + 1) {
if("children".equals(parts[partpos])) {
re.appendKeyArray("children");
Iterator<Result> iter = hier.getChildren(cur).iterator();
while(iter.hasNext()) {
Result child = iter.next();
re.startHash();
re.appendKeyValue("name", child.getShortName());
re.appendKeyValue("type", child.getClass().getSimpleName());