Example usage for com.fasterxml.jackson.databind.node JsonNodeFactory arrayNode

List of usage examples for com.fasterxml.jackson.databind.node JsonNodeFactory arrayNode

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind.node JsonNodeFactory arrayNode.

Prototype

public ArrayNode arrayNode() 

Source Link

Usage

From source file:com.redhat.lightblue.eval.Projector.java

private JsonNode project(JsonNodeFactory factory, Path contextPath, FieldTreeNode contextNode,
        JsonNodeCursor cursor, QueryEvaluationContext ctx, boolean processingArray) {
    JsonNode parentNode = null;/*  w  ww.j  av a2 s .c om*/
    do {
        Path fieldPath = cursor.getCurrentPath();
        Path contextRelativePath = contextPath.isEmpty() ? fieldPath
                : fieldPath.suffix(-contextPath.numSegments());
        JsonNode fieldNode = cursor.getCurrentNode();
        LOGGER.debug("project context={} fieldPath={} contextRelativePath={} isArray={}", contextPath,
                fieldPath, contextRelativePath, processingArray);
        FieldTreeNode fieldMd = contextNode.resolve(contextRelativePath);
        if (fieldMd != null) {
            Boolean result = project(fieldPath, ctx);
            LOGGER.debug("Projecting '{}' in context '{}': {}", contextRelativePath, contextPath, result);
            if (result == null) {
                // Projection is undecisive. Recurse into array/object/reference nodes and see if anything is projected there
                if (!(fieldNode instanceof NullNode)) {
                    if (fieldMd instanceof ObjectField || fieldMd instanceof ArrayField
                            || fieldMd instanceof ObjectArrayElement
                            || fieldMd instanceof ResolvedReferenceField) {
                        JsonNode newNode;
                        if (cursor.firstChild()) {
                            newNode = (getNestedProjector() == null ? this : getNestedProjector()).project(
                                    factory, contextPath, contextNode, cursor, ctx,
                                    !(fieldMd instanceof ObjectField || fieldMd instanceof ObjectArrayElement));
                            cursor.parent();
                        } else {
                            if (fieldMd instanceof ObjectField)
                                newNode = factory.objectNode();
                            else
                                newNode = factory.arrayNode();
                        }
                        if (newNode != null) {
                            if (newNode instanceof ArrayNode)
                                newNode = sort(factory, this, (ArrayNode) newNode, fieldPath);
                            if (parentNode == null)
                                parentNode = processingArray ? factory.arrayNode() : factory.objectNode();
                            if (parentNode instanceof ArrayNode)
                                ((ArrayNode) parentNode).add(newNode);
                            else
                                ((ObjectNode) parentNode).set(fieldPath.tail(0), newNode);
                        }
                    }
                }
            } else if (result) {
                // Field is included
                if (fieldNode instanceof NullNode) {
                    if (parentNode == null)
                        parentNode = processingArray ? factory.arrayNode() : factory.objectNode();
                    if (parentNode instanceof ArrayNode)
                        ((ArrayNode) parentNode).add(fieldNode);
                    else
                        ((ObjectNode) parentNode).set(fieldPath.tail(0), fieldNode);
                } else {
                    JsonNode newNode = null;
                    if (fieldMd instanceof ObjectField || fieldMd instanceof ObjectArrayElement) {
                        if (cursor.firstChild()) {
                            newNode = (getNestedProjector() == null ? this : getNestedProjector())
                                    .project(factory, contextPath, contextNode, cursor, ctx, false);
                            cursor.parent();
                            if (newNode == null)
                                newNode = factory.objectNode();
                        }
                    } else if (fieldMd instanceof ArrayField || fieldMd instanceof ResolvedReferenceField) {
                        if (cursor.firstChild()) {
                            newNode = (getNestedProjector() == null ? this : getNestedProjector())
                                    .project(factory, contextPath, contextNode, cursor, ctx, true);
                            cursor.parent();
                            if (newNode == null)
                                newNode = factory.arrayNode();
                        }
                    } else if (fieldMd instanceof SimpleField) {
                        newNode = fieldNode;
                    } else if (fieldMd instanceof SimpleArrayElement) {
                        newNode = fieldNode;
                    }
                    if (newNode != null) {
                        if (newNode instanceof ArrayNode)
                            newNode = sort(factory, this, (ArrayNode) newNode, fieldPath);
                        if (parentNode == null)
                            parentNode = processingArray ? factory.arrayNode() : factory.objectNode();
                        if (parentNode instanceof ArrayNode)
                            ((ArrayNode) parentNode).add(newNode);
                        else
                            ((ObjectNode) parentNode).set(fieldPath.tail(0), newNode);
                    }
                }
            }
        } else {
            LOGGER.warn("Unknown field:{}", fieldPath);
        }
    } while (cursor.nextSibling());
    return parentNode;
}

From source file:uk.ac.ucl.excites.sapelli.collector.SapColCmdLn.java

static public void printProjectInfoJSON(File sapFile, Project project) throws IOException {
    // Create the node factory that gives us nodes.
    JsonNodeFactory factory = new JsonNodeFactory(false);

    // create a json factory to write the treenode as json. for the example
    // we just write to console
    JsonFactory jsonFactory = new JsonFactory();
    JsonGenerator generator = jsonFactory.createGenerator(System.out);
    ObjectMapper mapper = new ObjectMapper();

    // the root node
    ObjectNode projectJSON = factory.objectNode();

    // describe project:
    projectJSON.put("source", sapFile.getAbsolutePath());
    projectJSON.put("id", project.getID());
    projectJSON.put("fingerprint", project.getFingerPrint());
    projectJSON.put("name", project.getName());
    projectJSON.put("variant", project.getVariant());
    projectJSON.put("version", project.getVersion());
    projectJSON.put("display-name", project.toString(false));
    projectJSON.put("model-id", project.getModel().id);
    projectJSON.put("install-path", fsp.getProjectInstallationFolder(project, false).getAbsolutePath());
    ArrayNode formsJSON = factory.arrayNode();
    for (Form frm : project.getForms()) {
        ObjectNode formJSON = factory.objectNode();
        formJSON.put("id", frm.id);
        formJSON.put("produces-data", frm.isProducesRecords());
        formJSON.put("model-schema-number",
                (frm.isProducesRecords() ? frm.getSchema().getModelSchemaNumber() : null));
        formsJSON.add(formJSON);//from ww  w.ja va  2  s  .  co m
    }
    projectJSON.set("forms", formsJSON);

    // Serialise:
    mapper.writeTree(generator, projectJSON);
}

From source file:uk.ac.ucl.excites.sapelli.collector.SapColCmdLn.java

/**
 * @param sapFile//from w  w  w. jav  a 2  s  . c  o  m
 * @param project
 * @throws IOException
 * @see https://github.com/ExCiteS/geokey-sapelli
 */
static public void printProjectInfoForGeoKey(File sapFile, Project project) throws IOException {
    GeoKeyFormDescriber gkFormDescriber = new GeoKeyFormDescriber();

    // Create the node factory that gives us nodes.
    JsonNodeFactory factory = new JsonNodeFactory(false);

    // create a json factory to write the treenode as json. for the example
    // we just write to console
    JsonFactory jsonFactory = new JsonFactory();
    JsonGenerator generator = jsonFactory.createGenerator(System.out);
    ObjectMapper mapper = new ObjectMapper();

    // the root node
    ObjectNode projectJSON = factory.objectNode();

    // describe project:
    projectJSON.put("name", project.getName());
    projectJSON.put("variant", project.getVariant());
    projectJSON.put("version", project.getVersion());
    projectJSON.put("display_name", project.toString(false));
    projectJSON.put("sapelli_id", project.getID());
    projectJSON.put("sapelli_fingerprint", project.getFingerPrint());
    projectJSON.put("sapelli_model_id", project.getModel().id);
    projectJSON.put("installation_path", fsp.getProjectInstallationFolder(project, false).getAbsolutePath());
    ArrayNode formsJSON = factory.arrayNode();
    for (Form frm : project.getForms()) {
        ObjectNode formNode = gkFormDescriber.getFormJSON(frm);
        if (formNode != null)
            formsJSON.add(formNode);
    }
    projectJSON.set("forms", formsJSON);

    // Serialise:
    mapper.writeTree(generator, projectJSON);
}