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.facebook.api.impl.json.MessageTagMapDeserializer.java
@SuppressWarnings("unchecked") @Override// w w w . j a v a2 s . c o m public Map<Integer, List<MessageTag>> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new FacebookModule()); jp.setCodec(mapper); if (jp.hasCurrentToken()) { JsonNode dataNode = jp.readValueAs(JsonNode.class); if (dataNode != null) { return (Map<Integer, List<MessageTag>>) mapper .reader(new TypeReference<Map<Integer, List<MessageTag>>>() { }).readValue(dataNode); } } return Collections.emptyMap(); }
From source file:org.springframework.social.facebook.api.impl.json.StoryTagMapDeserializer.java
@SuppressWarnings("unchecked") @Override//from w w w .j a v a 2 s . co m public Map<Integer, List<StoryTag>> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new FacebookModule()); jp.setCodec(mapper); if (jp.hasCurrentToken()) { JsonNode dataNode = jp.readValueAs(JsonNode.class); if (dataNode != null) { return (Map<Integer, List<StoryTag>>) mapper .reader(new TypeReference<Map<Integer, List<StoryTag>>>() { }).readValue(dataNode); } } return Collections.emptyMap(); }
From source file:eu.trentorise.opendata.commons.jackson.OdtCommonsModule.java
/** * Creates the module and registers all the needed serializers and * deserializers for Odt Commons objects *//* w w w . j a v a 2s .c o m*/ public OdtCommonsModule() { super("odt-commons-jackson", readJacksonVersion(OdtCommonsModule.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)); } }); setMixInAnnotation(LocalizedString.class, JacksonLocalizedString.class); }
From source file:org.agorava.linkedin.jackson.ConnectionAuthorizationDeserializer.java
@Override public ConnectionAuthorization deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { ObjectMapper mapper = BeanResolver.getInstance().resolve(ObjectMapper.class); 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 ava 2s.co m*/ } } throw ctxt.mappingException("Expected JSON object"); }
From source file:org.springframework.social.facebook.api.impl.json.ReferenceListAndCountDeserializer.java
@SuppressWarnings("unchecked") @Override//from w w w.j av a2 s.c o m public ListAndCount<Reference> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new FacebookModule()); 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.springframework.social.facebook.api.impl.json.TagListDeserializer.java
@SuppressWarnings("unchecked") @Override/*ww w . ja v a 2s .c o m*/ public List<Tag> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new FacebookModule()); jp.setCodec(mapper); if (jp.hasCurrentToken()) { JsonNode dataNode = (JsonNode) jp.readValueAs(JsonNode.class).get("data"); return (List<Tag>) mapper.reader(new TypeReference<List<Tag>>() { }).readValue(dataNode); } return null; }
From source file:org.springframework.social.linkedin.api.impl.json.LikesListDeserializer.java
@Override public List<LinkedInProfile> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new LinkedInModule()); jp.setCodec(mapper);/*from w w w . jav a2s . c om*/ if (jp.hasCurrentToken()) { JsonNode dataNode = jp.readValueAs(JsonNode.class).get("values"); List<LinkedInProfile> likes = new ArrayList<LinkedInProfile>(); // Have to iterate through list due to person sub object. for (JsonNode like : dataNode) { LinkedInProfile profile = mapper.reader(new TypeReference<LinkedInProfile>() { }).readValue(like.get("person")); likes.add(profile); } return likes; } return null; }
From source file:org.springframework.social.facebook.api.impl.json.ReferenceListDeserializer.java
@SuppressWarnings("unchecked") @Override//from ww w. j av a 2s. c o m public List<Reference> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new FacebookModule()); 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(); }
From source file:org.rapidoid.db.impl.inmem.JacksonEntitySerializer.java
@SuppressWarnings("rawtypes") private void initDbMapper() { SimpleModule dbModule = new SimpleModule("DbModule", new Version(1, 0, 0, null, null, null)); dbModule.addDeserializer(DbList.class, new JsonDeserializer<DbList>() { @SuppressWarnings("unchecked") @Override/*from w w w . j a v a 2s. c o m*/ public DbList deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { Map<String, Object> data = jp.readValueAs(Map.class); String relation = (String) data.get("relation"); List<? extends Number> ids = (List<Number>) data.get("ids"); return new InMemDbList(db, null, relation, ids); } }); dbModule.addDeserializer(DbSet.class, new JsonDeserializer<DbSet>() { @SuppressWarnings("unchecked") @Override public DbSet deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { Map<String, Object> data = jp.readValueAs(Map.class); String relation = (String) data.get("relation"); List<? extends Number> ids = (List<Number>) data.get("ids"); return new InMemDbSet(db, null, relation, ids); } }); dbModule.addDeserializer(DbRef.class, new JsonDeserializer<DbRef>() { @SuppressWarnings("unchecked") @Override public DbRef deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { Map<String, Object> data = jp.readValueAs(Map.class); String relation = (String) data.get("relation"); List<? extends Number> ids = (List<Number>) data.get("ids"); U.must(ids.size() <= 1, "Expected 0 or 1 IDs!"); long id = !ids.isEmpty() ? ids.get(0).longValue() : -1; return new InMemDbRef(db, null, relation, id); } }); mapper.registerModule(dbModule); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); }
From source file:org.springframework.social.facebook.api.impl.json.CommentListAndCountDeserializer.java
@SuppressWarnings("unchecked") @Override// w w w. j ava 2 s . com public ListAndCount<Comment> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new FacebookModule()); 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(); JsonNode countNode = commentsNode.get("count"); int commentCount = countNode != null ? countNode.intValue() : 0; return new ListAndCount<Comment>(commentsList, commentCount); } return null; }