JSONUtil { public static <T> String toJSONString(T object) { String jsonStr; try { jsonStr = mapper.writeValueAsString(object); } catch (Exception e) { LOGGER.error("Java Object Can't covert to JSON String!"); throw new RuntimeException(e); } return jsonStr; }  static String toJSONString(T object); static T toObject(String json, Class<T> clazz);  }
@Test public void testToJSONString() { JSONObject jsonObject = new JSONObject(1, "bysocket", 33); String jsonStr = JSONUtil.toJSONString(jsonObject); Assert.assertEquals("{\"age\":1,\"name\":\"bysocket\",\"id\":33}", jsonStr); }
@Test(expected = RuntimeException.class) public void testToJSONStringError() { JSONUtil.toJSONString(System.out); }