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:org.jboss.aerogear.sync.jsonmergepatch.client.JsonMergePatchClientSynchronizer.java

public static String checksum(final JsonNode content) {
    try {//from w ww .  java  2s .c om
        final MessageDigest md = MessageDigest.getInstance("SHA1");
        md.update(content.asText().getBytes(UTF_8));
        return new BigInteger(1, md.digest()).toString(16);
    } catch (final Exception e) {
        throw new RuntimeException(e.getMessage(), e.getCause());
    }
}

From source file:org.opendaylight.atrium.hostservice.impl.ConfigReader.java

public static Addresses getArpAddresses() {

    if (rootJsonNode == null) {
        return null;
    }/*from   w w  w  . j a  va  2s.c o  m*/

    JsonNode addressesJsonNode = rootJsonNode.path("addresses");
    Iterator<JsonNode> addressesNodeIterator = addressesJsonNode.elements();

    AddressesBuilder addressesBuilder = new AddressesBuilder();
    List<Address> addressList = new ArrayList<Address>();

    while (addressesNodeIterator.hasNext()) {
        AddressBuilder addressBuilder = new AddressBuilder();
        JsonNode addressElementNode = addressesNodeIterator.next();

        String hexDpid = addressElementNode.path("dpid").asText();
        String dpid = AtriumUtils.hexDpidStringToOpenFlowDpid(hexDpid);
        NodeConnectorId port = new NodeConnectorId(addressElementNode.path("port").asText());

        MacAddress mac = new MacAddress(addressElementNode.path("mac").asText());

        JsonNode ipsJsonNode = addressElementNode.path("ips");
        Iterator<JsonNode> ipsJsonNodeIterator = ipsJsonNode.elements();

        IpAddress ips = null;
        if (ipsJsonNodeIterator.hasNext()) {
            JsonNode ipJsonNode = ipsJsonNodeIterator.next();
            ips = new IpAddress(new Ipv4Address(ipJsonNode.asText().split("/")[0]));
        }

        int vlan = (int) addressElementNode.path("vlan").asLong();
        if (vlan == 0) {
            vlan = 1;
        }

        AddressKey addressKey = new AddressKey(ips);
        addressBuilder.setDpid(dpid);
        addressBuilder.setOfPortId(port);
        addressBuilder.setMac(mac);
        addressBuilder.setIpAddress(ips);
        addressBuilder.setVlan(new Integer(vlan));
        addressBuilder.setKey(addressKey);

        addressList.add(addressBuilder.build());
    }
    addressesBuilder.setAddress(addressList);
    return addressesBuilder.build();
}

From source file:org.apache.usergrid.android.sdk.utils.JsonUtils.java

public static String getStringProperty(Map<String, JsonNode> properties, String name) {
    JsonNode value = properties.get(name);
    if (value != null) {
        return value.asText();
    }/*ww w .  ja  v a 2 s  . c  om*/
    return null;
}

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

/**
 * Parses an n-ary relational expression from the given json object
 *///from   w  ww .  j  ava2 s  .  c  o m
public static NaryValueRelationalExpression fromJson(ObjectNode node) {
    if (node.size() == 3) {
        JsonNode x = node.get("op");
        if (x != null) {
            NaryRelationalOperator op = NaryRelationalOperator.fromString(x.asText());
            if (op != null) {
                x = node.get("field");
                if (x != null) {
                    Path field = new Path(x.asText());
                    x = node.get("values");
                    if (x instanceof ArrayNode) {
                        ArrayList<Value> values = new ArrayList<>(((ArrayNode) x).size());
                        for (Iterator<JsonNode> itr = ((ArrayNode) x).elements(); itr.hasNext();) {
                            values.add(Value.fromJson(itr.next()));
                        }
                        return new NaryValueRelationalExpression(field, op, values);
                    }
                }
            }
        }
    }
    throw Error.get(QueryConstants.ERR_INVALID_COMPARISON_EXPRESSION, node.toString());
}

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);/*from  w  w  w . j  a  v a  2  s . c  o  m*/
        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.redhat.lightblue.metadata.UIDFields.java

public static void initializeUIDFields(JsonNodeFactory factory, EntityMetadata md, JsonDoc doc) {
    FieldCursor cursor = md.getFieldCursor();
    while (cursor.next()) {
        FieldTreeNode node = cursor.getCurrentNode();
        // Process all UID fields
        if (node.getType().equals(UIDType.TYPE)) {
            Path p = cursor.getCurrentPath();
            LOGGER.debug("Processing UID field {}", p);
            if (node instanceof Field && required((Field) node)) {
                LOGGER.debug("Field {} is required", p);
                setRequiredField(factory, doc, p, 1, null);
            } else {
                // Here, node could be a field or an array
                LOGGER.debug("Field {} is not required", p);
                KeyValueCursor<Path, JsonNode> nodeCursor = doc.getAllNodes(p);
                while (nodeCursor.hasNext()) {
                    nodeCursor.next();// ww  w. j a v  a 2s .  co  m
                    JsonNode valueNode = nodeCursor.getCurrentValue();
                    if (valueNode.isNull() || valueNode.asText().length() == 0) {
                        String value = UIDType.newValue();
                        LOGGER.debug("Setting {} to {}", nodeCursor.getCurrentKey(), value);
                        doc.modify(nodeCursor.getCurrentKey(), factory.textNode(value), true);
                    }
                }
            }
        }
    }
}

From source file:models.Hit.java

private static String firstExisting() {
    for (String currentField : fields) {
        final String searchField = currentField /*@formatter:off*/
                .replace(GRAPH_KEY + ".", "").replace("." + ID_KEY, "")
                .replace("." + VALUE_KEY, ""); /*@formatter:on*/
        final JsonNode value = Json.toJson(hit.getSource()).findValue(searchField);
        if (value != null) {
            final JsonNode nestedId = value.findValue(ID_KEY);
            if (nestedId != null)
                return nestedId.asText();
            final JsonNode nestedValue = value.findValue(VALUE_KEY);
            if (nestedValue != null)
                return nestedValue.asText();
            if (!value.asText().trim().isEmpty())
                return value.asText();
        }//  www .  j  a  va 2 s .  c  om
    }
    if (fields.contains("_all"))
        return hit.getId();
    Logger.warn(String.format("Hit '%s' contains none of the fields: '%s'", hit.getSource(), fields));
    return null;
}

From source file:com.redhat.lightblue.metadata.UIDFields.java

private static void setRequiredField(JsonNodeFactory factory, JsonDoc doc, Path fieldPath, int startSegment,
        Path resolvedPath) {//from  w  w  w .  j  a va  2 s.c o m
    LOGGER.debug("setRequiredField: fieldPath:{} startSegment:{} resolvedPath:{}", fieldPath, startSegment,
            resolvedPath);
    int nSegments = fieldPath.numSegments();
    boolean array = false;
    for (int segment = startSegment; segment < nSegments; segment++) {
        if (fieldPath.head(segment).equals(Path.ANY)) {
            array = true;
            MutablePath arrPath = new MutablePath(fieldPath.prefix(segment));
            if (resolvedPath != null) {
                arrPath.rewriteIndexes(resolvedPath);
            }
            LOGGER.debug("Processing segment {}", arrPath);
            JsonNode node = doc.get(arrPath);
            if (node != null) {
                int size = node.size();
                LOGGER.debug("{} size={}", arrPath, size);
                arrPath.push(0);
                for (int i = 0; i < size; i++) {
                    arrPath.setLast(i);
                    setRequiredField(factory, doc, fieldPath, segment + 1, arrPath.immutableCopy());
                }
            }
            break;
        }
    }
    if (!array) {
        Path p;
        if (resolvedPath == null) {
            p = fieldPath;
        } else {
            p = new MutablePath(fieldPath).rewriteIndexes(resolvedPath);
        }
        LOGGER.debug("Setting {}", p);
        JsonNode valueNode = doc.get(p);
        if (valueNode == null || valueNode.isNull() || valueNode.asText().length() == 0) {
            String value = UIDType.newValue();
            LOGGER.debug("Setting {} to {}", p, value);
            doc.modify(p, factory.textNode(value), true);
        }
    }
}

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

/**
 * Parses a relational expression using the given object node
 *//*from w ww  .j a v  a 2 s  .  c  om*/
public static RelationalExpression fromJson(ObjectNode node) {
    JsonNode x = node.get("regex");
    if (x != null) {
        return RegexMatchExpression.fromJson(node);
    } else {
        x = node.get("op");
        if (x != null) {
            String op = x.asText();
            if (BinaryComparisonOperator.fromString(op) != null) {
                return BinaryRelationalExpression.fromJson(node);
            } else if (NaryRelationalOperator.fromString(op) != null) {
                return NaryRelationalExpression.fromJson(node);
            }
        }
    }
    throw Error.get(QueryConstants.ERR_INVALID_COMPARISON_EXPRESSION, node.toString());
}

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

public static boolean isXmlSchemaType(JsonNode type) {
    boolean isXsd = false;

    if (type != null && type.getNodeType() == JsonNodeType.STRING) {
        // very naive and simple for now
        isXsd = type.asText().trim().startsWith("xsd:");
    }/*  w  w  w . j a va 2 s  . c  o m*/

    return isXsd;
}