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.evrythng.java.wrapper.mapping.ActionDeserializerImpl.java

@Override
public Action deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException {

    ObjectCodec codec = jp.getCodec();/*from   w  ww .  j  a  va  2 s .  c o  m*/
    ObjectMapper mapper = (ObjectMapper) codec;
    ObjectNode root = mapper.readTree(jp);
    JsonNode type = root.get(getTypeFieldName());
    if (type == null) {
        throw new IllegalArgumentException(this.getValueClass().getSimpleName() + " type cannot be empty.");
    }
    String sType = type.textValue();
    if (sType == null || sType.isEmpty()) {
        throw new IllegalArgumentException(this.getValueClass().getSimpleName() + " type cannot be empty.");
    }

    return codec.treeToValue(root, resolveClass(sType));
}

From source file:org.apache.taverna.activities.interaction.InteractionActivity.java

String getPresentationOrigin() {
    JsonNode subNode = json.get("presentationOrigin");
    if (subNode == null) {
        return null;
    }/*from ww w  .  ja va  2s. c  o m*/
    String textValue = subNode.textValue();
    if (textValue == null) {
        return null;
    }
    return textValue;
}

From source file:com.evrythng.java.wrapper.mapping.GeoJsonDeserializerImpl.java

@Override
public GeoJson deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    ObjectCodec codec = jp.getCodec();/*from  w ww. j ava2  s. co m*/
    ObjectMapper mapper = (ObjectMapper) codec;
    ObjectNode root = (ObjectNode) mapper.readTree(jp);
    JsonNode type = root.get(getTypeFieldName());
    if (type == null) {
        throw new IllegalArgumentException(this.getValueClass().getSimpleName() + " type cannot be empty.");
    }
    String sType = type.textValue();
    if (sType == null || sType.isEmpty()) {
        throw new IllegalArgumentException(this.getValueClass().getSimpleName() + " type cannot be empty.");
    }

    Class<GeoJson> clazz = (Class<GeoJson>) resolveClass(sType);

    GeoJson obj = codec.treeToValue(root, clazz);
    if (obj == null) {
        throw new IllegalArgumentException(
                this.getValueClass().getSimpleName() + " type deserialised as null: " + root.toString());
    }
    return obj;
}

From source file:org.aperte.lp.movies.OpeningParser.java

/**
 * Returns a list of opening movies given a JSON input stream conforming
 * to the Rotten Tomatoes opening movie model.
 *
 * @param in the input stream/*w w  w  .ja va2 s.  com*/
 * @return the opening movies
 * @throws ParseException if there's an error parsing the input stream
 * @throws IOException    if there's an error reading the input stream
 */
List<OpeningMovie> parseMovies(InputStream in) throws ParseException, IOException {
    ObjectMapper mapper = new ObjectMapper();
    JsonNode root = null;
    try {
        root = mapper.readTree(in);
    } catch (JsonProcessingException e) {
        throw new ParseException("Unable to parse JSON", e);
    }

    if (root != null) {
        JsonNode movies = root.get("movies");
        if (movies == null) {
            return Collections.<OpeningMovie>emptyList();
        }

        List<OpeningMovie> result = new ArrayList<OpeningMovie>(movies.size());
        for (JsonNode movie : movies) {
            OpeningMovie o = new OpeningMovie();

            JsonNode title = movie.get("title");
            if (title != null) {
                o.setTitle(title.textValue());
            }

            JsonNode ratings = movie.get("ratings");
            if (ratings != null) {
                JsonNode criticsRating = ratings.get("critics_rating");
                if (criticsRating != null) {
                    o.setCriticsRating(criticsRating.textValue());
                }

                JsonNode criticsScore = ratings.get("critics_score");
                if (criticsScore != null) {
                    o.setCriticsScore(criticsScore.intValue());
                }

                JsonNode audienceRating = ratings.get("audience_rating");
                if (audienceRating != null) {
                    o.setAudienceRating(audienceRating.textValue());
                }

                JsonNode audienceScore = ratings.get("audience_score");
                if (audienceScore != null) {
                    o.setAudienceScore(audienceScore.intValue());
                }
            }

            JsonNode posters = movie.get("posters");
            if (posters != null) {
                JsonNode thumbnail = posters.get("thumbnail");
                if (thumbnail != null) {
                    o.setPosterThumbnailUrl(thumbnail.textValue());
                }

                JsonNode profile = posters.get("profile");
                if (profile != null) {
                    o.setPosterProfileUrl(profile.textValue());
                }
            }

            JsonNode cast = movie.get("abridged_cast");
            if (cast != null) {
                List<String> actorNames = new ArrayList<String>(cast.size());
                for (JsonNode actor : cast) {
                    JsonNode name = actor.get("name");
                    if (name != null) {
                        actorNames.add(name.textValue());
                    }
                }
                o.setCast(actorNames);
            }

            result.add(o);
        }

        return result;
    }

    return Collections.<OpeningMovie>emptyList();
}

From source file:io.sidecar.event.EventJsonValidationTest.java

private Set<String> tagsJsonArrayToSet() {
    return Sets//from   w  w w . j  av  a 2 s . co m
            .newHashSet(Iterables.transform(eventAsObjectNode.path("tags"), new Function<JsonNode, String>() {
                @Override
                public String apply(JsonNode input) {
                    return input.textValue();
                }
            }));
}

From source file:com.github.fge.jsonschema.keyword.digest.draftv4.DraftV4TypeDigester.java

@Override
public JsonNode digest(final JsonNode schema) {
    final ObjectNode ret = FACTORY.objectNode();
    final ArrayNode allowedTypes = FACTORY.arrayNode();
    ret.put(keyword, allowedTypes);//  w  w w.  jav  a  2 s . com

    final JsonNode node = schema.get(keyword);

    final EnumSet<NodeType> typeSet = EnumSet.noneOf(NodeType.class);

    if (node.isTextual()) // Single type
        typeSet.add(NodeType.fromName(node.textValue()));
    else // More than one type
        for (final JsonNode element : node)
            typeSet.add(NodeType.fromName(element.textValue()));

    if (typeSet.contains(NodeType.NUMBER))
        typeSet.add(NodeType.INTEGER);

    /*
     * Note that as this is an enumset, order is guaranteed
     */
    for (final NodeType type : typeSet)
        allowedTypes.add(type.toString());

    return ret;
}

From source file:org.eel.kitchen.jsonschema.GsonProvider.java

private JsonElement jsonNodeToGson(final JsonNode node) {
    if (node.isNull())
        return JsonNull.INSTANCE;
    if (node.isTextual())
        return new JsonPrimitive(node.textValue());
    if (node.isBoolean())
        return new JsonPrimitive(node.booleanValue());
    if (node.isNumber())
        return new JsonPrimitive(node.numberValue());

    return node.isArray() ? arrayNodeToGson(node) : objectNodeToGson(node);
}

From source file:de.tudarmstadt.ukp.dkpro.wsd.wsi.si.InducedSenseInventory.java

/**
 * Loads a serialized sense inventory from a json file
 *
 * @param fileName// w  ww .jav a2s.  com
 * @throws WSDException
 */
public void loadInventory(String fileName) throws WSDException {
    ObjectMapper mapper = new ObjectMapper();
    try {
        BufferedReader fileReader = new BufferedReader(new FileReader(fileName));
        JsonNode root = mapper.readTree(fileReader);
        Iterator<JsonNode> it = root.elements();
        while (it.hasNext()) {
            JsonNode termNode = it.next();
            if (termNode.has("term")) {
                String term = termNode.get("term").textValue();
                Iterator<JsonNode> it2 = termNode.get("clusters").elements();
                int senseIndex = 0;
                while (it2.hasNext()) {
                    String senseId = term + "_" + senseIndex++;
                    JsonNode clusterNode = it2.next();
                    Iterator<JsonNode> it3 = clusterNode.elements();
                    Collection<String> cluster = new LinkedList<String>();
                    while (it3.hasNext()) {

                        Iterator<JsonNode> it4 = it3.next().elements();
                        while (it4.hasNext()) {
                            JsonNode wordNode = it4.next();
                            cluster.add(wordNode.textValue());
                        }

                    }
                    addSense(term, senseId, cluster);

                }

            }
        }
        System.out.println("loaded " + inventory.size() + " terms and " + clusters.size() + " sense clusters");
    } catch (Exception e) {
        throw new WSDException(e);
    }

}

From source file:com.github.fge.jsonschema.keyword.digest.helpers.DraftV3TypeKeywordDigester.java

@Override
public JsonNode digest(final JsonNode schema) {
    final ObjectNode ret = FACTORY.objectNode();
    final ArrayNode simpleTypes = FACTORY.arrayNode();
    ret.put(keyword, simpleTypes);//ww w.j  ava  2 s. c  om
    final ArrayNode schemas = FACTORY.arrayNode();
    ret.put("schemas", schemas);

    final JsonNode node = schema.get(keyword);

    final EnumSet<NodeType> set = EnumSet.noneOf(NodeType.class);

    if (node.isTextual()) // Single type
        putType(set, node.textValue());
    else { // More than one type, and possibly schemas
        final int size = node.size();
        JsonNode element;
        for (int index = 0; index < size; index++) {
            element = node.get(index);
            if (element.isTextual())
                putType(set, element.textValue());
            else
                schemas.add(index);
        }
    }

    /*
     * If all types are there, no need to collect schemas
     */
    if (EnumSet.complementOf(set).isEmpty())
        schemas.removeAll();

    /*
     * Note that as this is an enumset, order is guaranteed
     */
    for (final NodeType type : set)
        simpleTypes.add(type.toString());

    return ret;
}

From source file:com.netflix.conductor.contribs.queue.QueueManager.java

private String getValue(String fieldName, JsonNode json) {
    JsonNode node = json.findValue(fieldName);
    if (node == null) {
        return null;
    }/*from  w w w.j  a v  a  2s . c o  m*/
    return node.textValue();
}