Example usage for com.fasterxml.jackson.databind.node JsonNodeType OBJECT

List of usage examples for com.fasterxml.jackson.databind.node JsonNodeType OBJECT

Introduction

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

Prototype

JsonNodeType OBJECT

To view the source code for com.fasterxml.jackson.databind.node JsonNodeType OBJECT.

Click Source Link

Usage

From source file:com.smartsheet.tin.filters.pkpublish.PublishAction.java

static public PublishAction newFromJson(JsonNode node) throws JsonFilterException, JsonProcessingException {
    confirmNodeType(node, JsonNodeType.OBJECT, "action", logger);

    PublishAction pa = new PublishAction();
    pa.setRoutingKey(fetchChildString(node, "routing_key", false));
    pa.setMessage(fetchChildString(node, "message", false));
    return pa;// w w w .j a v  a2s  .c  om
}

From source file:com.smartsheet.tin.filters.common.JsonFilterTools.java

/**
 * Fetch a child node from a JSON object node.
 * //from  ww w  .jav a 2s .  c  o m
 * @param parent The parent JSON object node.
 * @param node_name The name of the child node.
 * @param node_type array, object, string, number, container, value, or bool
 * @return The child node.
 * @throws JsonFilterException
 */
public static JsonNode fetchChildByName(JsonNode parent, String node_name, String node_type)
        throws JsonFilterException {
    if (JsonNodeType.OBJECT != parent.getNodeType()) {
        String err = String.format("parent must be an Object/{}/hash type");
        throw new JsonFilterException(err);
    }

    if (parent.hasNonNull(node_name)) {
        JsonNode child = parent.get(node_name);
        boolean type_error = false;

        if (node_type == null || node_type.isEmpty()) {
            // Do no validation on the child type.
            type_error = false;
        } else {
            if (node_type.equals("array")) {
                if (!child.isArray()) {
                    type_error = true;
                }
            } else if (node_type.equals("object")) {
                if (!child.isObject()) {
                    type_error = true;
                }
            } else if (node_type.equals("string")) {
                if (!child.isTextual()) {
                    type_error = true;
                }
            } else if (node_type.equals("number")) {
                if (!child.isNumber()) {
                    type_error = true;
                }
            } else if (node_type.equals("container")) {
                if (!child.isContainerNode()) {
                    type_error = true;
                }
            } else if (node_type.equals("value")) {
                if (!child.isValueNode()) {
                    type_error = true;
                }
            } else if (node_type.startsWith("bool")) {
                if (!child.isBoolean()) {
                    type_error = true;
                }
            }
        }
        if (type_error == true) {
            String err = String.format("Type error: child node '%s' " + "is not a %s", node_name, node_type);
            throw new JsonFilterException(err);
        }
        return child;
    } else {
        String err = String.format("Child node '%s', type: '%s' not found", node_name, node_type);
        throw new JsonFilterChildNotFound(err);
    }
}

From source file:io.fabric8.che.starter.template.CheServerTemplateTest.java

@Test
public void processTemplate() throws MalformedURLException, IOException {
    String json = template.get();
    assertTrue(!StringUtils.isEmpty(json));
    ObjectMapper mapper = new ObjectMapper();
    JsonNode node = mapper.readTree(json);
    assertTrue(node.getNodeType() == JsonNodeType.OBJECT);
}

From source file:com.smartsheet.tin.filters.pkpublish.RowPattern.java

public static RowPattern newFromJson(JsonNode node)
        throws JsonProcessingException, JsonFilterException, RowPatternException {
    if (JsonNodeType.OBJECT != node.getNodeType()) {
        String err = String.format("RowPattern node must be an Object, " + "it is '%s'", node.getNodeType());
        logger.error(err);//ww w. j ava 2  s .com
        throw new JsonFilterException(err);
    }

    RowPattern rp = new RowPattern();

    rp.setSchema(fetchChildString(node, "schema", true));
    rp.setTable(fetchChildString(node, "table", true));

    JsonNode change_types = fetchChildByName(node, "change_types", "array");
    for (JsonNode ctype : change_types) {
        rp.addChangeType(ctype.asText());
    }
    return rp;
}

From source file:com.smartsheet.tin.filters.pkpublish.RowFilter.java

public static RowFilter newFromJson(JsonNode node) throws JsonFilterException, JsonProcessingException {
    confirmNodeType(node, JsonNodeType.OBJECT, "RowFilter", logger);

    RowFilter rf = new RowFilter();
    rf.setName(fetchChildString(node, "name", false));
    rf.setPublish(fetchChildBoolean(node, "include", true));
    try {//from  www. ja  v a2s. com
        rf.setRowPattern(RowPattern.newFromJson(fetchChildByName(node, "row_pattern", "object")));
    } catch (RowPatternException e) {
        e.printStackTrace();
        throw new JsonFilterException(e);
    }

    // FIXME:  Handle actions without the excess of coupling here now.
    // We really need a way to have the actions support an efficient
    // visitor pattern.
    JsonNode actions_jn = null;
    try {
        actions_jn = fetchChildByName(node, "actions", "array");
    } catch (JsonFilterChildNotFound e) {
        // Do nothing.
    }
    if (actions_jn != null) {
        for (JsonNode action_jn : actions_jn) {
            confirmNodeType(action_jn, JsonNodeType.OBJECT, "action", logger);
            String action_type = fetchChildString(action_jn, "type", true);
            if (action_type.equals("publish")) {
                PublishAction pa = PublishAction.newFromJson(action_jn);
                rf.setPublish(true);
                rf.setRoutingKey(pa.getRoutingKey());
                rf.setMessage(pa.getMessage());
                rf.actions.add(pa);
            } else {
                String err = "Unknown action type: '" + action_type + "'";
                logger.error(err);
                throw new JsonFilterException(err);
            }
        }
    }
    return rf;
}

From source file:com.spotify.hamcrest.jackson.IsJsonObject.java

private IsJsonObject(final LinkedHashMap<String, Matcher<? super JsonNode>> entryMatchers) {
    super(JsonNodeType.OBJECT);
    this.entryMatchers = Objects.requireNonNull(entryMatchers);
}

From source file:de.thingweb.typesystem.TypeSystemChecker.java

public static boolean isJsonSchemaType(JsonNode type) {
    boolean isJsonSchema = false;

    // TODO use JSON schema parser
    if (type != null && type.getNodeType() == JsonNodeType.OBJECT) {
        JsonNode value = type.findValue("type");
        if (value != null) {
            isJsonSchema = JSON_SCHEMA_PRIMITIVE_TYPES.contains(value.asText());
        }/*from w  w w.j  a  v  a 2  s .c om*/
    }

    return isJsonSchema;

}

From source file:org.thingsplode.synapse.serializers.jackson.adapters.ParameterWrapperDeserializer.java

@Override
public ParameterWrapper deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    JsonNode node = jp.readValueAsTree();
    if (node != null && node.size() > 0 && node.isContainerNode()) {
        ParameterWrapper pw = ParameterWrapper.create();
        ArrayNode paramsNode = (ArrayNode) node.get("params");
        Iterator<JsonNode> elemIterator = paramsNode.elements();
        while (elemIterator.hasNext()) {
            JsonNode currentNode = elemIterator.next();
            if (currentNode.getNodeType() == JsonNodeType.OBJECT) {
                try {
                    String paramid = ((ObjectNode) currentNode).get("paramid").asText();
                    String typeName = ((ObjectNode) currentNode).get("type").asText();
                    Class paramType = null;
                    if (null != typeName)
                        switch (typeName) {
                        case "long":
                            paramType = Long.TYPE;
                            break;
                        case "byte":
                            paramType = Byte.TYPE;
                            break;
                        case "short":
                            paramType = Short.TYPE;
                            break;
                        case "int":
                            paramType = Integer.TYPE;
                            break;
                        case "float":
                            paramType = Float.TYPE;
                            break;
                        case "double":
                            paramType = Double.TYPE;
                            break;
                        case "boolean":
                            paramType = Boolean.TYPE;
                            break;
                        case "char":
                            paramType = Character.TYPE;
                            break;
                        default:
                            paramType = Class.forName(typeName);
                            break;
                        }/*from   w  w  w.  j  ava2s. c  o  m*/
                    Object parameterObject = jp.getCodec().treeToValue(currentNode.get("value"), paramType);
                    pw.add(paramid, paramType, parameterObject);
                } catch (ClassNotFoundException ex) {
                    throw new JsonParseException(jp, ex.getMessage());
                }
            }
        }
        return pw;
    } else {
        return null;
    }
}

From source file:com.smartsheet.tin.filters.pkpublish.PKPublishFilterRules.java

/**
 * Load the filter rules from a JSON-formatted string.
 * //w w  w.  j ava  2s  .  co m
 * @param rules_json JSON formatted string of the filter rules.
 * @throws PKPublishFilterException
 * @throws IOException
 * @throws JsonFilterException 
 * @throws JsonProcessingException
 */
public void loadRules(String rules_json) throws IOException, JsonFilterException {
    try {
        JsonNode rules_jn = new ObjectMapper().readTree(rules_json);
        confirmNodeType(rules_jn, JsonNodeType.OBJECT, "TransactionRules", logger);
        JsonNode transaction_filters_jn = fetchChildByName(rules_jn, "transaction_filters", "array");

        for (JsonNode tfilter_jn : transaction_filters_jn) {
            confirmNodeType(tfilter_jn, JsonNodeType.OBJECT, "TransactionFilter", logger);
            TransactionFilter tfilter = TransactionFilter.newFromJson(tfilter_jn);
            this.transaction_filters.add(tfilter);
            logger.debug("Added Transaction filter");
        }
    } catch (JsonProcessingException e) {
        throw new JsonFilterException(e);
    }
}

From source file:com.smartsheet.tin.filters.pkpublish.TransactionFilter.java

static public TransactionFilter newFromJson(JsonNode node) throws JsonFilterException, JsonProcessingException {
    confirmNodeType(node, JsonNodeType.OBJECT, "TransactionFilter", logger);

    TransactionFilter tf = new TransactionFilter();
    tf.setName(fetchChildString(node, "name", false));
    tf.setFilterMatchRule(fetchChildString(node, "filter_match_rule", false));
    tf.setRowMatchRule(fetchChildString(node, "row_match_rule", false));

    JsonNode row_filters_jn = fetchChildByName(node, "row_filters", "array");
    for (JsonNode row_filter_jn : row_filters_jn) {
        confirmNodeType(row_filter_jn, JsonNodeType.OBJECT, "row_filter", logger);
        RowFilter rf = RowFilter.newFromJson(row_filter_jn);
        tf.row_filters.add(rf);/*from   w w  w .ja  v  a2  s.  c  o m*/
    }

    JsonNode actions_jn = null;
    try {
        actions_jn = fetchChildByName(node, "actions", "array");
    } catch (JsonFilterChildNotFound e) {
        // Do nothing.
    }
    if (actions_jn != null) {
        for (JsonNode action_jn : actions_jn) {
            confirmNodeType(action_jn, JsonNodeType.OBJECT, "action", logger);
            String action_type = fetchChildString(action_jn, "type", true);
            if (action_type.equals("publish")) {
                PublishAction pa = PublishAction.newFromJson(action_jn);
                tf.setPublish(true);
                tf.setRoutingKey(pa.getRoutingKey());
                tf.setMessage(pa.getMessage());
                tf.actions.add(pa);
            } else {
                String err = String.format("Unknown action type: '%s', " + "in node: '%s'", action_type,
                        action_jn.toString());
                logger.error(err);
                throw new JsonFilterException(err);
            }
        }
    }
    return tf;
}