Skip to content

Commit 3e58e24

Browse files
rajat-gargrajatgarg
andauthored
[BAEL-6494] Add functionality to minify a JSON string (eugenp#14451)
* [BAEL-6494] Add functionality to minify JSON * [BAEL-6494] Address review comment --------- Co-authored-by: rajatgarg <[email protected]>
1 parent 37f883a commit 3e58e24

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.jsonminifier;
2+
3+
import java.lang.reflect.Type;
4+
5+
import com.fasterxml.jackson.databind.JsonNode;
6+
import com.fasterxml.jackson.databind.ObjectMapper;
7+
import com.google.gson.Gson;
8+
import com.google.gson.GsonBuilder;
9+
import com.google.gson.JsonElement;
10+
import com.google.gson.JsonPrimitive;
11+
import com.google.gson.JsonSerializationContext;
12+
import com.google.gson.JsonSerializer;
13+
14+
public class JsonMinifier {
15+
public String removeExtraWhitespace(String json) {
16+
StringBuilder result = new StringBuilder(json.length());
17+
boolean inQuotes = false;
18+
boolean escapeMode = false;
19+
20+
for (char character : json.toCharArray()) {
21+
if (escapeMode) {
22+
result.append(character);
23+
escapeMode = false;
24+
} else if (character == '"') {
25+
inQuotes = !inQuotes;
26+
result.append(character);
27+
} else if (character == '\\') {
28+
escapeMode = true;
29+
result.append(character);
30+
} else if (!inQuotes && character == ' ') {
31+
continue;
32+
} else {
33+
result.append(character);
34+
}
35+
}
36+
37+
return result.toString();
38+
}
39+
40+
public String removeExtraWhitespaceUsingJackson(String json) throws Exception {
41+
ObjectMapper objectMapper = new ObjectMapper();
42+
JsonNode jsonNode = objectMapper.readTree(json);
43+
return objectMapper.writeValueAsString(jsonNode);
44+
}
45+
46+
public String removeWhitespacesUsingGson(String json) {
47+
Gson gson = new GsonBuilder().registerTypeAdapter(String.class, new StringSerializer()).create();
48+
JsonElement jsonElement = gson.fromJson(json, JsonElement.class);
49+
return gson.toJson(jsonElement);
50+
}
51+
52+
class StringSerializer implements JsonSerializer<String> {
53+
@Override
54+
public JsonElement serialize(String src, Type typeOfSrc, JsonSerializationContext context) {
55+
return new JsonPrimitive(src.trim());
56+
}
57+
}
58+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.jsonminifier;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class JsonMinifierUnitTest {
7+
private JsonMinifier jsonMinifier = new JsonMinifier();
8+
private String inputJson = "{ \"name\" : \"John\" , \"address\" : \"New York\", \"age\" : 30 , \"phoneNumber\" : 9999999999 }";
9+
10+
11+
@Test
12+
public void givenWhiteSpaceRemoval_whenJsonContainsWhitespaces_thenWhitespaceRemoved() {
13+
String expectedJson = "{\"name\":\"John\",\"address\":\"New York\",\"age\":30,\"phoneNumber\":9999999999}";
14+
String result = jsonMinifier.removeExtraWhitespace(inputJson);
15+
System.out.println(result);
16+
assertEquals(expectedJson, result);
17+
}
18+
19+
@Test
20+
public void givenWhiteSpaceRemovalUsingJackson_whenJsonContainsWhitespaces_thenWhitespaceRemoved() throws Exception {
21+
String expectedJson = "{\"name\":\"John\",\"address\":\"New York\",\"age\":30,\"phoneNumber\":9999999999}";
22+
String result = jsonMinifier.removeExtraWhitespaceUsingJackson(inputJson);
23+
System.out.println(result);
24+
assertEquals(expectedJson, result);
25+
}
26+
27+
@Test
28+
public void givenWhiteSpaceRemovalUsingGson_whenJsonContainsWhitespaces_thenWhitespaceRemoved() {
29+
String expectedJson = "{\"name\":\"John\",\"address\":\"New York\",\"age\":30,\"phoneNumber\":9999999999}";
30+
String result = jsonMinifier.removeWhitespacesUsingGson(inputJson);
31+
System.out.println(result);
32+
assertEquals(expectedJson, result);
33+
}
34+
}

0 commit comments

Comments
 (0)