Added feedback from pull request.

This commit is contained in:
Karsten Silz 2020-09-21 20:31:14 +01:00
parent 5e2147e6f4
commit 025791611c
2 changed files with 31 additions and 34 deletions

View File

@ -13,4 +13,3 @@ This module contains articles about JSON.
- [Get a Value by Key in a JSONArray](https://www.baeldung.com/java-jsonarray-get-value-by-key) - [Get a Value by Key in a JSONArray](https://www.baeldung.com/java-jsonarray-get-value-by-key)
- [Iterating Over an Instance of org.json.JSONObject](https://www.baeldung.com/jsonobject-iteration) - [Iterating Over an Instance of org.json.JSONObject](https://www.baeldung.com/jsonobject-iteration)
- [Escape JSON String in Java](https://www.baeldung.com/java-json-escaping) - [Escape JSON String in Java](https://www.baeldung.com/java-json-escaping)
- [Reducing JSON Data Size](https://www.baeldung.com/reducing-json-data-size)

View File

@ -22,11 +22,11 @@ import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.module.SimpleModule;
class JsonOptimizationUnitTest { class JsonOptimizationUnitTest {
private static final String TEST_LABEL_DEFAULT_JSON = "Default JSON"; private static final String TEST_LABEL_JACKSON_DEFAULT_OPTIONS = "Default JSON";
private static final String TEST_LABEL_DEFAULT_JSON_NO_NULL = "Default JSON without null"; private static final String TEST_LABEL_DEFAULT_JSON_NO_NULL = "Default JSON without null";
private static final String TEST_LABEL_SHORT_NAMES = "Shorter field names"; private static final String TEST_LABEL_SHORTER_FIELD_NAMES = "Shorter field names";
private static final String TEST_LABEL_SHORT_NAMES_NO_NULL = "Shorter field names without null"; private static final String TEST_LABEL_SHORTER_FIELD_NAMES_AND_NO_NULL = "Shorter field names without null";
private static final String TEST_LABEL_CUSTOM_SERIALIZER = "Custom serializer"; private static final String TEST_LABEL_SERIALIZING_TO_ARRAY = "Custom serializer";
private static final String TEST_LABEL_SLIM_CUSTOM_SERIALIZER = "Slim custom serializer"; private static final String TEST_LABEL_SLIM_CUSTOM_SERIALIZER = "Slim custom serializer";
private static final String TEST_LABEL_SLIM_CUSTOMER = "Slim customer"; private static final String TEST_LABEL_SLIM_CUSTOMER = "Slim customer";
private static final String TEST_LABEL_SLIM_CUSTOMER_SHORT_NAMES = "Slim customer with shorter field names"; private static final String TEST_LABEL_SLIM_CUSTOMER_SHORT_NAMES = "Slim customer with shorter field names";
@ -53,82 +53,80 @@ class JsonOptimizationUnitTest {
} }
@Test @Test
void testSetUp() { void whenSetUp_ThenOneThousandCustomers() {
assertEquals(1000, customers.length, "There should be a 1000 customers"); assertEquals(1000, customers.length, "There should be a 1000 customers");
} }
@Test @Test
void testDefaultJson() throws IOException { void whenJacksonDefaultOptions_thenValid() throws IOException {
printBanner(TEST_LABEL_DEFAULT_JSON); printBanner(TEST_LABEL_JACKSON_DEFAULT_OPTIONS);
byte[] plainJson = createPlainJson(TEST_LABEL_DEFAULT_JSON, customers); byte[] plainJson = createJsonAndVerify(TEST_LABEL_JACKSON_DEFAULT_OPTIONS, customers);
compressJson(TEST_LABEL_DEFAULT_JSON, plainJson); compressJson(TEST_LABEL_JACKSON_DEFAULT_OPTIONS, plainJson);
} }
@Test @Test
void testDefaultNoNull() throws IOException { void whenExcludingNull_thenValid() throws IOException {
printBanner(TEST_LABEL_DEFAULT_JSON_NO_NULL); printBanner(TEST_LABEL_DEFAULT_JSON_NO_NULL);
mapper.setSerializationInclusion(Include.NON_NULL); mapper.setSerializationInclusion(Include.NON_NULL);
byte[] plainJson = createPlainJson(TEST_LABEL_DEFAULT_JSON_NO_NULL, customers); byte[] plainJson = createJsonAndVerify(TEST_LABEL_DEFAULT_JSON_NO_NULL, customers);
compressJson(TEST_LABEL_DEFAULT_JSON_NO_NULL, plainJson); compressJson(TEST_LABEL_DEFAULT_JSON_NO_NULL, plainJson);
} }
@Test @Test
void testShortNames() throws IOException { void whenShorterFieldNames_thenValid() throws IOException {
printBanner(TEST_LABEL_SHORT_NAMES); printBanner(TEST_LABEL_SHORTER_FIELD_NAMES);
CustomerShortNames[] shorterOnes = CustomerShortNames.fromCustomers(customers); CustomerShortNames[] shorterOnes = CustomerShortNames.fromCustomers(customers);
byte[] shorterJson = createPlainJson(TEST_LABEL_SHORT_NAMES, shorterOnes); byte[] shorterJson = createJsonAndVerify(TEST_LABEL_SHORTER_FIELD_NAMES, shorterOnes);
compressJson(TEST_LABEL_SHORT_NAMES, shorterJson); compressJson(TEST_LABEL_SHORTER_FIELD_NAMES, shorterJson);
} }
@Test @Test
void testShortNamesNoNull() throws IOException { void whenShorterFieldNamesAndExcludingNull_thenValid() throws IOException {
printBanner(TEST_LABEL_SHORT_NAMES_NO_NULL); printBanner(TEST_LABEL_SHORTER_FIELD_NAMES_AND_NO_NULL);
CustomerShortNames[] shorterOnes = CustomerShortNames.fromCustomers(customers); CustomerShortNames[] shorterOnes = CustomerShortNames.fromCustomers(customers);
mapper.setSerializationInclusion(Include.NON_NULL); mapper.setSerializationInclusion(Include.NON_NULL);
byte[] shorterJson = createPlainJson(TEST_LABEL_SHORT_NAMES_NO_NULL, shorterOnes); byte[] shorterJson = createJsonAndVerify(TEST_LABEL_SHORTER_FIELD_NAMES_AND_NO_NULL, shorterOnes);
compressJson(TEST_LABEL_SHORT_NAMES_NO_NULL, shorterJson); compressJson(TEST_LABEL_SHORTER_FIELD_NAMES_AND_NO_NULL, shorterJson);
} }
@Test @Test
void testSlim() throws IOException { void whenSlimCustomer_thenValid() throws IOException {
printBanner(TEST_LABEL_SLIM_CUSTOMER); printBanner(TEST_LABEL_SLIM_CUSTOMER);
CustomerSlim[] slimOnes = CustomerSlim.fromCustomers(customers); CustomerSlim[] slimOnes = CustomerSlim.fromCustomers(customers);
byte[] slimJson = createPlainJson(TEST_LABEL_SLIM_CUSTOMER, slimOnes); byte[] slimJson = createJsonAndVerify(TEST_LABEL_SLIM_CUSTOMER, slimOnes);
compressJson(TEST_LABEL_SLIM_CUSTOMER, slimJson); compressJson(TEST_LABEL_SLIM_CUSTOMER, slimJson);
} }
@Test @Test
void testSlimShortNames() throws IOException { void whenSlimCustomerAndShorterFieldNames_thenValid() throws IOException {
printBanner(TEST_LABEL_SLIM_CUSTOMER_SHORT_NAMES); printBanner(TEST_LABEL_SLIM_CUSTOMER_SHORT_NAMES);
CustomerSlimShortNames[] slimOnes = CustomerSlimShortNames.fromCustomers(customers); CustomerSlimShortNames[] slimOnes = CustomerSlimShortNames.fromCustomers(customers);
byte[] slimJson = createPlainJson(TEST_LABEL_SLIM_CUSTOMER_SHORT_NAMES, slimOnes); byte[] slimJson = createJsonAndVerify(TEST_LABEL_SLIM_CUSTOMER_SHORT_NAMES, slimOnes);
compressJson(TEST_LABEL_SLIM_CUSTOMER_SHORT_NAMES, slimJson); compressJson(TEST_LABEL_SLIM_CUSTOMER_SHORT_NAMES, slimJson);
} }
@Test @Test
void testCustomSerializer() throws IOException { void whenSerializingToArray_thenValid() throws IOException {
printBanner(TEST_LABEL_CUSTOM_SERIALIZER); printBanner(TEST_LABEL_SERIALIZING_TO_ARRAY);
SimpleModule serializer = new SimpleModule("CustomDeSerializer", new Version(1, 0, 0, null, null, null)); SimpleModule serializer = new SimpleModule("CustomDeSerializer", new Version(1, 0, 0, null, null, null));
serializer.addSerializer(Customer.class, new CustomerSerializer()); serializer.addSerializer(Customer.class, new CustomerSerializer());
serializer.addDeserializer(Customer.class, new CustomerDeserializer()); serializer.addDeserializer(Customer.class, new CustomerDeserializer());
mapper.registerModule(serializer); mapper.registerModule(serializer);
byte[] plainJson = createPlainJson(TEST_LABEL_CUSTOM_SERIALIZER, customers); byte[] plainJson = createJsonAndVerify(TEST_LABEL_SERIALIZING_TO_ARRAY, customers);
compressJson(TEST_LABEL_CUSTOM_SERIALIZER, plainJson); compressJson(TEST_LABEL_SERIALIZING_TO_ARRAY, plainJson);
} }
@Test @Test
void testSlimCustomSerializer() throws IOException { void whenSerializingToArrayAndSlimCustomer_thenValid() throws IOException {
printBanner(TEST_LABEL_SLIM_CUSTOM_SERIALIZER); printBanner(TEST_LABEL_SLIM_CUSTOM_SERIALIZER);
SimpleModule serializer = new SimpleModule("SlimCustomDeSerializer", new Version(1, 0, 0, null, null, null)); SimpleModule serializer = new SimpleModule("SlimCustomDeSerializer", new Version(1, 0, 0, null, null, null));
serializer.addSerializer(CustomerSlim.class, new CustomerSlimSerializer()); serializer.addSerializer(CustomerSlim.class, new CustomerSlimSerializer());
serializer.addDeserializer(CustomerSlim.class, new CustomerSlimDeserializer()); serializer.addDeserializer(CustomerSlim.class, new CustomerSlimDeserializer());
mapper.registerModule(serializer); mapper.registerModule(serializer);
CustomerSlim[] slimOnes = CustomerSlim.fromCustomers(customers); CustomerSlim[] slimOnes = CustomerSlim.fromCustomers(customers);
byte[] plainJson = createPlainJson(TEST_LABEL_SLIM_CUSTOM_SERIALIZER, slimOnes); byte[] plainJson = createJsonAndVerify(TEST_LABEL_SLIM_CUSTOM_SERIALIZER, slimOnes);
compressJson(TEST_LABEL_SLIM_CUSTOM_SERIALIZER, plainJson); compressJson(TEST_LABEL_SLIM_CUSTOM_SERIALIZER, plainJson);
} }
@ -153,7 +151,7 @@ class JsonOptimizationUnitTest {
assertTrue(plainJson.length > gzippedJson.length, label + " should be longer than GZIPped data"); assertTrue(plainJson.length > gzippedJson.length, label + " should be longer than GZIPped data");
} }
private byte[] createPlainJson(String label, Object[] customers) throws IOException { private byte[] createJsonAndVerify(String label, Object[] customers) throws IOException {
System.out.println(label + " sample: "); System.out.println(label + " sample: ");
ObjectWriter prettyWritter = mapper.writerWithDefaultPrettyPrinter(); ObjectWriter prettyWritter = mapper.writerWithDefaultPrettyPrinter();
System.out.println(prettyWritter.writeValueAsString(customers[0])); System.out.println(prettyWritter.writeValueAsString(customers[0]));
@ -174,7 +172,7 @@ class JsonOptimizationUnitTest {
System.out.println(label + " file: " + tempFile.toString()); System.out.println(label + " file: " + tempFile.toString());
Object[] restoredOnes = mapper.readValue(feedback, customers.getClass()); Object[] restoredOnes = mapper.readValue(feedback, customers.getClass());
assertArrayEquals(TEST_LABEL_DEFAULT_JSON + ": restoring from JSON should work", customers, restoredOnes); assertArrayEquals(TEST_LABEL_JACKSON_DEFAULT_OPTIONS + ": restoring from JSON should work", customers, restoredOnes);
return feedback; return feedback;
} }