[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:
Rajat Garg
2023-07-23 13:38:21 +05:30
committed by GitHub
parent a1baca99fb
commit 0da3664191
2 changed files with 92 additions and 0 deletions
@@ -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);
}
}