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

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

Introduction

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

Prototype

public ObjectNode objectNode() 

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 w  w  .ja  v  a 2 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;
}