def asJSON(tree): """ Return a String with a JSON map containing the name, id, and list of connections. """ json = StringBuilder() json.append('{"id" : "').append(tree.id).append('",\n') json.append(' "name" : "').append(getTitle(tree)).append('",\n') tableOut, tableIn = findConnections(tree) json.append(' "outgoing" : [') for target, num in sortMapByValue(tableOut, True): json.append(' ["').append(getTitle(target)).append('", ').append(num).append('],\n') if len(tableOut) > 0: json.setLength(json.length()-2) json.append('],\n') json.append(' "incoming" : [') for origin, num in sortMapByValue(tableIn, True): json.append(' ["').append(getTitle(origin)).append('", ').append(num).append('],\n') if len(tableIn) > 0: json.setLength(json.length()-2) json.append(']}\n') return json.toString()