Example usage for com.fasterxml.jackson.core JsonParser readValueAs

List of usage examples for com.fasterxml.jackson.core JsonParser readValueAs

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonParser readValueAs.

Prototype

@SuppressWarnings("unchecked")
public <T> T readValueAs(TypeReference<?> valueTypeRef) throws IOException, JsonProcessingException 

Source Link

Document

Method to deserialize JSON content into a Java type, reference to which is passed as argument.

Usage

From source file:org.springframework.social.twitter.api.impl.TweetDeserializer.java

@Override
public Tweet deserialize(final JsonParser jp, final DeserializationContext ctx) throws IOException {
    final JsonNode node = jp.readValueAs(JsonNode.class);
    if (null == node || node.isMissingNode() || node.isNull()) {
        return null;
    }/*from   w w w .  j  a  v  a2s. c  o m*/
    final Tweet tweet = this.deserialize(node);
    jp.skipChildren();
    return tweet;
}

From source file:org.agorava.twitter.jackson.SimilarPlacesDeserializer.java

@Override
public SimilarPlacesResponse deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    ObjectMapper mapper = BeanResolver.getInstance().resolve(ObjectMapper.class);
    jp.setCodec(mapper);/*from w w  w  .ja  v  a 2 s.co m*/
    JsonNode node = jp.readValueAs(JsonNode.class);
    JsonNode resultNode = node.get("result");
    String token = resultNode.get("token").textValue();
    JsonNode placesNode = resultNode.get("places");
    @SuppressWarnings("unchecked")
    List<Place> places = (List<Place>) mapper.reader(new TypeReference<List<Place>>() {
    }).readValue(placesNode);
    return new SimilarPlacesResponse(places, token);
}

From source file:org.springframework.social.twitter.api.impl.SimilarPlacesDeserializer.java

@Override
public SimilarPlacesResponse deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new TwitterModule());
    jp.setCodec(mapper);//from   w  w  w .  jav  a2 s  .  com
    JsonNode node = jp.readValueAs(JsonNode.class);
    JsonNode resultNode = node.get("result");
    String token = resultNode.get("token").textValue();
    JsonNode placesNode = resultNode.get("places");
    @SuppressWarnings("unchecked")
    List<Place> places = (List<Place>) mapper.reader(new TypeReference<List<Place>>() {
    }).readValue(placesNode);
    return new SimilarPlacesResponse(places, token);
}

From source file:com.arpnetworking.jackson.BuilderDeserializer.java

/**
 * {@inheritDoc}/*from   w  ww .  ja  va  2  s  . c o m*/
 */
@Override
public T deserialize(final JsonParser parser, final DeserializationContext context) throws IOException {
    final Builder<? extends T> builder = parser.readValueAs(_builderClass);
    return builder.build();
}

From source file:no.ssb.jsonstat.v2.deser.DimensionDeserializer.java

private ImmutableMap<String, String> parseIndexAsArray(JsonParser p) throws IOException {
    ImmutableMap<String, String> index;

    List<String> listIndex = p.readValueAs(INDEX_LIST);
    index = IntStream.range(0, listIndex.size()).boxed()
            .collect(toImmutableMap(listIndex::get, Object::toString));
    return index;
}

From source file:no.ssb.jsonstat.v2.deser.DimensionDeserializer.java

private ImmutableMap<String, String> parseIndexAsMap(JsonParser p) throws IOException {
    ImmutableMap<String, String> index;

    Map<String, Integer> mapIndex = p.readValueAs(INDEX_MAP);

    // Even though the type is String, the sorting actually uses the
    // integer value thanks to the forMap function.
    Ordering<String> byValue = Ordering.natural().onResultOf(Functions.forMap(mapIndex));

    index = ImmutableSortedMap.copyOf(Maps.transformValues(mapIndex, Object::toString), byValue);
    return index;
}

From source file:com.basistech.rosette.dm.jackson.VersionCheckDeserializer.java

@Override
public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
    if (p.getCurrentToken() != JsonToken.VALUE_STRING) {
        throw ctxt.wrongTokenException(p, JsonToken.VALUE_STRING, "The value of 'version' must be a string");
    }/*from   w  w w  . ja va2  s . c  o  m*/
    String version = p.readValueAs(String.class);
    String[] bits = version.split("\\.");
    if (bits.length < 3) { // allow for a fourth digit for some reason some day.
        throw ctxt.weirdStringException(version, String.class, "Versions must be of the form x.y.z");
    }
    if (!"1".equals(bits[0])) {
        throw ctxt.weirdStringException(version, String.class,
                String.format("Incompatible ADM version %s", version));
    }
    return version;
}

From source file:org.agorava.facebook.jackson.ReferenceListAndCountDeserializer.java

@SuppressWarnings("unchecked")
@Override//  w w w.  j a  v  a  2s .c  om
public ListAndCount<Reference> deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    ObjectMapper mapper = getInstance().resolve(ObjectMapper.class);

    jp.setCodec(mapper);
    if (jp.hasCurrentToken()) {
        JsonNode node = jp.readValueAs(JsonNode.class);
        JsonNode dataNode = node.get("data");
        List<Reference> commentsList = dataNode != null
                ? (List<Reference>) mapper.reader(new TypeReference<List<Reference>>() {
                }).readValue(dataNode)
                : Collections.<Reference>emptyList();
        JsonNode countNode = node.get("count");
        int referenceCount = countNode != null ? countNode.intValue() : 0;
        return new ListAndCount<Reference>(commentsList, referenceCount);
    }

    return null;
}

From source file:org.agorava.facebook.jackson.CommentListAndCountDeserializer.java

@SuppressWarnings("unchecked")
@Override//from   w  w  w . ja  v a 2  s .c  om
public ListAndCount<Comment> deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    ObjectMapper mapper = getInstance().resolve(ObjectMapper.class);
    jp.setCodec(mapper);
    if (jp.hasCurrentToken()) {
        JsonNode commentsNode = jp.readValueAs(JsonNode.class);
        JsonNode dataNode = commentsNode.get("data");
        List<Comment> commentsList = dataNode != null
                ? (List<Comment>) mapper.reader(new TypeReference<List<Comment>>() {
                }).readValue(dataNode)
                : Collections.<Comment>emptyList();
        return new ListAndCount<Comment>(commentsList, commentsList.size());
    }

    return null;
}

From source file:org.agorava.facebook.jackson.ReferenceListDeserializer.java

@SuppressWarnings("unchecked")
@Override// w  w w. ja  va 2  s .  c o m
public List<Reference> deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    ObjectMapper mapper = getInstance().resolve(ObjectMapper.class);
    jp.setCodec(mapper);
    if (jp.hasCurrentToken()) {
        JsonNode dataNode = (JsonNode) jp.readValueAs(JsonNode.class).get("data");
        if (dataNode != null) {
            return (List<Reference>) mapper.reader(new TypeReference<List<Reference>>() {
            }).readValue(dataNode);
        }
    }

    return Collections.emptyList();
}