List of usage examples for com.fasterxml.jackson.core JsonParser readValueAs
@SuppressWarnings("unchecked") public <T> T readValueAs(TypeReference<?> valueTypeRef) throws IOException, JsonProcessingException
From source file:org.springframework.social.linkedin.api.impl.json.ConnectionAuthorizationDeserializer.java
public ConnectionAuthorization deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new LinkedInModule()); if (jp.hasCurrentToken() && jp.getCurrentToken().equals(JsonToken.START_OBJECT)) { JsonNode dataNode = jp.readValueAs(JsonNode.class).get("headers").get("values").get(0); if (dataNode != null) { return mapper.reader(new TypeReference<ConnectionAuthorization>() { }).readValue(dataNode);/*from w w w . j av a2 s. c om*/ } } throw ctxt.mappingException("Expected JSON object"); }
From source file:org.hawkular.apm.server.elasticsearch.SpanServiceElasticsearch.java
private <T> T deserialize(String json, Class<T> type) throws IOException { JsonParser parser = mapper.getFactory().createParser(json); return parser.readValueAs(type); }
From source file:eu.trentorise.opendata.commons.jackson.TodCommonsModule.java
/** * Creates the module and registers all the needed serializers and * deserializers for Tod Commons objects *//* w w w.ja v a 2s . c o m*/ public TodCommonsModule() { super("tod-commons-jackson", readJacksonVersion(TodCommonsModule.class)); addSerializer(Dict.class, new StdSerializer<Dict>(Dict.class) { @Override public void serialize(Dict value, JsonGenerator jgen, SerializerProvider provider) throws IOException { jgen.writeObject(value.asMultimap()); } }); addDeserializer(Dict.class, new StdDeserializer<Dict>(Dict.class) { @Override public Dict deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { TypeReference ref = new TypeReference<ImmutableListMultimap<Locale, String>>() { }; return Dict.of((ImmutableListMultimap) jp.readValueAs(ref)); } }); addDeserializer(Locale.class, new LocaleDeserializer()); setMixInAnnotation(LocalizedString.class, JacksonLocalizedString.class); setMixInAnnotation(Ref.class, JacksonRef.class); }
From source file:ch.rasc.wampspring.message.EventMessage.java
public EventMessage(JsonParser jp, WampSession wampSession) throws IOException { super(WampMessageType.EVENT); if (jp.nextToken() != JsonToken.VALUE_STRING) { throw new IOException(); }//from w w w. j a v a2 s .c om setTopicURI(replacePrefix(jp.getValueAsString(), wampSession)); jp.nextToken(); this.event = jp.readValueAs(Object.class); }
From source file:no.ssb.jsonstat.v2.deser.DimensionDeserializer.java
private Map<String, String> parseCategoryLabel(JsonParser p, DeserializationContext ctxt) throws IOException { if (p.currentToken() != JsonToken.START_OBJECT) ctxt.reportWrongTokenException(p, JsonToken.START_OBJECT, "label was not an object", (Object) null); Map<String, String> label = p.readValueAs(LABEL_MAP); return checkNotNull(label, "label object was null"); }
From source file:no.ssb.jsonstat.v2.deser.DatasetDeserializer.java
List<Number> parseValues(JsonParser p, DeserializationContext ctxt) throws IOException { List<Number> result = Collections.emptyList(); switch (p.getCurrentToken()) { case START_OBJECT: SortedMap<Integer, Number> map = p.readValueAs(VALUES_MAP); result = new AbstractList<Number>() { @Override//ww w .j a va 2 s.c o m public int size() { return map.lastKey() + 1; } @Override public Number get(int index) { return map.get(index); } }; break; case START_ARRAY: result = p.readValueAs(VALUES_LIST); break; default: ctxt.handleUnexpectedToken(this._valueClass, p.getCurrentToken(), p, "msg"); } return result; }
From source file:org.killbill.billing.plugin.meter.timeline.persistent.Replayer.java
@VisibleForTesting public void read(final File file, final Function<SourceSamplesForTimestamp, Void> fn) throws IOException { final JsonParser smileParser = smileFactory.createJsonParser(file); if (smileParser.nextToken() != JsonToken.START_ARRAY) { return;// w ww . ja v a 2s . c om } while (!shuttingDown.get() && smileParser.nextToken() != JsonToken.END_ARRAY) { final SourceSamplesForTimestamp sourceSamplesForTimestamp = smileParser .readValueAs(SourceSamplesForTimestamp.class); fn.apply(sourceSamplesForTimestamp); } smileParser.close(); }
From source file:com.basistech.AfterburnerOopsTest.java
@Test public void oops() throws Exception { SmileFactory smileFactory = new SmileFactory(); ObjectMapper mapper = new ObjectMapper(smileFactory); mapper = AnnotatedDataModelModule.setupObjectMapper(mapper); EntityMention em = new EntityMention(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); List<EntityMention> mentions = Lists.newArrayList(); mentions.add(em);/*from w w w. j a v a 2s. c om*/ mapper.writeValue(byteArrayOutputStream, mentions); mapper = AnnotatedDataModelModule.setupObjectMapper(new ObjectMapper(smileFactory)); mapper.registerModule(new AfterburnerModule()); JsonParser jp = smileFactory.createParser(new ByteArrayInputStream(byteArrayOutputStream.toByteArray())); jp.setCodec(mapper); JsonToken current; current = jp.nextToken(); if (current != JsonToken.START_ARRAY) { System.err.println("Error: root should be array: quiting."); return; } while (jp.nextToken() != JsonToken.END_ARRAY) { jp.readValueAs(EntityMention.class); } }
From source file:ch.rasc.wampspring.message.PublishMessage.java
public PublishMessage(JsonParser jp, WampSession wampSession) throws IOException { super(WampMessageType.PUBLISH); if (jp.nextToken() != JsonToken.VALUE_STRING) { throw new IOException(); }//from ww w. j a v a2 s . c o m setTopicURI(replacePrefix(jp.getValueAsString(), wampSession)); jp.nextToken(); this.event = jp.readValueAs(Object.class); if (jp.nextToken() != JsonToken.END_ARRAY) { if (jp.getCurrentToken() == JsonToken.VALUE_TRUE || jp.getCurrentToken() == JsonToken.VALUE_FALSE) { this.excludeMe = jp.getValueAsBoolean(); this.exclude = null; this.eligible = null; if (jp.nextToken() != JsonToken.END_ARRAY) { // Wrong message format, excludeMe should not be followed by // any value throw new IOException(); } } else { this.excludeMe = null; TypeReference<Set<String>> typRef = new TypeReference<Set<String>>() { // nothing here }; if (jp.getCurrentToken() != JsonToken.START_ARRAY) { throw new IOException(); } this.exclude = jp.readValueAs(typRef); if (jp.nextToken() == JsonToken.START_ARRAY) { this.eligible = jp.readValueAs(typRef); } else { this.eligible = null; } } } else { this.excludeMe = null; this.exclude = null; this.eligible = null; } }
From source file:org.agorava.linkedin.jackson.LinkedInNetworkUpdateListDeserializer.java
@Override public LinkedInNetworkUpdate deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { ObjectMapper mapper = BeanResolver.getInstance().resolve(ObjectMapper.class); jp.setCodec(mapper);// ww w.j av a 2 s. co m JsonNode dataNode = jp.readValueAs(JsonNode.class); if (dataNode != null) { LinkedInNetworkUpdate linkedInNetworkUpdate = (LinkedInNetworkUpdate) mapper .reader(new TypeReference<LinkedInNetworkUpdate>() { }).readValue(dataNode); UpdateContent updatedContent = null; UpdateType type = linkedInNetworkUpdate.getUpdateType(); JsonNode updatedNode = dataNode.get("updateContent"); JsonNode person = updatedNode.get("person"); if (type == UpdateType.MSFC) { // Totally different. Looks like a bad API to be honest. person = updatedNode.get("companyPersonUpdate").get("person"); } switch (type) { case CONN: updatedContent = mapper.reader(new TypeReference<UpdateContentConnection>() { }).readValue(person); break; case STAT: updatedContent = mapper.reader(new TypeReference<UpdateContentStatus>() { }).readValue(person); break; case JGRP: updatedContent = mapper.reader(new TypeReference<UpdateContentGroup>() { }).readValue(person); break; case PREC: case SVPR: updatedContent = mapper.reader(new TypeReference<UpdateContentRecommendation>() { }).readValue(person); break; case APPM: updatedContent = mapper.reader(new TypeReference<UpdateContentPersonActivity>() { }).readValue(person); break; case MSFC: updatedContent = mapper.reader(new TypeReference<UpdateContentFollow>() { }).readValue(person); break; case VIRL: updatedContent = mapper.reader(new TypeReference<UpdateContentViral>() { }).readValue(person); break; case SHAR: updatedContent = mapper.reader(new TypeReference<UpdateContentShare>() { }).readValue(person); break; case CMPY: updatedContent = mapper.reader(new TypeReference<UpdateContentCompany>() { }).readValue(updatedNode); break; default: try { updatedContent = mapper.reader(new TypeReference<UpdateContent>() { }).readValue(person); } catch (Exception e) { throw new RuntimeException(e); } } // Need to use reflection to set private updateContent field try { Field f = LinkedInNetworkUpdate.class.getDeclaredField("updateContent"); f.setAccessible(true); f.set(linkedInNetworkUpdate, updatedContent); } catch (Exception e) { throw new RuntimeException(e); } if (type == UpdateType.MSFC) { // Set the action via reflection as it's private String action = updatedNode.get("companyPersonUpdate").get("action").get("code").asText(); try { Field f = UpdateContentFollow.class.getDeclaredField("action"); f.setAccessible(true); f.set(updatedContent, action); } catch (Exception e) { throw new RuntimeException(e); } // Set following via reflection as it's private Company company = mapper.reader(new TypeReference<Company>() { }).readValue(updatedNode.get("company")); try { Field f = UpdateContentFollow.class.getDeclaredField("following"); f.setAccessible(true); f.set(updatedContent, company); } catch (Exception e) { throw new RuntimeException(e); } } else if (type == UpdateType.VIRL) { JsonNode originalUpdate = updatedNode.path("updateAction").path("originalUpdate"); UpdateAction updateAction = mapper.reader(new TypeReference<UpdateAction>() { }).readValue(originalUpdate); String code = updatedNode.path("updateAction").path("action").path("code").textValue(); // Set private immutable field action on updateAction try { Field f = UpdateAction.class.getDeclaredField("action"); f.setAccessible(true); f.set(updateAction, code); } catch (Exception e) { throw new RuntimeException(e); } // Set private immutable field updateAction on updatedContent try { Field f = UpdateContentViral.class.getDeclaredField("updateAction"); f.setAccessible(true); f.set(updatedContent, updateAction); } catch (Exception e) { throw new RuntimeException(e); } } return linkedInNetworkUpdate; } return null; }