Example usage for com.fasterxml.jackson.databind.node ObjectNode get

List of usage examples for com.fasterxml.jackson.databind.node ObjectNode get

Introduction

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

Prototype

public JsonNode get(String paramString) 

Source Link

Usage

From source file:org.apache.streams.data.util.JsonUtil.java

/**
 * Creates an empty array if missing/*from w w w  .j a  v  a  2s .co m*/
 * @param node object to create the array within
 * @param field location to create the array
 * @return the Map representing the extensions property
 */
public static ArrayNode ensureArray(ObjectNode node, String field) {
    String[] path = Lists.newArrayList(Splitter.on('.').split(field)).toArray(new String[0]);
    ObjectNode current = node;
    ArrayNode result = null;
    for (int i = 0; i < path.length; i++) {
        current = ensureObject((ObjectNode) node.get(path[i]), path[i]);
    }
    if (current.get(field) == null)
        current.put(field, mapper.createArrayNode());
    result = (ArrayNode) node.get(field);
    return result;
}

From source file:com.addthis.codec.jackson.Jackson.java

public static void merge(ObjectNode primary, ObjectNode backup) {
    Iterator<String> fieldNames = backup.fieldNames();
    while (fieldNames.hasNext()) {
        String fieldName = fieldNames.next();
        JsonNode primaryValue = primary.get(fieldName);
        if (primaryValue == null) {
            JsonNode backupValue = backup.get(fieldName).deepCopy();
            primary.set(fieldName, backupValue);
        } else if (primaryValue.isObject()) {
            JsonNode backupValue = backup.get(fieldName);
            if (backupValue.isObject()) {
                merge((ObjectNode) primaryValue, (ObjectNode) backupValue.deepCopy());
            }//from   w w  w  . j  av  a  2 s  .c o  m
        }
    }
}

From source file:controllers.MetaController.java

private static void put(final ObjectNode node, final String remaining, final ConfigValue value) {
    final int dotIndex = remaining.indexOf('.');
    final boolean leaf = dotIndex == -1;
    if (leaf) {// w w w . j av a  2s  . co  m
        node.set(remaining, getConfigNode(value.unwrapped()));
    } else {
        final String firstChunk = remaining.substring(0, dotIndex);
        ObjectNode child = (ObjectNode) node.get(firstChunk);
        if (child == null || child.isNull()) {
            child = JsonNodeFactory.instance.objectNode();
            node.set(firstChunk, child);
        }
        put(child, remaining.substring(dotIndex + 1), value);
    }
}

From source file:com.palominolabs.crm.sf.rest.RestConnectionImpl.java

@Nonnull
private static JsonNode getNode(ObjectNode jsonNode, String key) throws ResponseParseException {
    JsonNode value = jsonNode.get(key);
    if (isNull(value)) {
        throw new ResponseParseException("Null value for key <" + key + ">");
    }/*from   w  w  w. jav  a 2s .  com*/
    return value;
}

From source file:com.squarespace.template.plugins.platform.CommerceUtils.java

public static ArrayNode getItemVariantOptions(JsonNode item) {
    JsonNode structuredContent = item.path("structuredContent");
    JsonNode variants = structuredContent.path("variants");
    if (variants.size() <= 1) {
        return EMPTY_ARRAY;
    }/*from   ww  w .  j a v  a 2s. c  om*/

    ArrayNode userDefinedOptions = JsonUtils.createArrayNode();
    JsonNode ordering = structuredContent.path("variantOptionOrdering");
    for (int i = 0; i < ordering.size(); i++) {
        String optionName = ordering.path(i).asText();
        ObjectNode option = JsonUtils.createObjectNode();
        option.put("name", optionName);
        option.put("values", JsonUtils.createArrayNode());
        userDefinedOptions.add(option);
    }

    for (int i = 0; i < variants.size(); i++) {
        JsonNode variant = variants.path(i);
        JsonNode attributes = variant.get("attributes");
        if (attributes == null) {
            continue;
        }
        Iterator<String> fields = attributes.fieldNames();

        while (fields.hasNext()) {
            String field = fields.next();

            String variantOptionValue = attributes.get(field).asText();
            ObjectNode userDefinedOption = null;

            for (int j = 0; j < userDefinedOptions.size(); j++) {
                ObjectNode current = (ObjectNode) userDefinedOptions.get(j);
                if (current.get("name").asText().equals(field)) {
                    userDefinedOption = current;
                }
            }

            if (userDefinedOption != null) {
                boolean hasOptionValue = false;
                ArrayNode optionValues = (ArrayNode) userDefinedOption.get("values");
                for (int k = 0; k < optionValues.size(); k++) {
                    String optionValue = optionValues.get(k).asText();
                    if (optionValue.equals(variantOptionValue)) {
                        hasOptionValue = true;
                        break;
                    }
                }

                if (!hasOptionValue) {
                    optionValues.add(variantOptionValue);
                }
            }
        }
    }
    return userDefinedOptions;
}

From source file:controllers.Auth.java

public static Promise<Result> authenticate() {
    final Form<Login> loginForm = form(Login.class).bindFromRequest();
    if (loginForm.hasErrors()) {
        return Promise.promise(new ErrorResult(login.render(loginForm)));
    }/*from w  w  w . j a v  a  2  s .  c om*/
    final ObjectNode user = Json.newObject();
    user.put("username", loginForm.get().email);
    user.put("password", loginForm.get().password);
    Promise<Boolean> exists = User.nodes.exists(user);
    return exists.map(new Function<Boolean, Result>() {
        public Result apply(Boolean exists) {
            if (exists) {
                session().clear();
                session("username", user.get("username").asText());
                flash("success", "Login successful.");
                return redirect(routes.Application.home());
            } else {
                flash("error", "Login failed: Unknown user.");
                return badRequest(login.render(loginForm));
            }
        }
    });
}

From source file:org.waarp.common.json.JsonHandler.java

/**
 * //from w  w w  . java2  s.c om
 * @param node
 * @param field
 * @param defValue
 * @return the String if the field exists, else defValue
 */
public final static String getValue(ObjectNode node, String field, String defValue) {
    JsonNode elt = node.get(field);
    if (elt != null) {
        String val = elt.asText();
        if (val.equals("null")) {
            return defValue;
        }
        return val;
    }
    return defValue;
}

From source file:org.waarp.common.json.JsonHandler.java

/**
 * //ww w . java2 s. c o  m
 * @param node
 * @param field
 * @param defValue
 * @return the byte array if the field exists, else defValue
 */
public final static byte[] getValue(ObjectNode node, String field, byte[] defValue) {
    JsonNode elt = node.get(field);
    if (elt != null) {
        try {
            return elt.binaryValue();
        } catch (IOException e) {
            return defValue;
        }
    }
    return defValue;
}

From source file:com.redhat.lightblue.query.SetExpression.java

/**
 * Parses a set expression using the given json object
 *///ww w.  j a v  a 2  s .co m
public static SetExpression fromJson(ObjectNode node) {
    if (node.size() == 1) {
        UpdateOperator op = null;
        if (node.has(UpdateOperator._add.toString())) {
            op = UpdateOperator._add;
        } else if (node.has(UpdateOperator._set.toString())) {
            op = UpdateOperator._set;
        }
        if (op != null) {
            ObjectNode arg = (ObjectNode) node.get(op.toString());
            List<FieldAndRValue> list = new ArrayList<>();
            for (Iterator<Map.Entry<String, JsonNode>> itr = arg.fields(); itr.hasNext();) {
                Map.Entry<String, JsonNode> entry = itr.next();
                Path field = new Path(entry.getKey());
                RValueExpression rvalue = RValueExpression.fromJson(entry.getValue());
                list.add(new FieldAndRValue(field, rvalue));
            }
            return new SetExpression(op, list);
        }
    }
    throw Error.get(QueryConstants.ERR_INVALID_SET_EXPRESSION, node.toString());
}

From source file:com.redhat.lightblue.query.UnaryLogicalExpression.java

/**
 * Parses a unary logical expression using the given object node
 *//*from  w  ww.j  a va  2s.c om*/
public static UnaryLogicalExpression fromJson(ObjectNode node) {
    if (node.size() != 1) {
        throw Error.get(QueryConstants.ERR_INVALID_LOGICAL_EXPRESSION, node.toString());
    }
    String fieldName = node.fieldNames().next();
    UnaryLogicalOperator op = UnaryLogicalOperator.fromString(fieldName);
    if (op == null) {
        throw Error.get(QueryConstants.ERR_INVALID_LOGICAL_EXPRESSION, node.toString());
    }
    QueryExpression q = QueryExpression.fromJson(node.get(fieldName));
    return new UnaryLogicalExpression(op, q);
}