List of usage examples for com.fasterxml.jackson.core.type TypeReference TypeReference
protected TypeReference()
From source file:org.springframework.social.linkedin.api.impl.json.RecommendationsListDeserializer.java
public List<Recommendation> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new LinkedInModule()); jp.setCodec(mapper);//from w w w.ja v a2 s. com if (jp.hasCurrentToken()) { JsonNode dataNode = jp.readValueAs(JsonNode.class).get("values"); if (dataNode != null) { return mapper.reader(new TypeReference<List<Recommendation>>() { }).readValue(dataNode); } } return null; }
From source file:com.ning.jetty.core.modules.TestInjection.java
@Test(groups = "fast") public void testJackson() throws Exception { final String country = "France"; final String date = "1998-07-12T00:00:00.000Z"; final String someJson = "{\"country\":\"" + country + "\",\"date\":\"" + date + "\"}"; final Map<String, String> jsonObject = mapper.readValue(someJson, new TypeReference<Map<String, String>>() { });/*from w ww.j a va 2 s. com*/ Assert.assertEquals(jsonObject.get("country"), country); Assert.assertEquals(jsonObject.get("date"), date); Assert.assertEquals(mapper.writeValueAsString(jsonObject), someJson); final WorldCupChampion champion = mapper.readValue(someJson, WorldCupChampion.class); Assert.assertEquals(champion.getCountry(), country); Assert.assertEquals(champion.getDate(), new DateTime(date, DateTimeZone.UTC)); Assert.assertEquals(mapper.writeValueAsString(champion), someJson); }
From source file:io.sprucehill.mandrill.service.MessageService.java
@Override public List<MessageResponse> sendMessage(MessagePayload payload) throws MessageError, IOException { try {// w w w . j a v a 2s . c o m List<MessageResponse> messageResponses = send(payload, new TypeReference<List<MessageResponse>>() { }, MessageError.class); return messageResponses; } catch (MessageError e) { logger.warn("Got MessageError with code {}, name {} and message {} when sending message!", new Object[] { e.getCode().toString(), e.getName(), e.getMessage() }); throw e; } catch (IOException e) { logger.error("Got IOException while sending message!"); throw e; } }
From source file:com.evrythng.java.wrapper.service.UrlBindingService.java
/** * Creates url binding./*from ww w . j av a 2 s .com*/ * * @param binding {@link UrlBinding} instance. * * @return a preconfigured {@link Builder}. */ public Builder<UrlBinding> bindingCreator(final UrlBinding binding) throws EvrythngClientException { return post(PATH_URLS, binding, new TypeReference<UrlBinding>() { }); }
From source file:com.unboundid.scim2.common.ListResponseTestCase.java
/** * Test list response.// ww w . j av a 2s . c om * * @throws Exception If an error occurs. */ @Test public void testListResponse() throws Exception { ListResponse<ObjectNode> listResponse = JsonUtils.getObjectReader() .forType(new TypeReference<ListResponse<ObjectNode>>() { }).readValue("{ \n" + " \"schemas\":[ \n" + " \"urn:ietf:params:scim:api:messages:2.0:ListResponse\"\n" + " ],\n" + // Test required property case-insensitivity " \"totalresults\":2,\n" + " \"startIndex\":1,\n" + // Test case-insensitivity " \"ItemsPerPage\":3,\n" + " \"Resources\":[ \n" + " { \n" + " \"userName\":\"bjensen\"\n" + " },\n" + " { \n" + " \"userName\":\"jsmith\"\n" + " }\n" + " ]\n" + "}"); try { listResponse = JsonUtils.getObjectReader().forType(new TypeReference<ListResponse<ObjectNode>>() { }).readValue("{ \n" + " \"schemas\":[ \n" + " \"urn:ietf:params:scim:api:messages:2.0:ListResponse\"\n" + " ],\n" + // Test missing required property: totalResults " \"startIndex\":1,\n" + // Test case-insensitivity " \"ItemsPerPage\":3,\n" + " \"Resources\":[ \n" + " { \n" + " \"userName\":\"bjensen\"\n" + " },\n" + " { \n" + " \"userName\":\"jsmith\"\n" + " }\n" + " ]\n" + "}"); fail("Expected failure for missing required property 'totalResults'"); } catch (final JsonMappingException je) { assertTrue(je.getMessage().contains("Missing required creator property"), je.getMessage()); } assertEquals(listResponse.getTotalResults(), 2); assertEquals(listResponse.getStartIndex(), Integer.valueOf(1)); assertEquals(listResponse.getItemsPerPage(), Integer.valueOf(3)); assertEquals(listResponse.getResources().size(), 2); ArrayList<ResourceTypeResource> resourceTypeList = new ArrayList<ResourceTypeResource>(); resourceTypeList.add(new ResourceTypeResource("urn:test", "test", "test", new URI("/test"), new URI("urn:test"), Collections.<ResourceTypeResource.SchemaExtension>emptyList())); resourceTypeList.add(new ResourceTypeResource("urn:test2", "test2", "test2", new URI("/test2"), new URI("urn:test2"), Collections.<ResourceTypeResource.SchemaExtension>emptyList())); ListResponse<ResourceTypeResource> response = new ListResponse<ResourceTypeResource>(100, resourceTypeList, 1, 10); String serialized = JsonUtils.getObjectWriter().writeValueAsString(response); assertEquals(JsonUtils.getObjectReader().forType(new TypeReference<ListResponse<ResourceTypeResource>>() { }).readValue(serialized), response); }
From source file:edu.slu.tpen.transfer.JsonImporter.java
public void update(InputStream input) throws IOException, SQLException { Map<String, Object> payload = new ObjectMapper().readValue(input, new TypeReference<Map<String, Object>>() { });// w ww .ja v a 2 s .c o m Map<String, Object> manifest = getObject(payload, "manifest", true); List<Object> canvasses = getArray(manifest, "canvasses", true); if (canvasses.size() != folios.length) { throw new IOException( String.format("Malformed JSON input: %d folios in T-PEN project, but %d canvasses in input.", folios.length, canvasses.size())); } int folioI = 0; for (Object c : canvasses) { if (!(c instanceof Map)) { throw new IOException("Malformed JSON input: canvasses entry is not an object."); } Folio f = folios[folioI++]; Map<String, Object> canvas = (Map<String, Object>) c; String folioURI = getFolioURI(canvas, f); if (folioURI == null) { throw new IOException( String.format("Malformed JSON input: no image match for folio %s.", f.getImageName())); } Transcription[] transcrs = Transcription.getProjectTranscriptions(project.getProjectID(), f.getFolioNumber()); List<Object> rows = getArray(canvas, "rows", true); int transcrI = 0; for (Object r : rows) { if (!(r instanceof Map)) { throw new IOException("Malformed JSON input: rows entry is not an object."); } if (transcrI < transcrs.length) { updateTranscription((Map<String, Object>) r, transcrs[transcrI++]); } else { // Newly inserted lines. insertTranscription((Map<String, Object>) r, f); } } // Delete any trailing transcription lines which no longer corresponde to rows. while (transcrI < transcrs.length) { removeTranscription(transcrs[transcrI]); transcrI++; } folioI++; } }
From source file:com.basistech.rosette.dm.json.array.ListAttributeTest.java
@Test public void list() throws Exception { ListAttribute.Builder<Sentence> listBuilder = new ListAttribute.Builder<>(Sentence.class); listBuilder.add(new Sentence.Builder(0, 10).build()); listBuilder.add(new Sentence.Builder(10, 20).build()); listBuilder.extendedProperty("ek", "ev"); ListAttribute<Sentence> sentList = listBuilder.build(); String json = objectMapper().writeValueAsString(sentList); ListAttribute<Sentence> readBack = objectMapper().readValue(json, new TypeReference<ListAttribute<Sentence>>() { });//from w w w .ja va 2 s .co m // well, try round-trip. // this is the simple case, we'll also have to try it inside an AnnotatedText in another test case. assertEquals(sentList, readBack); }
From source file:com.simiacryptus.util.io.IOUtil.java
/** * Read json t.//from ww w .j av a 2 s.c o m * * @param <T> the type parameter * @param file the file * @return the t */ public static <T> T readJson(File file) { try { return objectMapper.readValue(new String(Files.readAllBytes(file.toPath())), new TypeReference<T>() { }); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:org.agorava.linkedin.jackson.RecommendationsListDeserializer.java
@Override public List<Recommendation> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { ObjectMapper mapper = BeanResolver.getInstance().resolve(ObjectMapper.class); jp.setCodec(mapper);/* w w w .j a v a2 s .co m*/ if (jp.hasCurrentToken()) { JsonNode dataNode = jp.readValueAs(JsonNode.class).get("values"); if (dataNode != null) { return mapper.reader(new TypeReference<List<Recommendation>>() { }).readValue(dataNode); } } return null; }
From source file:javaslang.jackson.datatype.PolymorphicTypesTest.java
@Test public void deserializeOptionA() throws IOException { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new JavaslangModule()); Option<I> i = mapper.readerFor(new TypeReference<Option<I>>() { }).readValue("{\"type\":\"a\"}"); Assert.assertTrue(i.isDefined());// www. ja v a 2 s.c o m Assert.assertTrue(i.get() instanceof A); }