Search in sources :

Example 1 with IJSONStructure

use of org.eclipse.wst.json.core.document.IJSONStructure in project webtools.sourceediting by eclipse.

the class TestUtil method serialize.

private static void serialize(IJSONNode node, StringBuilder json) {
    if (node == null) {
        return;
    }
    // .getNextSibling()) {
    switch(node.getNodeType()) {
        case IJSONNode.DOCUMENT_NODE:
            serialize(node.getFirstChild(), json);
            break;
        case IJSONNode.OBJECT_NODE:
        case IJSONNode.ARRAY_NODE:
            IJSONStructure object = (IJSONStructure) node;
            json.append(object.getFirstStructuredDocumentRegion().getText());
            serialize(node.getFirstChild(), json);
            if (object.isClosed()) {
                json.append(object.getEndStructuredDocumentRegion().getText());
            }
            if (node.getNextSibling() != null) {
                json.append(",");
                serialize(node.getNextSibling(), json);
            }
            break;
        case IJSONNode.PAIR_NODE:
            IJSONPair pair = (IJSONPair) node;
            json.append("\"");
            json.append(pair.getName());
            json.append("\"");
            if (pair.getValue() != null) {
                json.append(":");
                serialize(pair.getValue(), json);
            }
            if (node.getNextSibling() != null) {
                json.append(",");
                serialize(node.getNextSibling(), json);
            }
            break;
        case IJSONNode.VALUE_BOOLEAN_NODE:
        case IJSONNode.VALUE_NULL_NODE:
        case IJSONNode.VALUE_NUMBER_NODE:
        case IJSONNode.VALUE_STRING_NODE:
            IJSONValue value = (IJSONValue) node;
            json.append(value.getSimpleValue());
            if (node.getNextSibling() != null) {
                json.append(",");
                serialize(node.getNextSibling(), json);
            }
            break;
    }
// }
}
Also used : IJSONPair(org.eclipse.wst.json.core.document.IJSONPair) IJSONValue(org.eclipse.wst.json.core.document.IJSONValue) IJSONStructure(org.eclipse.wst.json.core.document.IJSONStructure)

Aggregations

IJSONPair (org.eclipse.wst.json.core.document.IJSONPair)1 IJSONStructure (org.eclipse.wst.json.core.document.IJSONStructure)1 IJSONValue (org.eclipse.wst.json.core.document.IJSONValue)1