[BAEL-6494] Add functionality to minify a JSON string (#14451)
* [BAEL-6494] Add functionality to minify JSON * [BAEL-6494] Address review comment --------- Co-authored-by: rajatgarg <rajatgarg@adobe.com>
This commit is contained in:
parent
a1baca99fb
commit
0da3664191
|
@ -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<String> {
|
||||||
|
@Override
|
||||||
|
public JsonElement serialize(String src, Type typeOfSrc, JsonSerializationContext context) {
|
||||||
|
return new JsonPrimitive(src.trim());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue