diff --git a/json-modules/json-2/src/main/java/com/baeldung/jsonprettyprinter/JsonPrettyPrinter.java b/json-modules/json-2/src/main/java/com/baeldung/jsonprettyprinter/JsonPrettyPrinter.java new file mode 100644 index 0000000000..cb6376515c --- /dev/null +++ b/json-modules/json-2/src/main/java/com/baeldung/jsonprettyprinter/JsonPrettyPrinter.java @@ -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); + } +} diff --git a/json-modules/json-2/src/test/java/com/baeldung/jsonprettyprinter/JsonPrettyPrinterUnitTest.java b/json-modules/json-2/src/test/java/com/baeldung/jsonprettyprinter/JsonPrettyPrinterUnitTest.java new file mode 100644 index 0000000000..03f9fa767b --- /dev/null +++ b/json-modules/json-2/src/test/java/com/baeldung/jsonprettyprinter/JsonPrettyPrinterUnitTest.java @@ -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); + } +}