JSONUtil { public static <T> T toObject(String json, Class<T> clazz) { T object; try { object = mapper.readValue(json, clazz); } catch (Exception e) { LOGGER.error("JSON String Can't covert to Java Object!"); throw new RuntimeException(e); } return object; }  static String toJSONString(T object); static T toObject(String json, Class<T> clazz);  }
@Test public void testToObject() { JSONObject jsonObject = new JSONObject(1, "bysocket", 33); String jsonStr = JSONUtil.toJSONString(jsonObject); JSONObject resultObject = JSONUtil.toObject(jsonStr, JSONObject.class); Assert.assertEquals(jsonObject.toString(), resultObject.toString()); }
@Test(expected = RuntimeException.class) public void testToObjectError() { JSONUtil.toObject("{int:1}", JSONObject.class); }