Example usage for com.fasterxml.jackson.databind.node ValueNode asText

List of usage examples for com.fasterxml.jackson.databind.node ValueNode asText

Introduction

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

Prototype

public abstract String asText();

Source Link

Usage

From source file:com.hpcloud.util.config.Configurations.java

private static void buildConfigFor(String path, Map<String, String> config, JsonNode node) {
    for (Iterator<Map.Entry<String, JsonNode>> i = node.fields(); i.hasNext();) {
        Map.Entry<String, JsonNode> field = i.next();
        if (field.getValue() instanceof ValueNode) {
            ValueNode valueNode = (ValueNode) field.getValue();
            config.put(DOT_JOINER.join(path, field.getKey()), valueNode.asText());
        } else if (field.getValue() instanceof ArrayNode) {
            StringBuilder combinedValue = new StringBuilder();
            ArrayNode arrayNode = (ArrayNode) field.getValue();
            for (Iterator<JsonNode> it = arrayNode.elements(); it.hasNext();) {
                String value = it.next().asText().replaceAll("^\"|\"$", "");
                if (combinedValue.length() > 0)
                    combinedValue.append(',');
                combinedValue.append(value);
            }/*w w  w  . j a  v  a  2s . c  o m*/

            config.put(DOT_JOINER.join(path, field.getKey()), combinedValue.toString());
        }

        buildConfigFor(DOT_JOINER.join(path, field.getKey()), config, field.getValue());
    }
}

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

private static void addKeys(String currentPath, JsonNode jsonNode, Map<String, Object> map, char seperator) {
    if (jsonNode.isObject()) {
        ObjectNode objectNode = (ObjectNode) jsonNode;
        Iterator<Map.Entry<String, JsonNode>> iter = objectNode.fields();
        String pathPrefix = currentPath.isEmpty() ? "" : currentPath + seperator;

        while (iter.hasNext()) {
            Map.Entry<String, JsonNode> entry = iter.next();
            addKeys(pathPrefix + entry.getKey(), entry.getValue(), map, seperator);
        }//from   w  ww.j  av  a  2  s  .c  o  m
    } else if (jsonNode.isArray()) {
        ArrayNode arrayNode = (ArrayNode) jsonNode;
        map.put(currentPath, arrayNode);
    } else if (jsonNode.isValueNode()) {
        ValueNode valueNode = (ValueNode) jsonNode;
        if (valueNode.isTextual())
            map.put(currentPath, valueNode.asText());
        else if (valueNode.isNumber())
            map.put(currentPath, valueNode);
    }
}

From source file:com.mapr.data.sputnik.util.JsonUtils.java

public void flattenJsonIntoMap(String currentPath, JsonNode jsonNode, Map<String, Object> map) {
    if (jsonNode.isObject()) {
        ObjectNode objectNode = (ObjectNode) jsonNode;
        Iterator<Map.Entry<String, JsonNode>> iter = objectNode.fields();
        String pathPrefix = currentPath.isEmpty() ? "" : currentPath + ".";

        while (iter.hasNext()) {
            Map.Entry<String, JsonNode> entry = iter.next();
            flattenJsonIntoMap(pathPrefix + entry.getKey(), entry.getValue(), map);
        }/*from w w  w  .  jav  a 2s.  com*/
    } else if (jsonNode.isArray()) {
        ArrayNode arrayNode = (ArrayNode) jsonNode;
        for (int i = 0; i < arrayNode.size(); i++) {
            flattenJsonIntoMap(currentPath + "[" + i + "]", arrayNode.get(i), map);
        }
    } else if (jsonNode.isValueNode()) {
        ValueNode valueNode = (ValueNode) jsonNode;
        Object value = null;
        if (valueNode.isNumber()) {
            value = valueNode.numberValue();
        } else if (valueNode.isBoolean()) {
            value = valueNode.asBoolean();
        } else if (valueNode.isTextual()) {
            value = valueNode.asText();
        }
        map.put(currentPath, value);
    }
}

From source file:org.apache.streams.util.PropertyUtil.java

private static void addKeys(String currentPath, JsonNode jsonNode, Map<String, Object> map, char seperator) {
    if (jsonNode.isObject()) {
        ObjectNode objectNode = (ObjectNode) jsonNode;
        Iterator<Map.Entry<String, JsonNode>> iter = objectNode.fields();
        String pathPrefix = currentPath.isEmpty() ? "" : currentPath + seperator;

        while (iter.hasNext()) {
            Map.Entry<String, JsonNode> entry = iter.next();
            addKeys(pathPrefix + entry.getKey(), entry.getValue(), map, seperator);
        }/*from  w w  w .ja  va  2s . co  m*/
    } else if (jsonNode.isArray()) {
        ArrayNode arrayNode = (ArrayNode) jsonNode;
        if (arrayNode.isTextual()) {
            List<String> list = mapper.convertValue(arrayNode, List.class);
            map.put(currentPath, list);
        }
        if (arrayNode.isNumber()) {
            List<String> list = mapper.convertValue(arrayNode, List.class);
            map.put(currentPath, list);
        }
    } else if (jsonNode.isValueNode()) {
        ValueNode valueNode = (ValueNode) jsonNode;
        if (valueNode.isTextual())
            map.put(currentPath, valueNode.asText());
        else if (valueNode.isNumber())
            map.put(currentPath, valueNode);
    }
}