Example usage for org.json JSONFormatter format

List of usage examples for org.json JSONFormatter format

Introduction

In this page you can find the example usage for org.json JSONFormatter format.

Prototype

public static String format(final JSONObject object) throws JSONException 

Source Link

Usage

From source file:cc.redpen.formatter.JSONFormatterTest.java

@Test
public void testFormatDocumentsAndErrors() throws RedPenException, JSONException {
    JSONFormatter formatter = new JSONFormatter();
    List<ValidationError> errors = new ArrayList<>();
    setErrorList(errors);//from w  ww.j av a 2 s. c om
    addLocalizedError(new Sentence("testing JSONFormatter", 1));
    Document document = Document.builder().setFileName("docName").build();
    Map<Document, List<ValidationError>> documentListMap = new HashMap<>();
    documentListMap.put(document, errors);

    String result = formatter.format(documentListMap);
    assertEquals(
            "[{\"document\":\"docName\",\"errors\":[{\"sentence\":\"testing JSONFormatter\",\"validator\":\"JSONFormatterTest\",\"lineNum\":1,\"sentenceStartColumnNum\":0,\"message\":\"json test error\"}]}]",
            result);
    JSONArray jsonArray = new JSONArray(result);
    assertTrue(jsonArray.length() == 1);
    JSONObject jsonObject = jsonArray.getJSONObject(0);

    String docName = jsonObject.getString("document");
    assertEquals("docName", docName);
    JSONArray jsonErrors = jsonObject.getJSONArray("errors");
    assertNotNull(jsonErrors);
    assertEquals(1, jsonErrors.length());
    assertEquals("testing JSONFormatter", jsonErrors.getJSONObject(0).getString("sentence"));
    assertEquals("json test error", jsonErrors.getJSONObject(0).getString("message"));
    assertEquals(1, jsonErrors.getJSONObject(0).getInt("lineNum"));
}