JSONUtils { @Deprecated public static <T> T read(final String json, final Class<T> type) { try { return OBJECT_MAPPER.readValue(json, type); } catch (Exception e) { throw new WrappedRuntimeException(e); } } private  JSONUtils(); @Deprecated static T read(final String json, final Class<T> type); static T read(final String json, final TypeReference<T> type); @Deprecated static T read(final InputStream inputStream, final TypeReference<T> type); @Deprecated static T read(final InputStream inputStream, final Class<T> valueType); static String write(final Object object); static ObjectMapper createObjectMapper(); static final ObjectMapper OBJECT_MAPPER; }
@Test public void deserialize() { String json = "{\"myProp\":\" 1 \",\"b\":2, \"sub\": {\"x\":14,\"y\":33} }"; MyObject o = JSONUtils.read(json, new TypeReference<MyObject>() { }); Assert.assertEquals("1", o.getMyProp()); Assert.assertEquals(Integer.valueOf(2), o.getB()); Assert.assertNotNull(o.getSub()); Assert.assertSame(MySubObject.class, o.getSub().getClass()); Assert.assertEquals(Integer.valueOf(14), o.getSub().getX()); Assert.assertEquals(Integer.valueOf(33), o.getSub().getY()); }
@Test public void deserializeLess() { String json = "{\"myProp\":\"1\" }"; MyObject o = JSONUtils.read(json, new TypeReference<MyObject>() { }); Assert.assertEquals("1", o.getMyProp()); Assert.assertNull(o.getB()); Assert.assertNull(o.getSub()); }
@Test public void deserializeLessInSubObject() { String json = "{\"myProp\":\"1\",\"b\":2, \"sub\": {\"y\":33} }"; MyObject o = JSONUtils.read(json, new TypeReference<MyObject>() { }); Assert.assertEquals("1", o.getMyProp()); Assert.assertEquals(Integer.valueOf(2), o.getB()); Assert.assertNotNull(o.getSub()); Assert.assertSame(MySubObject.class, o.getSub().getClass()); Assert.assertNull(o.getSub().getX()); Assert.assertEquals(Integer.valueOf(33), o.getSub().getY()); }
@Test public void deserializeNoneInSubObject() { String json = "{\"myProp\":\"1\",\"b\":2, \"sub\": {} }"; MyObject o = JSONUtils.read(json, new TypeReference<MyObject>() { }); Assert.assertEquals("1", o.getMyProp()); Assert.assertEquals(Integer.valueOf(2), o.getB()); Assert.assertNotNull(o.getSub()); Assert.assertSame(MySubObject.class, o.getSub().getClass()); Assert.assertNull(o.getSub().getX()); Assert.assertNull(o.getSub().getY()); }
@Test public void deserializeMore() { String json = "{\"myProp\":\"1\",\"b\":2,\"sub\": {\"x\":14,\"y\":33},\"unexpected\":\"3\", \"w\":{\"y\":42}}"; MyObject o = JSONUtils.read(json, new TypeReference<MyObject>() { }); Assert.assertEquals("1", o.getMyProp()); Assert.assertEquals(Integer.valueOf(2), o.getB()); Assert.assertNotNull(o.getSub()); Assert.assertSame(MySubObject.class, o.getSub().getClass()); Assert.assertEquals(Integer.valueOf(14), o.getSub().getX()); Assert.assertEquals(Integer.valueOf(33), o.getSub().getY()); }
@Test public void deserializeMoreInSubObject() { String json = "{\"myProp\":\"1\",\"b\":2, \"sub\": {\"x\":14,\"y\":33,\"u\":12} }"; MyObject o = JSONUtils.read(json, new TypeReference<MyObject>() { }); Assert.assertEquals("1", o.getMyProp()); Assert.assertEquals(Integer.valueOf(2), o.getB()); Assert.assertNotNull(o.getSub()); Assert.assertSame(MySubObject.class, o.getSub().getClass()); Assert.assertEquals(Integer.valueOf(14), o.getSub().getX()); Assert.assertEquals(Integer.valueOf(33), o.getSub().getY()); }
@Test(expected = Exception.class) public void deserializeBadType() { String json = "{\"myProp\":\"1\",\"b\":\"a\", \"sub\": {\"x\":14,\"y\":33} }"; JSONUtils.read(json, new TypeReference<MyObject>() { }); }
@Test(expected = Exception.class) public void deserializeBadSubType() { String json = "{\"myProp\":\"1\",\"b\":2, \"sub\": {\"x\":\"a\",\"y\":33} }"; JSONUtils.read(json, new TypeReference<MyObject>() { }); }