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:com.microsoft.azure.serializer.CloudErrorDeserializer.java

@Override
public CloudError deserialize(JsonParser p, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    JsonNode topNode = p.readValueAsTree();
    if (topNode == null) {
        return null;
    }//from  w w w  .j a  va2 s.  c o  m
    JsonNode errorNode = topNode.get("error");
    if (errorNode == null) {
        return null;
    }
    JsonParser parser = new JsonFactory().createParser(errorNode.toString());
    parser.setCodec(mapper);
    return parser.readValueAs(CloudError.class);
}

From source file:com.okta.sdk.framework.JsonApiClient.java

@Override
protected <T> T unmarshall(HttpResponse response, TypeReference<T> clazz) throws IOException {
    if (response.getEntity() == null || clazz.getType().equals(Void.class)) {
        EntityUtils.consume(response.getEntity());
        return null;
    }//  www . j a v  a  2s. co m
    InputStream inputStream = response.getEntity().getContent();
    JsonParser parser = objectMapper.getFactory().createParser(inputStream);
    T toReturn = parser.readValueAs(clazz);
    EntityUtils.consume(response.getEntity());
    return toReturn;
}

From source file:com.meltmedia.jackson.crypto.beans.Base64Deserializer.java

@Override
public String deserialize(JsonParser parser, DeserializationContext arg1)
        throws IOException, JsonProcessingException {

    byte[] decoded = decoder.decode(parser.readValueAs(String.class));
    return new String(decoded, "UTF-8");
}

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

@Override
public LocalTrendsHolder deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    JsonNode node = jp.readValueAs(JsonNode.class);
    Iterator<JsonNode> dayIt = node.iterator();
    if (dayIt.hasNext()) {
        JsonNode day = dayIt.next();//from  w  ww. j a v  a 2 s.  c om
        Date createdAt = toDate(day.get("created_at").asText());
        JsonNode trendNodes = day.get("trends");
        List<Trend> trends = new ArrayList<Trend>();
        for (Iterator<JsonNode> trendsIt = trendNodes.iterator(); trendsIt.hasNext();) {
            JsonNode trendNode = trendsIt.next();
            trends.add(new Trend(trendNode.get("name").asText(), trendNode.get("query").asText()));
        }
        jp.skipChildren();
        return new LocalTrendsHolder(new Trends(createdAt, trends));
    }

    throw ctxt.mappingException(LocalTrendsHolder.class);
}

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

@Override
public RateLimitStatusHolder deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    JsonNode node = jp.readValueAs(JsonNode.class);
    if (null == node || node.isMissingNode() || node.isNull()) {
        return null;
    }/*from w w w.  ja va 2  s  .  co  m*/
    JsonNode resources = node.get("resources");
    Map<ResourceFamily, List<RateLimitStatus>> rateLimits = new EnumMap<ResourceFamily, List<RateLimitStatus>>(
            ResourceFamily.class);
    for (Iterator<Entry<String, JsonNode>> resourceFamilyIt = resources.fields(); resourceFamilyIt.hasNext();) {
        Entry<String, JsonNode> resourceFamilyNode = resourceFamilyIt.next();
        List<RateLimitStatus> rateLimitsList = new LinkedList<RateLimitStatus>();
        for (Iterator<Entry<String, JsonNode>> resourceEndpointIt = resourceFamilyNode.getValue()
                .fields(); resourceEndpointIt.hasNext();) {
            Entry<String, JsonNode> endpointNode = resourceEndpointIt.next();
            RateLimitStatus endpointLimit = new RateLimitStatus(endpointNode.getKey(),
                    endpointNode.getValue().get("limit").asInt(),
                    endpointNode.getValue().get("remaining").asInt(),
                    endpointNode.getValue().get("reset").asInt());
            rateLimitsList.add(endpointLimit);
        }
        rateLimits.put(ResourceFamily.getResourceFamily(resourceFamilyNode.getKey()), rateLimitsList);
    }
    return new RateLimitStatusHolder(rateLimits);
}

From source file:org.intelligentsia.dowsers.core.serializers.jackson.LocaleJsonDeserializer.java

@Override
public Locale deserialize(final JsonParser jp, final DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    final String locale = jp.readValueAs(String.class);
    return parse(locale);
}

From source file:org.hawkular.apm.server.api.utils.zipkin.BinaryAnnotationMappingStorage.java

private void loadMappings(String path) {
    try {//w w  w. j av  a 2s. c om
        String file = readFile(new File(path));

        TypeReference<Map<String, BinaryAnnotationMapping>> typeReference = new TypeReference<Map<String, BinaryAnnotationMapping>>() {
        };

        ObjectMapper objectMapper = new ObjectMapper();
        JsonParser parser = objectMapper.getFactory().createParser(file);

        keyBasedMappings = Collections.unmodifiableMap(parser.readValueAs(typeReference));
    } catch (IOException ex) {
        log.errorf("Could not load Zipkin binary annotation mapping file %s", path);
        keyBasedMappings = Collections.emptyMap();
    }
}

From source file:com.github.marcosalis.kraken.utils.json.ArrayToListDeserializer.java

@Override
public List<M> deserialize(JsonParser jsonparser, DeserializationContext deserializationcontext)
        throws IOException, JsonProcessingException {
    try {/*w  w w. j a  v  a 2  s.  com*/
        List<M> list = jsonparser.readValueAs(getTypeReference());
        return list;
    } catch (JsonProcessingException e) {
        LogUtils.logException(e);
        return null; // something wrong happened
    }
}

From source file:org.springframework.social.betaseries.api.impl.json.EpisodeDeserializer.java

@Override
public Episode deserialize(JsonParser jp, DeserializationContext ctx)
        throws IOException, JsonProcessingException {
    final JsonNode node = jp.readValueAs(JsonNode.class);
    if (null == node || node.isMissingNode() || node.isNull()) {
        return null;
    }/*from  ww  w  . j  a v a  2 s.co  m*/
    mapper = new ObjectMapper().registerModule(new BetaSeriesModule());

    final Episode episode = this.deserialize(node);
    return episode;
}

From source file:to.sven.androidrccar.arduino.test.AccessoryTestSuiteRunner.java

/**
 * Creates a new {@link AccessoryTestSuiteRunner}
 * @param testSuite A Stream where the {@link TestSuite} can be read from.
 * @throws Exception Could not read the {@link TestSuite}
 *//*from  w  w  w  .j a va  2 s .c o m*/
public AccessoryTestSuiteRunner(InputStream testSuite) throws Exception {
    ObjectMapper jsonMapper = new ObjectMapper();
    JsonFactory factory = jsonMapper.getJsonFactory();

    JsonParser jsonParser;
    jsonParser = factory.createJsonParser(testSuite);
    this.testSuite = jsonParser.readValueAs(TestSuite.class);
}