Example usage for com.fasterxml.jackson.databind JsonNode asText

List of usage examples for com.fasterxml.jackson.databind JsonNode asText

Introduction

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

Prototype

public abstract String asText();

Source Link

Usage

From source file:net.solarnetwork.node.weather.nz.metservice.MetserviceSupport.java

/**
 * Parse a FLoat from an attribute value.
 * //from   ww w  .j  a va 2  s.co  m
 * <p>
 * If the Float cannot be parsed, <em>null</em> will be returned.
 * </p>
 * 
 * @param key
 *        the attribute key to obtain from the {@code data} node
 * @param data
 *        the attributes
 * @return the parsed {@link Float}, or <em>null</em> if an error occurs or
 *         the specified attribute {@code key} is not available
 */
protected Float parseFloatAttribute(String key, JsonNode data) {
    Float num = null;
    if (data != null) {
        JsonNode node = data.get(key);
        if (node != null) {
            try {
                num = Float.valueOf(node.asText());
            } catch (NumberFormatException e) {
                log.debug("Error parsing float attribute [{}] value [{}]: {}",
                        new Object[] { key, data.get(key), e.getMessage() });
            }
        }
    }
    return num;
}

From source file:com.redhat.smonkey.RndDate.java

@Override
public JsonNode generate(JsonNodeFactory nodeFactory, JsonNode data, Monkey mon) {
    SimpleDateFormat fmt;/*from  w w  w  . j a v a  2  s .  co  m*/
    JsonNode formatNode = data.get("format");
    if (formatNode == null) {
        fmt = DEFAULT_FORMATTER;
    } else {
        fmt = new SimpleDateFormat(formatNode.asText());
    }

    try {
        String min = Utils.asString(data.get("min"), null);
        Date minDate = min == null ? null : fmt.parse(min);
        String max = Utils.asString(data.get("max"), null);
        Date maxDate = max == null ? null : fmt.parse(max);
        if (minDate == null && maxDate == null) {
            String base = Utils.asString(data.get("base"), fmt.format(new Date()));
            Date baseDate = fmt.parse(base);

            String fwd = Utils.asString(data.get("fwd"), null);
            String bck = Utils.asString(data.get("bck"), null);
            if (fwd == null && bck == null)
                return nodeFactory.textNode(base);

            if (fwd != null)
                maxDate = applyDiff(baseDate, fwd, false);
            if (bck != null)
                minDate = applyDiff(baseDate, bck, true);
        }
        return nodeFactory.textNode(fmt.format(genDate(minDate, maxDate)));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:net.solarnetwork.node.weather.nz.metservice.MetserviceSupport.java

/**
 * Parse a Double from an attribute value.
 * //  ww  w.  j a  v a2  s.  c  om
 * <p>
 * If the Double cannot be parsed, <em>null</em> will be returned.
 * </p>
 * 
 * @param key
 *        the attribute key to obtain from the {@code data} Map
 * @param data
 *        the attributes
 * @return the parsed {@link Double}, or <em>null</em> if an error occurs or
 *         the specified attribute {@code key} is not available
 */
protected Double parseDoubleAttribute(String key, JsonNode data) {
    Double num = null;
    if (data != null) {
        JsonNode node = data.get(key);
        if (node != null) {
            try {
                num = Double.valueOf(node.asText());
            } catch (NumberFormatException e) {
                log.debug("Error parsing double attribute [{}] value [{}]: {}",
                        new Object[] { key, data.get(key), e.getMessage() });
            }
        }
    }
    return num;
}

From source file:io.fabric8.kubernetes.api.KubernetesHelper.java

protected static Config loadList(byte[] data) throws IOException {
    Config config = new Config();
    List<Object> itemList = new ArrayList<>();
    config.setItems(itemList);/*www  .ja  v a  2s  .c  om*/
    JsonNode jsonNode = objectMapper.readTree(data);
    JsonNode items = jsonNode.get("items");
    for (JsonNode item : items) {
        if (item != null) {
            JsonNode kindObject = item.get("kind");
            if (kindObject != null && kindObject.isTextual()) {
                String kind = kindObject.asText();
                String json = toJson(item);
                byte[] bytes = json.getBytes();
                Object entity = loadEntity(bytes, kind, null);
                if (entity != null) {
                    itemList.add(entity);
                }
            }
        }
    }
    return config;
}

From source file:net.solarnetwork.node.weather.nz.metservice.MetserviceSupport.java

/**
 * Parse a Integer from an attribute value.
 * //from  w  ww  .java2s . co  m
 * <p>
 * If the Integer cannot be parsed, <em>null</em> will be returned.
 * </p>
 * 
 * @param key
 *        the attribute key to obtain from the {@code data} node
 * @param data
 *        the attributes
 * @return the parsed {@link Integer}, or <em>null</em> if an error occurs
 *         or the specified attribute {@code key} is not available
 */
protected Integer parseIntegerAttribute(String key, JsonNode data) {
    Integer num = null;
    if (data != null) {
        JsonNode node = data.get(key);
        if (node != null) {
            try {
                num = Integer.valueOf(node.asText());
            } catch (NumberFormatException e) {
                log.debug("Error parsing integer attribute [{}] value [{}]: {}",
                        new Object[] { key, data.get(key), e.getMessage() });
            }
        }
    }
    return num;
}

From source file:com.ocdsoft.bacta.soe.json.schema.SoeDefaultRule.java

/**
 * Applies this schema rule to take the required code generation steps.
 * <p>//  w w  w .j  av a 2 s.co  m
 * Default values are implemented by assigning an expression to the given
 * field (so when instances of the generated POJO are created, its fields
 * will then contain their default values).
 * <p>
 * Collections (Lists and Sets) are initialized to an empty collection, even
 * when no default value is present in the schema (node is null).
 *
 * @param nodeName
 *            the name of the property which has (or may have) a default
 * @param node
 *            the default node (may be null if no default node was present
 *            for this property)
 * @param field
 *            the Java field that has added to a generated type to represent
 *            this property
 * @return field, which will have an init expression is appropriate
 */
@Override
public JFieldVar apply(String nodeName, JsonNode node, JFieldVar field, Schema currentSchema) {

    boolean defaultPresent = node != null && isNotEmpty(node.asText());

    String fieldType = field.type().fullName();

    if (defaultPresent && !field.type().isPrimitive() && node.isNull()) {
        field.init(JExpr._null());

    } else if (fieldType.startsWith(List.class.getName())) {
        field.init(getDefaultList(field.type(), node));

    } else if (fieldType.startsWith(Set.class.getName())) {
        field.init(getDefaultSet(field.type(), node));

    } else if (defaultPresent) {
        field.init(getDefaultValue(field.type(), node));

    }

    return field;
}

From source file:com.facebook.api.FacebookPostActivitySerializer.java

private void addTitle(Activity activity, JsonNode value) {
    activity.setTitle(value.asText());
}

From source file:com.redhat.smonkey.Monkey.java

private JsonNode attemptGenerateValue(JsonNode templateNode) {
    String fname = null;//from w w  w . ja va2 s  . c  om
    if (templateNode.size() == 1) {
        fname = templateNode.fieldNames().next();
    } else if (!templateNode.isContainerNode()) {
        fname = templateNode.asText();
    }
    // Check if this is an escaped name
    if (fname != null && !fname.startsWith("\\")) {
        JsonNode dataNode = templateNode.get(fname);
        if (dataNode == null)
            dataNode = templateNode;
        JsonNode value = generateValue(fname, dataNode);
        if (value != null)
            return value;
    }
    return null;
}

From source file:jsonbrowse.JsonBrowse.java

/**
 * Builds a tree of TreeNode objects using the tree under the
 * given JsonNode.//from  ww  w .  j a  v a 2 s  . c om
 * 
 * @param name Text to be associated with node
 * @param node
 * @return root TreeNode
 */
private DefaultMutableTreeNode buildTree(String name, JsonNode node) {
    DefaultMutableTreeNode treeNode = new DefaultMutableTreeNode(name);

    Iterator<Entry<String, JsonNode>> it = node.fields();
    while (it.hasNext()) {
        Entry<String, JsonNode> entry = it.next();
        treeNode.add(buildTree(entry.getKey(), entry.getValue()));
    }

    if (node.isArray()) {
        for (int i = 0; i < node.size(); i++) {
            JsonNode child = node.get(i);
            if (child.isValueNode())
                treeNode.add(new DefaultMutableTreeNode(child.asText()));
            else
                treeNode.add(buildTree(String.format("[%d]", i), child));
        }
    } else if (node.isValueNode()) {
        treeNode.add(new DefaultMutableTreeNode(node.asText()));
    }

    return treeNode;
}

From source file:org.apache.logging.log4j.core.config.EDNConfiguration.java

private String getType(final JsonNode node, final String name) {
    final Iterator<Map.Entry<String, JsonNode>> iter = node.fields();
    while (iter.hasNext()) {
        final Map.Entry<String, JsonNode> entry = iter.next();
        if (entry.getKey().equalsIgnoreCase("type")) {
            final JsonNode n = entry.getValue();
            if (n.isValueNode()) {
                return n.asText();
            }/*from   w  ww .j  a va 2 s  . c o  m*/
        }
    }
    return name;
}