@Test public void fromJson() throws Exception { String beanString = "{\"name\":\"A\"}"; TestBean bean = JsonMapper.INSTANCE.fromJson(beanString, TestBean.class); System.out.println("Bean:" + bean); String mapString = "{\"name\":\"A\",\"age\":2}"; Map<String, Object> map = JsonMapper.INSTANCE.fromJson(mapString, HashMap.class); System.out.println("Map:"); for (Entry<String, Object> entry : map.entrySet()) { System.out.println(entry.getKey() + " " + entry.getValue()); } String listString = "[\"A\",\"B\",\"C\"]"; List<String> stringList = JsonMapper.INSTANCE.fromJson(listString, List.class); System.out.println("String List:"); for (String element : stringList) { System.out.println(element); } String beanListString = "[{\"name\":\"A\"},{\"name\":\"B\"}]"; List<TestBean> beanList = JsonMapper.INSTANCE.fromJson(beanListString, JsonMapper.INSTANCE.buildCollectionType(List.class, TestBean.class)); System.out.println("Bean List:"); for (TestBean element : beanList) { System.out.println(element); } }
public <T> T fromJson(@Nullable String jsonString, Class<T> clazz) { if (StringUtils.isEmpty(jsonString)) { return null; } try { return mapper.readValue(jsonString, clazz); } catch (IOException e) { logger.warn("parse json string error:" + jsonString, e); return null; } }
JsonMapper { public <T> T fromJson(@Nullable String jsonString, Class<T> clazz) { if (StringUtils.isEmpty(jsonString)) { return null; } try { return mapper.readValue(jsonString, clazz); } catch (IOException e) { logger.warn("parse json string error:" + jsonString, e); return null; } } }
JsonMapper { public <T> T fromJson(@Nullable String jsonString, Class<T> clazz) { if (StringUtils.isEmpty(jsonString)) { return null; } try { return mapper.readValue(jsonString, clazz); } catch (IOException e) { logger.warn("parse json string error:" + jsonString, e); return null; } } JsonMapper();  JsonMapper(Include include); }
JsonMapper { public <T> T fromJson(@Nullable String jsonString, Class<T> clazz) { if (StringUtils.isEmpty(jsonString)) { return null; } try { return mapper.readValue(jsonString, clazz); } catch (IOException e) { logger.warn("parse json string error:" + jsonString, e); return null; } } JsonMapper();  JsonMapper(Include include); static JsonMapper nonNullMapper(); static JsonMapper nonEmptyMapper(); static JsonMapper defaultMapper(); String toJson(Object object); T fromJson(@Nullable String jsonString, Class<T> clazz); T fromJson(@Nullable String jsonString, JavaType javaType); JavaType buildCollectionType(Class<? extends Collection> collectionClass, Class<?> elementClass); JavaType buildMapType(Class<? extends Map> mapClass, Class<?> keyClass, Class<?> valueClass); void update(String jsonString, Object object); String toJsonP(String functionName, Object object); void enableEnumUseToString(); ObjectMapper getMapper(); }
JsonMapper { public <T> T fromJson(@Nullable String jsonString, Class<T> clazz) { if (StringUtils.isEmpty(jsonString)) { return null; } try { return mapper.readValue(jsonString, clazz); } catch (IOException e) { logger.warn("parse json string error:" + jsonString, e); return null; } } JsonMapper();  JsonMapper(Include include); static JsonMapper nonNullMapper(); static JsonMapper nonEmptyMapper(); static JsonMapper defaultMapper(); String toJson(Object object); T fromJson(@Nullable String jsonString, Class<T> clazz); T fromJson(@Nullable String jsonString, JavaType javaType); JavaType buildCollectionType(Class<? extends Collection> collectionClass, Class<?> elementClass); JavaType buildMapType(Class<? extends Map> mapClass, Class<?> keyClass, Class<?> valueClass); void update(String jsonString, Object object); String toJsonP(String functionName, Object object); void enableEnumUseToString(); ObjectMapper getMapper(); static final JsonMapper INSTANCE; }