JacksonObjectMapper implements ObjectMapper { @Override public <T> T readValue(String value, Class<T> valueType) { try { return om.readValue(value, valueType); } catch (IOException e) { throw new UnirestException(e); } } JacksonObjectMapper();  JacksonObjectMapper(com.fasterxml.jackson.databind.ObjectMapper om); @Override T readValue(String value, Class<T> valueType); @Override T readValue(String value, GenericType<T> genericType); @Override String writeValue(Object value);  }
@Test public void canRead(){ TestMe test = om.readValue("{\"text\":\"foo\",\"nmbr\":42,\"another\":{\"text\":\"bar\",\"nmbr\":666,\"another\":null}}", TestMe.class); assertEquals("foo", test.text); assertEquals(42, test.nmbr); assertEquals("bar", test.another.text); assertEquals(666, test.another.nmbr); assertEquals(null, test.another.another); }
@Test public void canReadGenerics(){ List<TestMe> testList = om.readValue("[{\"text\":\"foo\",\"nmbr\":42,\"another\":{\"text\":\"bar\",\"nmbr\":666,\"another\":null}}]", new GenericType<List<TestMe>>(){}); TestMe test = testList.get(0); assertEquals("foo", test.text); assertEquals(42, test.nmbr); assertEquals("bar", test.another.text); assertEquals(666, test.another.nmbr); assertEquals(null, test.another.another); }
@Test public void configSoItFails() { ObjectMapper jom = new ObjectMapper(); jom.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true); JacksonObjectMapper j = new JacksonObjectMapper(jom); try { j.readValue("{\"something\": [1,2,3] }", TestMe.class); fail("Should have thrown"); }catch (Exception e) { assertEquals("com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field \"something\" (class kong.unirest.jackson.JacksonObjectMapperTest$TestMe), not marked as ignorable (3 known properties: \"another\", \"text\", \"nmbr\"])\n" + " at [Source: (String)\"{\"something\": [1,2,3] }\"; line: 1, column: 16] (through reference chain: kong.unirest.jackson.JacksonObjectMapperTest$TestMe[\"something\"])", e.getMessage()); } }