Search in sources :

Example 1 with JsonField

use of argo.jdom.JsonField in project stargate-core by tuplejump.

the class JsonDocumentTest method formatJsonNode.

public static void formatJsonNode(final JsonNode jsonNode, PrintWriter writer, int indent) throws IOException {
    switch(jsonNode.getType()) {
        case ARRAY:
            writer.append('[');
            final Iterator<JsonNode> elements = jsonNode.getElements().iterator();
            while (elements.hasNext()) {
                final JsonNode node = elements.next();
                writer.println();
                addTabs(writer, indent + 1);
                formatJsonNode(node, writer, indent + 1);
                if (elements.hasNext()) {
                    writer.append(",");
                }
            }
            if (!jsonNode.getElements().isEmpty()) {
                writer.println();
                addTabs(writer, indent);
            }
            writer.append(']');
            break;
        case OBJECT:
            writer.append('{');
            final Iterator<JsonField> jsonStringNodes = fieldSorter.sort(jsonNode.getFieldList()).iterator();
            while (jsonStringNodes.hasNext()) {
                final JsonField field = jsonStringNodes.next();
                writer.println();
                addTabs(writer, indent + 1);
                formatJsonNode(field.getName(), writer, indent + 1);
                writer.append(": ");
                formatJsonNode(field.getValue(), writer, indent + 1);
                if (jsonStringNodes.hasNext()) {
                    writer.append(",");
                }
            }
            if (!jsonNode.getFieldList().isEmpty()) {
                writer.println();
                addTabs(writer, indent);
            }
            writer.append('}');
            break;
        case STRING:
            writer.append('"').append(escapeString(jsonNode.getText())).append('"');
            break;
        case NUMBER:
            writer.append(jsonNode.getText());
            break;
        case FALSE:
            writer.append("false");
            break;
        case TRUE:
            writer.append("true");
            break;
        case NULL:
            writer.append("null");
            break;
        default:
            throw new RuntimeException("Coding failure in Argo:  Attempt to format a JsonNode of unknown type [" + jsonNode.getType() + "];");
    }
}
Also used : JsonField(argo.jdom.JsonField) JsonNode(argo.jdom.JsonNode)

Aggregations

JsonField (argo.jdom.JsonField)1 JsonNode (argo.jdom.JsonNode)1