JsonMessageFormatter extends AbstractPluginableElement implements MessageFormatter { @Override public String format(String message) { final ArrayList<SubText> jsonFragments = jsonFinder.findJsonFragments(message); StringBuilder sb = new StringBuilder(); int lastEnd = 0; for (SubText jsonFragment : jsonFragments) { sb.append(message.substring(lastEnd, jsonFragment.getStart())); final String group = jsonFragment.subString(message); String toAppend = group; try { JSONObject o = new JSONObject(group); final String jsonFormatted = o.toString(2); toAppend = jsonFormatted; } catch (JSONException e) { LOGGER.debug("There is no need to format {}", group); } if (!sb.toString().endsWith("\n")) { sb.append("\n"); } sb.append(toAppend).append("\n"); lastEnd = jsonFragment.getEnd(); } sb.append(message.substring(lastEnd)); return sb.toString().trim(); } JsonMessageFormatter(); @Override int getApiVersion(); @Override boolean formattingNeeded(String message); @Override String format(String message); public JsonFinder jsonFinder; }
@Test(dataProvider = "formatData") public void testFormat(String input, String expected, String description) throws Exception { final String format = underTest.format(input); System.out.println("formatted:\n" + format); Assert.assertEquals(format, expected, description); }