diff --git a/json-modules/json-2/src/main/java/com/baeldung/jsonminifier/JsonMinifier.java b/json-modules/json-2/src/main/java/com/baeldung/jsonminifier/JsonMinifier.java new file mode 100644 index 0000000000..d402e8b1a1 --- /dev/null +++ b/json-modules/json-2/src/main/java/com/baeldung/jsonminifier/JsonMinifier.java @@ -0,0 +1,58 @@ +package com.baeldung.jsonminifier; + +import java.lang.reflect.Type; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonElement; +import com.google.gson.JsonPrimitive; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; + +public class JsonMinifier { + public String removeExtraWhitespace(String json) { + StringBuilder result = new StringBuilder(json.length()); + boolean inQuotes = false; + boolean escapeMode = false; + + for (char character : json.toCharArray()) { + if (escapeMode) { + result.append(character); + escapeMode = false; + } else if (character == '"') { + inQuotes = !inQuotes; + result.append(character); + } else if (character == '\\') { + escapeMode = true; + result.append(character); + } else if (!inQuotes && character == ' ') { + continue; + } else { + result.append(character); + } + } + + return result.toString(); + } + + public String removeExtraWhitespaceUsingJackson(String json) throws Exception { + ObjectMapper objectMapper = new ObjectMapper(); + JsonNode jsonNode = objectMapper.readTree(json); + return objectMapper.writeValueAsString(jsonNode); + } + + public String removeWhitespacesUsingGson(String json) { + Gson gson = new GsonBuilder().registerTypeAdapter(String.class, new StringSerializer()).create(); + JsonElement jsonElement = gson.fromJson(json, JsonElement.class); + return gson.toJson(jsonElement); + } + + class StringSerializer implements JsonSerializer { + @Override + public JsonElement serialize(String src, Type typeOfSrc, JsonSerializationContext context) { + return new JsonPrimitive(src.trim()); + } + } +} diff --git a/json-modules/json-2/src/test/java/com/baeldung/jsonminifier/JsonMinifierUnitTest.java b/json-modules/json-2/src/test/java/com/baeldung/jsonminifier/JsonMinifierUnitTest.java new file mode 100644 index 0000000000..056e3429bd --- /dev/null +++ b/json-modules/json-2/src/test/java/com/baeldung/jsonminifier/JsonMinifierUnitTest.java @@ -0,0 +1,34 @@ +package com.baeldung.jsonminifier; + +import static org.junit.Assert.assertEquals; +import org.junit.jupiter.api.Test; + +public class JsonMinifierUnitTest { + private JsonMinifier jsonMinifier = new JsonMinifier(); + private String inputJson = "{ \"name\" : \"John\" , \"address\" : \"New York\", \"age\" : 30 , \"phoneNumber\" : 9999999999 }"; + + + @Test + public void givenWhiteSpaceRemoval_whenJsonContainsWhitespaces_thenWhitespaceRemoved() { + String expectedJson = "{\"name\":\"John\",\"address\":\"New York\",\"age\":30,\"phoneNumber\":9999999999}"; + String result = jsonMinifier.removeExtraWhitespace(inputJson); + System.out.println(result); + assertEquals(expectedJson, result); + } + + @Test + public void givenWhiteSpaceRemovalUsingJackson_whenJsonContainsWhitespaces_thenWhitespaceRemoved() throws Exception { + String expectedJson = "{\"name\":\"John\",\"address\":\"New York\",\"age\":30,\"phoneNumber\":9999999999}"; + String result = jsonMinifier.removeExtraWhitespaceUsingJackson(inputJson); + System.out.println(result); + assertEquals(expectedJson, result); + } + + @Test + public void givenWhiteSpaceRemovalUsingGson_whenJsonContainsWhitespaces_thenWhitespaceRemoved() { + String expectedJson = "{\"name\":\"John\",\"address\":\"New York\",\"age\":30,\"phoneNumber\":9999999999}"; + String result = jsonMinifier.removeWhitespacesUsingGson(inputJson); + System.out.println(result); + assertEquals(expectedJson, result); + } +}