[BAEL-6506] Add utility for Pretty-Print JSON (#14378)

* Add utility for  Pretty-Print JSON

* Remove accidental change

* Address review comments

* Address review comments

---------

Co-authored-by: rajatgarg <rajatgarg@adobe.com>
This commit is contained in:
Rajat Garg 2023-07-11 00:17:36 +05:30 committed by GitHub
parent dcb08e1495
commit 89d83168ae
2 changed files with 96 additions and 0 deletions

View File

@ -0,0 +1,34 @@
package core;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
/*
* Class to print string JSON to well-formatted JSON using Jackson and Gson.
*/
public class JsonPrettyPrinter {
private ObjectMapper mapper = new ObjectMapper();
public String prettyPrintJsonUsingDefaultPrettyPrinter(String uglyJsonString) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
Object jsonObject = objectMapper.readValue(uglyJsonString, Object.class);
return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObject);
}
public String prettyPrintUsingGlobalSetting(String uglyJsonString) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
Object jsonObject = mapper.readValue(uglyJsonString, Object.class);
return mapper.writeValueAsString(jsonObject);
}
public String prettyPrintUsingGson(String uglyJsonString) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Object jsonObject = gson.fromJson(uglyJsonString, Object.class);
return gson.toJson(jsonObject);
}
}

View File

@ -0,0 +1,62 @@
package core;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
public class JsonPrettyPrinterUnitTest {
private String uglyJsonString = "{\"one\":\"AAA\",\"two\":[\"BBB\",\"CCC\"],\"three\":{\"four\":\"DDD\",\"five\":[\"EEE\",\"FFF\"]}}";
private core.JsonPrettyPrinter jsonPrettyPrinter = new core.JsonPrettyPrinter();
@Test
public void shouldPrettyPrintJsonStringUsingDefaultPrettyPrinter() throws JsonProcessingException {
String formattedJsonString = jsonPrettyPrinter.prettyPrintJsonUsingDefaultPrettyPrinter(uglyJsonString);
String expectedJson = "{\n" +
" \"one\" : \"AAA\",\n" +
" \"two\" : [ \"BBB\", \"CCC\" ],\n" +
" \"three\" : {\n" +
" \"four\" : \"DDD\",\n" +
" \"five\" : [ \"EEE\", \"FFF\" ]\n" +
" }\n" +
"}";
assertEquals(expectedJson, formattedJsonString);
}
@Test
public void shouldPrettyPrintJsonStringUsingGlobalSetting() throws JsonProcessingException {
String formattedJsonString = jsonPrettyPrinter.prettyPrintUsingGlobalSetting(uglyJsonString);
String expectedJson = "{\n" +
" \"one\" : \"AAA\",\n" +
" \"two\" : [ \"BBB\", \"CCC\" ],\n" +
" \"three\" : {\n" +
" \"four\" : \"DDD\",\n" +
" \"five\" : [ \"EEE\", \"FFF\" ]\n" +
" }\n" +
"}";
assertEquals(expectedJson, formattedJsonString);
}
@Test
public void shouldPrettyPrintJsonStringUsingGson() {
String formattedJsonString = jsonPrettyPrinter.prettyPrintUsingGson(uglyJsonString);
String expectedPrettyJson = "{\n" +
" \"one\": \"AAA\",\n" +
" \"two\": [\n" +
" \"BBB\",\n" +
" \"CCC\"\n" +
" ],\n" +
" \"three\": {\n" +
" \"four\": \"DDD\",\n" +
" \"five\": [\n" +
" \"EEE\",\n" +
" \"FFF\"\n" +
" ]\n" +
" }\n" +
"}";
assertEquals(expectedPrettyJson, formattedJsonString);
}
}