JSONUtil { public static String getJSONasDefaultPrettyPrintFromString(String obj) { ObjectMapper mapper = new ObjectMapper(); try { Object json = mapper.readValue(obj, Object.class); DefaultPrettyPrinter pp = new DefaultPrettyPrinter(); pp.indentArraysWith(DefaultPrettyPrinter.FixedSpaceIndenter.instance); return mapper.writer(pp).writeValueAsString(json); } catch (JsonProcessingException e) { return TVaultConstants.EMPTY_JSON; } catch (IOException e) { return TVaultConstants.EMPTY_JSON; } }  static String getJSON(Object obj); static String getJSONasDefaultPrettyPrint(Object obj); static String getJSONasDefaultPrettyPrintFromString(String obj); static Object getObj(String jsonStr, Class<?> className);  }
@Test public void test_getJSONasDefaultPrettyPrintFromString() throws Exception { String errorJsonStr="{}"; String str = "{\"username\":\"testuser\",\"password\":\"testuser\"}"; String json = JSONUtil.getJSONasDefaultPrettyPrintFromString(str); assertNotEquals(errorJsonStr, json); }
@Test public void test_getJSONasDefaultPrettyPrintFromString_error() throws Exception { String expectedJsonStr="{}"; String str = "{\"username\":abc\"testuser\",\"password\":\"testuser\"}"; String json = JSONUtil.getJSONasDefaultPrettyPrintFromString(str); assertEquals(expectedJsonStr, json); }