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:eu.eubrazilcc.lvl.core.GeoJsonBindingTest.java

@Test
public void test() {
    System.out.println("GeoJsonBindingTest.test()");
    try {//from   w  ww .  ja v a  2 s.c o m
        // test GeoJSON point JSON binding
        final Point point = Point.builder().coordinates(LngLatAlt.builder().coordinates(1.0d, 2.0d).build())
                .build();
        assertThat("point is not null", point, notNullValue());
        String payload = JSON_MAPPER.writeValueAsString(point);
        assertThat("point payload is not null", payload, notNullValue());
        assertThat("point payload is not empty", isNotBlank(payload));
        final Point point2 = JSON_MAPPER.readValue(payload, Point.class);
        assertThat("point back is not null", point2, notNullValue());
        assertThat("point back coincides with original", point2, equalTo(point));
        System.out.println("point: " + point.toString());
        System.out.println("point JSON payload: " + payload);

        // test GeoJSON line JSON binding
        final LineString line = LineString.builder()
                .coordinates(LngLatAlt.builder().coordinates(1.0d, 2.0d).build(),
                        LngLatAlt.builder().coordinates(3.0d, 4.0d).build())
                .build();
        payload = JSON_MAPPER.writeValueAsString(line);
        assertThat("line payload is not null", payload, notNullValue());
        assertThat("line payload is not empty", isNotBlank(payload));
        final LineString line2 = JSON_MAPPER.readValue(payload, LineString.class);
        assertThat("line back is not null", line2, notNullValue());
        assertThat("line back coincides with original", line2, equalTo(line));
        System.out.println("line: " + line.toString());
        System.out.println("line JSON payload: " + payload);

        // test GeoJSON polygon JSON binding
        final Polygon polygon = Polygon.builder()
                .exteriorRing(LngLatAlt.builder().coordinates(1.0d, 1.0d).build(),
                        LngLatAlt.builder().coordinates(2.0d, 2.0d).build(),
                        LngLatAlt.builder().coordinates(3.0d, 3.0d).build(),
                        LngLatAlt.builder().coordinates(1.0d, 1.0d).build())
                .build();
        payload = JSON_MAPPER.writeValueAsString(polygon);
        assertThat("polygon payload is not null", payload, notNullValue());
        assertThat("polygon payload is not empty", isNotBlank(payload));
        final Polygon polygon2 = JSON_MAPPER.readValue(payload, Polygon.class);
        assertThat("polygon back is not null", polygon2, notNullValue());
        assertThat("polygon back coincides with original", polygon2, equalTo(polygon));
        System.out.println("polygon: " + polygon.toString());
        System.out.println("polygon JSON payload: " + payload);

        // test GeoJSON feature collection JSON binding
        final FeatureCollection collection = FeatureCollection.builder().crs(Crs.builder().wgs84().build())
                .features(
                        Feature.builder().geometry(Point.builder()
                                .coordinates(LngLatAlt.builder().coordinates(1.0d, 2.0d).build()).build())
                                .build(),
                        Feature.builder().geometry(Point.builder()
                                .coordinates(LngLatAlt.builder().coordinates(3.0d, 4.0d).build()).build())
                                .build())
                .build();
        payload = JSON_MAPPER.writeValueAsString(collection);
        assertThat("payload is not null", payload, notNullValue());
        assertThat("payload is not empty", isNotBlank(payload));
        final FeatureCollection collection2 = JSON_MAPPER.readValue(payload, FeatureCollection.class);
        assertThat("feature collection back is not null", collection2, notNullValue());
        assertThat("feature collection back coincides with original", collection2, equalTo(collection));
        System.out.println("feature collection: " + collection.toString());
        System.out.println("feature collection JSON payload: " + payload);

        // test reading complex GeoJSON from files
        final Collection<File> files = getGeoJsonFiles();
        for (final File file : files) {
            System.out.println(" >> GeoJSON file: " + file.getCanonicalPath());
            final JsonNode typeNode = JSON_MAPPER.readTree(file).get("type");
            assertThat("GeoJSON type is not null", typeNode, notNullValue());
            final String type = typeNode.textValue();
            assertThat("GeoJSON type is not empty", isNotBlank(type));
            Object geojsonObject = null;
            if ("Point".equals(type)) {
                geojsonObject = JSON_MAPPER.readValue(file, Point.class);
            } else if ("LineString".equals(type)) {
                geojsonObject = JSON_MAPPER.readValue(file, LineString.class);
            } else if ("Polygon".equals(type)) {
                geojsonObject = JSON_MAPPER.readValue(file, Polygon.class);
            } else if ("FeatureCollection".equals(type)) {
                geojsonObject = JSON_MAPPER.readValue(file, FeatureCollection.class);
            } else {
                throw new IllegalStateException("Unsupported GeoJSON type: " + type);
            }
            assertThat("GeoJSON object is not null", geojsonObject, notNullValue());
            System.out.println("GeoJSON object: " + geojsonObject.toString());
            System.out.println("GeoJSON JSON: " + JSON_MAPPER.writeValueAsString(geojsonObject));
        }
    } catch (Exception e) {
        e.printStackTrace(System.err);
        fail("GeoJsonBindingTest.test() failed: " + e.getMessage());
    } finally {
        System.out.println("GeoJsonBindingTest.test() has finished");
    }
}

From source file:org.apache.streams.rss.serializer.SyndEntryActivitySerializer.java

public Activity deserializeWithRomeExtension(ObjectNode entry, boolean withExtension) {
    Preconditions.checkNotNull(entry);/* ww w  .  java2s  .  co m*/

    Activity activity = new Activity();
    Provider provider = buildProvider(entry);
    Actor actor = buildActor(entry);
    ActivityObject activityObject = buildActivityObject(entry);

    activityObject.setUrl(provider.getUrl());
    activityObject.setAuthor(actor.getAuthor());

    activity.setUrl(provider.getUrl());
    activity.setProvider(provider);
    activity.setActor(actor);
    activity.setVerb("post");
    activity.setId("id:rss:post:" + activity.getUrl());

    JsonNode published = entry.get("publishedDate");
    if (published != null) {
        try {
            activity.setPublished(RFC3339Utils.parseToUTC(published.textValue()));
        } catch (Exception e) {
            LOGGER.warn("Failed to parse date : {}", published.textValue());

            DateTime now = DateTime.now().withZone(DateTimeZone.UTC);
            activity.setPublished(now);
        }
    }

    activity.setUpdated(activityObject.getUpdated());
    activity.setObject(activityObject);

    if (withExtension) {
        activity = addRomeExtension(activity, entry);
    }

    return activity;
}

From source file:com.github.fge.jsonschema.keyword.special.PatternKeywordTest.java

@DataProvider
public Iterator<Object[]> getValueTests() {
    final List<Object[]> list = Lists.newArrayList();

    String msg;//from   w ww.jav  a2s. com
    JsonNode msgNode, msgData, msgParams;

    for (final JsonNode node : testData) {
        msgNode = node.get("message");
        msgData = node.get("msgData");
        msgParams = node.get("msgParams");
        msg = msgNode == null ? null : TestUtils.buildMessage(BUNDLE, msgNode.textValue(), msgParams, msgData);
        list.add(new Object[] { node.get("schema"), node.get("data"), msg, node.get("valid").booleanValue(),
                node.get("msgData") });
    }
    return list.iterator();
}

From source file:org.level28.android.moca.json.FaqDeserializer.java

private FaqEntry parseEntry(final String currentCategory, JsonNode objectRoot)
        throws JsonDeserializerException {
    if (objectRoot == null || objectRoot.isNull()) {
        throw new JsonDeserializerException("null objectRoot");
    }//from   w w  w .  j  a v  a2  s . c  o  m
    if (!objectRoot.isObject()) {
        throw new JsonDeserializerException("objectRoot is not a JSON object");
    }

    final JsonNode q = objectRoot.path("q");
    final JsonNode a = objectRoot.path("a");
    if (q.isMissingNode() || a.isMissingNode() || !q.isTextual() || !a.isTextual()) {
        throw new JsonDeserializerException("Malformed FAQ entry");
    }

    return new FaqEntry(faqIdTally++, currentCategory, q.textValue(), a.textValue());
}

From source file:com.github.fge.jsonschema.core.load.configuration.LoadingConfigurationBuilder.java

/**
 * Preload a schema/*from  w  w w . j  av  a 2  s . c o  m*/
 *
 * <p>Use this if the schema already has an absolute {@code id}.</p>
 *
 * @param schema the schema
 * @return this
 * @throws NullPointerException schema is null
 * @throws IllegalArgumentException schema has no {@code id}, or its {@code
 * id} is not an absolute JSON Reference
 * @see JsonRef
 */
public LoadingConfigurationBuilder preloadSchema(final JsonNode schema) {
    final JsonNode node = schema.path("id");
    BUNDLE.checkArgument(node.isTextual(), "loadingCfg.noIDInSchema");
    return preloadSchema(node.textValue(), schema);
}

From source file:com.unboundid.scim2.common.utils.JsonUtils.java

/**
 * Compares two JsonNodes for order. Nodes containing datetime and numerical
 * values are ordered accordingly. Otherwise, the values' string
 * representation will be compared lexicographically.
 *
 * @param n1 the first node to be compared.
 * @param n2 the second node to be compared.
 * @param attributeDefinition The attribute definition of the attribute
 *                            whose values to compare or {@code null} to
 *                            compare string values using case insensitive
 *                            matching.//from   w  w  w  .  j av  a 2 s  .com
 * @return a negative integer, zero, or a positive integer as the
 *         first argument is less than, equal to, or greater than the second.
 */
public static int compareTo(final JsonNode n1, final JsonNode n2,
        final AttributeDefinition attributeDefinition) {
    if (n1.isTextual() && n2.isTextual()) {
        Date d1 = dateValue(n1);
        Date d2 = dateValue(n2);
        if (d1 != null && d2 != null) {
            return d1.compareTo(d2);
        } else {
            if (attributeDefinition != null && attributeDefinition.getType() == AttributeDefinition.Type.STRING
                    && attributeDefinition.isCaseExact()) {
                return n1.textValue().compareTo(n2.textValue());
            }
            return StaticUtils.toLowerCase(n1.textValue()).compareTo(StaticUtils.toLowerCase(n2.textValue()));
        }
    }

    if (n1.isNumber() && n2.isNumber()) {
        if (n1.isBigDecimal() || n2.isBigDecimal()) {
            return n1.decimalValue().compareTo(n2.decimalValue());
        }

        if (n1.isFloatingPointNumber() || n2.isFloatingPointNumber()) {
            return Double.compare(n1.doubleValue(), n2.doubleValue());
        }

        if (n1.isBigInteger() || n2.isBigInteger()) {
            return n1.bigIntegerValue().compareTo(n2.bigIntegerValue());
        }

        return Long.compare(n1.longValue(), n2.longValue());
    }

    // Compare everything else lexicographically
    return n1.asText().compareTo(n2.asText());
}

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

@DataProvider
public final Iterator<Object[]> testData() {
    final List<Object[]> list = Lists.newArrayList();

    String msg;//from ww w  .  ja v  a2 s .  co  m
    JsonNode msgNode, msgData, msgParams;

    for (final JsonNode node : testNode) {
        msgNode = node.get("message");
        msgData = node.get("msgData");
        msgParams = node.get("msgParams");
        msg = msgNode == null ? null : buildMessage(msgNode.textValue(), msgParams, msgData);
        list.add(new Object[] { node.get("data"), node.get("valid").booleanValue(), msg, msgData });
    }

    return list.iterator();
}

From source file:com.datamountaineer.streamreactor.connect.json.SimpleJsonConverterTest.java

@Test
public void timeToJson() throws IOException {
    GregorianCalendar calendar = new GregorianCalendar(1970, Calendar.JANUARY, 1, 0, 0, 0);
    calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
    calendar.add(Calendar.MILLISECOND, 14400000);
    java.util.Date date = calendar.getTime();

    JsonNode converted = converter.fromConnectData(Time.SCHEMA, date);
    assertTrue(converted.isTextual());/*w  w w  .  j  a  v  a  2s .c  o m*/
    assertEquals(SimpleJsonConverter.TIME_FORMAT.format(date), converted.textValue());
}

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

@DataProvider
protected final Iterator<Object[]> getValueTests() {
    final List<Object[]> list = Lists.newArrayList();

    String msg;//  w  ww  .j  a  v a  2  s.co m
    JsonNode msgNode, msgData, msgParams;

    for (final JsonNode node : testNode) {
        msgNode = node.get("message");
        msgData = node.get("msgData");
        msgParams = node.get("msgParams");
        msg = msgNode == null ? null : buildMessage(msgNode.textValue(), msgParams, msgData);
        list.add(new Object[] { node.get("digest"), node.get("data"), msg, node.get("valid").booleanValue(),
                msgData });
    }

    return list.iterator();
}