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:la.alsocan.jsonshapeshifter.transformations.SimpleCollectionTransformationTest.java

@Test
public void collectionBindingShouldWorkWithBindingOnNonCollectionNodes() throws IOException {

    Schema source = Schema.buildSchema(new ObjectMapper().readTree(DataSet.SIMPLE_COLLECTION_SCHEMA));
    Schema target = Schema.buildSchema(new ObjectMapper().readTree(DataSet.SIMPLE_COLLECTION_SCHEMA));
    Transformation t = new Transformation(source, target);

    Iterator<SchemaNode> it = t.toBind();
    it.next();/*w ww . j  a v  a 2  s .co m*/
    t.bind(it.next(), new ArrayNodeBinding((SchemaArrayNode) source.at("/someStringArray")));
    t.bind(it.next(), new StringNodeBinding(source.at("/someString")));
    t.bind(it.next(), new ArrayNodeBinding((SchemaArrayNode) source.at("/someIntegerArray")));
    t.bind(it.next(), new IntegerConstantBinding(12));

    JsonNode payload = new ObjectMapper().readTree(DataSet.SIMPLE_COLLECTION_PAYLOAD);
    JsonNode result = t.apply(payload);

    for (JsonNode node : (ArrayNode) result.at("/someStringArray")) {
        assertThat(node.asText(), is(equalTo("string1")));
    }

    for (JsonNode node : (ArrayNode) result.at("/someIntegerArray")) {
        assertThat(node.asInt(), is(equalTo(12)));
    }
}

From source file:com.networknt.schema.BaseJsonValidator.java

protected void parseErrorCode(String errorCodeKey) {
    JsonNode errorCodeNode = getParentSchema().getSchemaNode().get(errorCodeKey);
    if (errorCodeNode != null && errorCodeNode.isTextual()) {
        errorCode = errorCodeNode.asText();
    }/*from   w  w w .j  a va2 s  .co  m*/
}

From source file:com.ikanow.aleph2.security.service.IkanowV1DataModificationChecker.java

protected Date queryLastModifiedDate(Boolean wasIndexed) {
    if (wasIndexed) {

        SingleQueryComponent<JsonNode> query = CrudUtils.allOf()
                .orderBy(new Tuple2<String, Integer>("modified", -1)).limit(1);
        CompletableFuture<Cursor<JsonNode>> fCursor = getCommunityDb().getObjectsBySpec(query,
                Arrays.asList("modified"), true);
        try {/*from w w  w.  j  a  va  2 s . c  om*/
            Iterator<JsonNode> it = fCursor.get().iterator();
            if (it.hasNext()) {
                JsonNode n = it.next();
                logger.debug(n);
                JsonNode modifiedNode = n.get("modified");
                if (modifiedNode != null) {
                    String modifiedText = modifiedNode.asText();
                    // logger.debug(modifiedText);
                    // example Thu Aug 13 15:44:08 CDT 2015
                    SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
                    lastModified = sdf.parse(modifiedText);
                    // logger.debug(lastModified);
                }
            }
        } catch (Throwable e) {
            logger.error("queryLastModifiedDate caught Exception", e);
        }
    }
    return null;
}

From source file:la.alsocan.jsonshapeshifter.transformations.SimpleCollectionTransformationTest.java

@Test
public void collectionBindingShouldProduceTheRightValues() throws IOException {

    Schema source = Schema.buildSchema(new ObjectMapper().readTree(DataSet.SIMPLE_COLLECTION_SCHEMA));
    Schema target = Schema.buildSchema(new ObjectMapper().readTree(DataSet.SIMPLE_COLLECTION_SCHEMA));
    Transformation t = new Transformation(source, target);

    Iterator<SchemaNode> it = t.toBind();
    it.next();/*from  w w w.j  a  va  2s  .co  m*/
    t.bind(it.next(), new ArrayNodeBinding((SchemaArrayNode) source.at("/someStringArray")));
    t.bind(it.next(), new StringNodeBinding(source.at("/someStringArray/{i}")));
    t.bind(it.next(), new ArrayNodeBinding((SchemaArrayNode) source.at("/someIntegerArray")));
    t.bind(it.next(), new IntegerNodeBinding(source.at("/someIntegerArray/{i}")));

    JsonNode payload = new ObjectMapper().readTree(DataSet.SIMPLE_COLLECTION_PAYLOAD);
    JsonNode result = t.apply(payload);

    int index = 0;
    String[] expectedStrings = new String[] { "a", "b", "c", "d", "e" };
    for (JsonNode node : (ArrayNode) result.at("/someStringArray")) {
        assertThat(node.asText(), is(equalTo(expectedStrings[index++])));
    }

    index = 0;
    Integer[] expectedIntegers = new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8 };
    for (JsonNode node : (ArrayNode) result.at("/someIntegerArray")) {
        assertThat(node.asInt(), is(equalTo(expectedIntegers[index++])));
    }
}

From source file:com.redhat.lightblue.metadata.types.IntegerTypeTest.java

@Test
public void testToJson() {
    JsonNodeFactory jsonNodeFactory = new JsonNodeFactory(true);
    JsonNode jsonNode = integerType.toJson(jsonNodeFactory, Integer.MAX_VALUE);
    assertTrue(new Integer(jsonNode.asText()).equals(Integer.MAX_VALUE));
}

From source file:com.googlecode.jsonrpc4j.JsonRpcMultiServer.java

/**
 * Get the service name from the methodNode.  JSON-RPC methods with the form
 * Service.method will result in "Service" being returned in this case.
 *
 * @param methodNode the JsonNode for the method
 * @return the name of the service, or <code>null</code>
 *//* w w  w  . ja va  2 s. c  o  m*/
@Override
protected String getServiceName(JsonNode methodNode) {
    String methodName = (methodNode != null && !methodNode.isNull()) ? methodNode.asText() : null;
    if (methodName != null) {
        int ndx = methodName.indexOf(this.separator);
        if (ndx > 0) {
            return methodName.substring(0, ndx);
        }
    }
    return methodName;
}

From source file:com.googlecode.jsonrpc4j.JsonRpcMultiServer.java

/**
 * Get the method name from the methodNode, stripping off the service name.
 *
 * @param methodNode the JsonNode for the method
 * @return the name of the method that should be invoked
 *//*  www . j  a  va  2  s. c  om*/
@Override
protected String getMethodName(JsonNode methodNode) {
    String methodName = (methodNode != null && !methodNode.isNull()) ? methodNode.asText() : null;
    if (methodName != null) {
        int ndx = methodName.indexOf(this.separator);
        if (ndx > 0) {
            return methodName.substring(ndx + 1);
        }
    }
    return methodName;
}

From source file:com.ikanow.aleph2.graph.titan.utils.TitanGraphBuildingUtils.java

/** Separates out edges/vertices, groups by key
 * @param config//from   www . ja  v a 2  s.  c  o  m
 * @param vertices_and_edges
 * @return
 */
protected static Map<ObjectNode, Tuple2<List<ObjectNode>, List<ObjectNode>>> groupNewEdgesAndVertices(
        final GraphSchemaBean config, final MutableStatsBean stats,
        final Stream<ObjectNode> vertices_and_edges) {
    final Map<ObjectNode, Tuple2<List<ObjectNode>, List<ObjectNode>>> nodes_to_get = vertices_and_edges
            .filter(o -> o.has(GraphAnnotationBean.type))
            .<Tuple3<ObjectNode, ObjectNode, Boolean>>flatMap(o -> {
                final JsonNode type = o.get(GraphAnnotationBean.type);

                if ((null == type) || !type.isTextual())
                    return Stream.empty();
                if (GraphAnnotationBean.ElementType.edge.toString().equals(type.asText())) {
                    stats.edges_emitted++;
                    // Grab both edges from both ends:
                    return Stream.concat(
                            Optional.ofNullable(o.get(GraphAnnotationBean.inV))
                                    .map(k -> Stream.of(Tuples._3T(convertToObject(k, config), o, false)))
                                    .orElse(Stream.empty()),
                            Optional.ofNullable(o.get(GraphAnnotationBean.outV))
                                    .map(k -> Stream.of(Tuples._3T(convertToObject(k, config), o, false)))
                                    .orElse(Stream.empty()));
                } else if (GraphAnnotationBean.ElementType.vertex.toString().equals(type.asText())) {
                    stats.vertices_emitted++;
                    return Optional.ofNullable(o.get(GraphAnnotationBean.id))
                            .map(k -> Stream.of(Tuples._3T(convertToObject(k, config), o, true)))
                            .orElse(Stream.empty());
                } else
                    return Stream.empty();
            }).collect(Collectors.groupingBy(t3 -> t3._1() // group by key
                    , Collectors.collectingAndThen(
                            Collectors.<Tuple3<ObjectNode, ObjectNode, Boolean>>partitioningBy(t3 -> t3._3()) // group by edge/vertex
                            ,
                            m -> Tuples._2T(m.get(true).stream().map(t3 -> t3._2()).collect(Collectors.toList()) // convert group edge/vertex to pair of lists
                                    , m.get(false).stream().map(t3 -> t3._2()).collect(Collectors.toList())))));

    return nodes_to_get;
}

From source file:com.galenframework.tests.integration.GalenFullJsProjectIT.java

private List<String> collectAllErrorMessagesFrom(JsonNode jsonNode) {
    List<String> list = new LinkedList<>();

    if (jsonNode.isArray()) {
        for (JsonNode aJsonNode : jsonNode) {
            list.addAll(collectAllErrorMessagesFrom(aJsonNode));
        }/*www .j a v  a2  s. com*/
    } else {
        Iterator<String> fieldsIterator = jsonNode.fieldNames();
        while (fieldsIterator.hasNext()) {
            String fieldName = fieldsIterator.next();
            if ("errors".equals(fieldName)) {
                JsonNode errorsNode = jsonNode.get(fieldName);
                if (errorsNode.isArray()) {
                    for (JsonNode errorNode : errorsNode) {
                        list.add(errorNode.asText());
                    }
                }
            } else {
                list.addAll(collectAllErrorMessagesFrom(jsonNode.get(fieldName)));
            }
        }
    }
    return list;
}

From source file:com.digitalpebble.storm.crawler.parse.filter.XPathFilter.java

private void addExpression(String key, JsonNode expression) {
    String xpathvalue = expression.asText();
    try {/*ww w.  j av a2 s  .c o m*/
        List<LabelledExpression> lexpressionList = expressions.get(key);
        if (lexpressionList == null) {
            lexpressionList = new ArrayList<>();
            expressions.put(key, lexpressionList);
        }
        LabelledExpression lexpression = new LabelledExpression(key, xpathvalue);
        lexpressionList.add(lexpression);
    } catch (XPathExpressionException e) {
        throw new RuntimeException("Can't compile expression : " + xpathvalue, e);
    }
}