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

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

Introduction

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

Prototype

public String textValue() 

Source Link

Usage

From source file:com.github.fge.jsonschema.format.AbstractFormatAttributeTest.java

private static Object valueToArgument(final JsonNode value) {
    final NodeType type = NodeType.getNodeType(value);

    switch (type) {
    case STRING://from  w w  w  .  j a  v a2s  . c o  m
        return value.textValue();
    case INTEGER:
        return value.bigIntegerValue();
    case NUMBER:
        return value.decimalValue().toPlainString();
    case NULL:
        return value;
    case BOOLEAN:
        return value.booleanValue();
    case ARRAY:
        final List<Object> list = Lists.newArrayList();
        for (final JsonNode element : value)
            list.add(valueToArgument(element));
        return list;
    default:
        throw new UnsupportedOperationException();
    }
}

From source file:com.github.fge.jsonschema.keyword.validator.AbstractKeywordValidatorTest.java

private static Object valueToArgument(final JsonNode value) {
    final NodeType type = NodeType.getNodeType(value);

    switch (type) {
    case STRING:/*from w ww  .j  a  v a 2 s. com*/
        return value.textValue();
    case INTEGER:
        return value.bigIntegerValue();
    case NUMBER:
    case NULL:
    case OBJECT:
    case ARRAY:
        return value;
    case BOOLEAN:
        return value.booleanValue();
    //            case ARRAY:
    //                final List<Object> list = Lists.newArrayList();
    //                for (final JsonNode element: value)
    //                    list.add(valueToArgument(element));
    //                return list;
    default:
        throw new UnsupportedOperationException();
    }
}

From source file:com.palominolabs.testutil.JsonAssert.java

private static void assertJsonNodeEquals(String msg, JsonNode expected, JsonNode actual) {
    if (expected.isTextual()) {
        if (actual.isTextual()) {
            assertEquals(msg, expected.textValue(), actual.textValue());
        } else {/*from   w w w .  j a va  2s .  c o m*/
            nonMatchingClasses(msg, expected, actual);
        }
    } else if (expected.isInt()) {
        if (actual.isInt()) {
            assertEquals(msg, expected.intValue(), actual.intValue());
        } else {
            nonMatchingClasses(msg, expected, actual);
        }
    } else if (expected.isObject()) {
        if (actual.isObject()) {
            assertJsonObjectEquals(msg, (ObjectNode) expected, (ObjectNode) actual);
        } else {
            nonMatchingClasses(msg, expected, actual);
        }
    } else if (expected.isArray()) {
        if (actual.isArray()) {
            assertJsonArrayEquals(msg, (ArrayNode) expected, (ArrayNode) actual);
        } else {
            nonMatchingClasses(msg, expected, actual);
        }
    } else if (expected == NullNode.getInstance()) {
        if (actual == NullNode.getInstance()) {
            return;
        }
        nonMatchingClasses(msg, expected, actual);
    } else if (expected.isBoolean()) {
        if (actual.isBoolean()) {
            assertEquals(msg, expected.booleanValue(), actual.booleanValue());
        } else {
            nonMatchingClasses(msg, expected, actual);
        }
    } else {
        fail(msg + "/Can only handle recursive Object, Array and Null instances, got a " + expected.getClass()
                + ": " + expected);
    }
}

From source file:com.github.fge.jsonschema.core.keyword.syntax.checkers.SyntaxCheckersTest.java

private static String buildMessage(final String key, final JsonNode params, final JsonNode data) {
    final ProcessingMessage message = new ProcessingMessage().setMessage(BUNDLE.getMessage(key));
    if (params != null) {
        String name;/*from www  .j  a  v a  2  s . co  m*/
        JsonNode value;
        for (final JsonNode node : params) {
            name = node.textValue();
            value = data.get(name);
            message.putArgument(name, valueToArgument(value));
        }
    }
    return message.getMessage();
}

From source file:com.jivesoftware.os.amza.service.AmzaGetStress.java

private static void get(org.apache.http.client.HttpClient httpClient, String hostName, int port,
        String partitionName, int firstDocId, int count, int batchSize)
        throws IOException, InterruptedException {
    long start = System.currentTimeMillis();
    for (int key = firstDocId; key < count; key++) {
        StringBuilder url = new StringBuilder();
        url.append("http://");
        url.append(hostName).append(":").append(port);
        url.append("/amza/get");
        url.append("?partition=").append(partitionName);
        url.append("&key=");

        Set<String> expectedValues = Sets.newHashSet();
        for (int b = 0; b < batchSize; b++) {
            if (b > 0) {
                url.append(',');
            }//from  www .  jav a2s.  c  o m
            url.append(b).append('k').append(key);
            expectedValues.add(b + "v" + key);
        }

        while (true) {
            HttpGet method = new HttpGet(url.toString());
            StatusLine statusLine;
            try {
                try {
                    HttpResponse response = httpClient.execute(method);

                    statusLine = response.getStatusLine();
                    if (statusLine.getStatusCode() == 200) {
                        //System.out.println("Got:" + new String(method.getResponseBody()));
                        ArrayNode node = mapper.readValue(EntityUtils.toString(response.getEntity()),
                                ArrayNode.class);
                        for (JsonNode value : node) {
                            if (!value.isNull()) {
                                expectedValues.remove(new String(
                                        BaseEncoding.base64().decode(value.textValue()), Charsets.UTF_8));
                            }
                        }
                        if (!expectedValues.isEmpty()) {
                            System.out.println("Missing values in " + partitionName + " for key " + key + ": "
                                    + expectedValues);
                        }
                        break;
                    }
                } catch (Exception x) {
                    x.printStackTrace();
                }
                Thread.sleep(1000);
            } finally {
                method.releaseConnection();
            }
        }

        if (key % 100 == 0) {
            long elapse = System.currentTimeMillis() - start;
            double millisPerAdd = ((double) elapse / (double) key);
            System.out.println(partitionName + " millisPerGet:" + millisPerAdd + " getsPerSec:"
                    + (1000d / millisPerAdd) + " key:" + key);
        }
    }
}

From source file:com.github.fge.jsonschema2avro.predicates.AvroPredicates.java

public static Predicate<AvroPayload> isEnum() {
    return new Predicate<AvroPayload>() {
        @Override/*w  w  w  .  j  a v a2  s  .  c  o m*/
        public boolean apply(final AvroPayload input) {
            final JsonNode node = schemaNode(input);
            final Set<String> set = Sets.newHashSet(node.fieldNames());
            set.retainAll(KNOWN_KEYWORDS);
            if (!set.equals(ImmutableSet.of("enum")))
                return false;

            // Test individual entries: they must be strings, and must be
            // the same "shape" as any Avro name
            for (final JsonNode element : node.get("enum")) {
                if (!element.isTextual())
                    return false;
                if (!isValidAvroName(element.textValue()))
                    return false;
            }

            return true;
        }
    };
}

From source file:org.eel.kitchen.jsonschema.ref.JsonRef.java

/**
 * Build a JSON Reference from a {@link JsonNode}
 *
 * <p>If the node is not textual, this returns an empty reference.
 * Otherwise, it calls {@link #fromString(String)} with this node's text
 * value.</p>//from   w  w  w  . jav  a 2 s . co  m
 *
 * @param node the node
 * @return the reference
 * @throws JsonSchemaException see {@link #fromString(String)}
 * @throws NullPointerException provided node is null
 */
public static JsonRef fromNode(final JsonNode node) throws JsonSchemaException {
    Preconditions.checkNotNull(node, "node must not be null");

    return node.isTextual() ? fromString(node.textValue()) : EmptyJsonRef.getInstance();
}

From source file:com.github.fge.jsonschema.core.keyword.syntax.checkers.SyntaxCheckersTest.java

private static Object valueToArgument(final JsonNode value) {
    final NodeType type = NodeType.getNodeType(value);

    switch (type) {
    case STRING://from  www  . ja  v a2  s .c  o  m
        return value.textValue();
    case INTEGER:
        return value.bigIntegerValue();
    case NUMBER:
    case NULL:
        return value;
    case BOOLEAN:
        return value.booleanValue();
    case ARRAY:
        final List<Object> list = Lists.newArrayList();
        for (final JsonNode element : value)
            list.add(valueToArgument(element));
        return list;
    default:
        throw new UnsupportedOperationException();
    }
}

From source file:org.primaldev.ppm.util.ChartTypeGenerator.java

public static ChartTypeComponent generateChart(byte[] reportData) {

    // Convert json to pojo
    JsonNode jsonNode = convert(reportData);

    // Title//from  w  w w  .j  a v a 2  s.c o m
    JsonNode titleNode = jsonNode.get("title");
    String title = null;
    if (titleNode != null) {
        title = titleNode.textValue();
    }

    ChartTypeComponent chartComponent = new ChartTypeComponent(title);

    // Retrieve data sets
    JsonNode datasetsNode = jsonNode.get("datasets");

    // If no data was returned
    if (datasetsNode.size() == 0) {
        chartComponent.addChart(null, null, "Not enough data");
        return chartComponent;
    }

    if (datasetsNode != null && datasetsNode.isArray()) {

        Iterator<JsonNode> dataIterator = datasetsNode.iterator();
        while (dataIterator.hasNext()) {

            JsonNode datasetNode = dataIterator.next();

            JsonNode descriptionNode = datasetNode.get("description");
            String description = null;
            if (descriptionNode != null) {
                description = descriptionNode.textValue();
            }
            JsonNode dataNode = datasetNode.get("data");

            if (dataNode == null || dataNode.size() == 0) {
                chartComponent.addChart(description, null, "Not enough data");
            } else {

                String[] names = new String[dataNode.size()];
                Number[] values = new Number[dataNode.size()];

                int index = 0;
                Iterator<String> fieldIterator = dataNode.fieldNames();
                while (fieldIterator.hasNext()) {
                    String field = fieldIterator.next();
                    names[index] = field;
                    values[index] = dataNode.get(field).numberValue();
                    index++;
                }

                // Generate chart (or 'no data' message)
                if (names.length > 0) {
                    Component chart = createChart(datasetNode, names, values);
                    chartComponent.addChart(description, chart, null);
                } else {
                    chartComponent.addChart(description, null, "Not enough data");
                }

            }

        }

    }

    return chartComponent;
}

From source file:net.hamnaberg.json.ValueFactory.java

public static Value createValue(JsonNode node) {
    if (node == null) {
        throw new IllegalArgumentException("Node may not be null");
    } else if (node.isNumber()) {
        return new ValueImpl(node.decimalValue());
    } else if (node.isBoolean()) {
        return new ValueImpl(node.booleanValue());
    } else if (node.isTextual()) {
        return new ValueImpl(node.textValue());
    } else if (node.isNull()) {
        return ValueImpl.NULL;
    }//from  w  w w .j  a  v a 2 s .co m
    throw new IllegalArgumentException("Illegal value " + node);
}