JSON { @Nullable public static <T> T fromJSON(Type instanceType, String json) { try { JavaType javaType = OBJECT_MAPPER.getTypeFactory().constructType(instanceType); return OBJECT_MAPPER.readValue(json, javaType); } catch (IOException e) { throw new UncheckedIOException(e); } }  @Nullable static T fromJSON(Type instanceType, String json); @Nullable static T fromJSON(Class<T> instanceClass, String json); static String toJSON(@Nullable Object instance); static T fromEnumValue(Class<T> valueClass, String jsonValue); static String toEnumValue(T value);  }
@Test void dateFieldFromJavaScript() { TestBean bean = JSON.fromJSON(TestBean.class, "{\"date\": \"2018-05-10T05:42:09.776Z\", \"dateTime\": \"2018-05-10T05:42:09.776Z\", \"zonedDateTime\": \"2018-05-10T05:42:09.776Z\"}"); assertThat(bean.dateField).isEqualTo("2018-05-10"); assertThat(bean.dateTimeField).isEqualTo("2018-05-10T05:42:09.776"); assertThat(bean.zonedDateTimeField).isEqualTo("2018-05-10T05:42:09.776Z"); }
@Test void listObject() { List<TestBean> beans = JSON.fromJSON(Types.list(TestBean.class), "[{\"string\":\"n1\"},{\"string\":\"n2\"}]"); assertThat(beans).hasSize(2); assertThat(beans.get(0).stringField).isEqualTo("n1"); assertThat(beans.get(1).stringField).isEqualTo("n2"); }
@Test void defaultValue() { TestBean bean = JSON.fromJSON(TestBean.class, "{}"); assertThat(bean.defaultValueField).isEqualTo("defaultValue"); }
@Test void invalidJSON() { assertThatThrownBy(() -> JSON.fromJSON(TestBean.class, "{")) .isInstanceOf(UncheckedIOException.class); assertThatThrownBy(() -> JSON.fromJSON(Types.list(TestBean.class), "{")) .isInstanceOf(UncheckedIOException.class); }