diff --git a/json/src/main/java/com/baeldung/jsonoptimization/CustomerNoNull.java b/json/src/main/java/com/baeldung/jsonoptimization/CustomerNoNull.java deleted file mode 100644 index 62cf526f78..0000000000 --- a/json/src/main/java/com/baeldung/jsonoptimization/CustomerNoNull.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.baeldung.jsonoptimization; - -import com.fasterxml.jackson.annotation.JsonInclude; - -@JsonInclude(JsonInclude.Include.NON_NULL) -public class CustomerNoNull extends Customer { - - @Override - public String toString() { - return "CustomerNoNull [toString()=" + super.toString() + "]"; - } - - public static CustomerNoNull[] fromCustomers(Customer[] customers) { - CustomerNoNull[] feedback = new CustomerNoNull[customers.length]; - - for (int i = 0; i < customers.length; i++) { - Customer aCustomer = customers[i]; - CustomerNoNull newOne = new CustomerNoNull(); - - newOne.setId(aCustomer.getId()); - newOne.setFirstName(aCustomer.getFirstName()); - newOne.setLastName(aCustomer.getLastName()); - newOne.setStreet(aCustomer.getStreet()); - newOne.setCity(aCustomer.getCity()); - newOne.setPostalCode(aCustomer.getPostalCode()); - newOne.setState(aCustomer.getState()); - newOne.setPhoneNumber(aCustomer.getPhoneNumber()); - newOne.setEmail(aCustomer.getEmail()); - - feedback[i] = newOne; - } - - return feedback; - } - -} diff --git a/json/src/main/java/com/baeldung/jsonoptimization/CustomerShortNamesNoNull.java b/json/src/main/java/com/baeldung/jsonoptimization/CustomerShortNamesNoNull.java deleted file mode 100644 index 1b62fb2c06..0000000000 --- a/json/src/main/java/com/baeldung/jsonoptimization/CustomerShortNamesNoNull.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.baeldung.jsonoptimization; - -import com.fasterxml.jackson.annotation.JsonInclude; - -@JsonInclude(JsonInclude.Include.NON_NULL) -public class CustomerShortNamesNoNull extends CustomerShortNames { - - @Override - public String toString() { - return "CustomerShortNamesNoNull [toString()=" + super.toString() + "]"; - } - - public static CustomerShortNamesNoNull[] fromCustomers(Customer[] customers) { - CustomerShortNamesNoNull[] feedback = new CustomerShortNamesNoNull[customers.length]; - - for (int i = 0; i < customers.length; i++) { - Customer aCustomer = customers[i]; - CustomerShortNamesNoNull newOne = new CustomerShortNamesNoNull(); - - newOne.setId(aCustomer.getId()); - newOne.setFirstName(aCustomer.getFirstName()); - newOne.setLastName(aCustomer.getLastName()); - newOne.setStreet(aCustomer.getStreet()); - newOne.setCity(aCustomer.getCity()); - newOne.setPostalCode(aCustomer.getPostalCode()); - newOne.setState(aCustomer.getState()); - newOne.setPhoneNumber(aCustomer.getPhoneNumber()); - newOne.setEmail(aCustomer.getEmail()); - - feedback[i] = newOne; - } - - return feedback; - } - -} diff --git a/json/src/test/java/com/baeldung/jsonoptimization/JsonOptimizationUnitTest.java b/json/src/test/java/com/baeldung/jsonoptimization/JsonOptimizationUnitTest.java index e9bd044869..5734518fad 100644 --- a/json/src/test/java/com/baeldung/jsonoptimization/JsonOptimizationUnitTest.java +++ b/json/src/test/java/com/baeldung/jsonoptimization/JsonOptimizationUnitTest.java @@ -15,6 +15,7 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.core.Version; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectWriter; @@ -23,14 +24,14 @@ import com.fasterxml.jackson.databind.module.SimpleModule; class JsonOptimizationUnitTest { private static final String TEST_LABEL_DEFAULT_JSON = "Default JSON"; private static final String TEST_LABEL_DEFAULT_JSON_NO_NULL = "Default JSON without null"; - private static final String TEST_LABEL_SHORT_NAMES = "Shorter attribute names"; - private static final String TEST_LABEL_SHORT_NAMES_NO_NULL = "Shorter attribute names without null"; + private static final String TEST_LABEL_SHORT_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_CUSTOM_SERIALIZER = "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_SHORT_NAMES = "Slim customer with shorter attribute names"; - private static DecimalFormat LENGTH_FORMATTER = new DecimalFormat("###,###"); - private static DecimalFormat PERCENT_FORMATTER = new DecimalFormat("###"); + private static final String TEST_LABEL_SLIM_CUSTOMER_SHORT_NAMES = "Slim customer with shorter field names"; + private static DecimalFormat LENGTH_FORMATTER = new DecimalFormat("###,###.0"); + private static DecimalFormat PERCENT_FORMATTER = new DecimalFormat("###.0"); private static Customer[] customers; private ObjectMapper mapper; private static int defaultJsonLength; @@ -41,6 +42,9 @@ class JsonOptimizationUnitTest { ObjectMapper oneTimeMapper = new ObjectMapper(); byte[] feedback = oneTimeMapper.writeValueAsBytes(customers); defaultJsonLength = feedback.length; + System.out.println(); + System.out.println("Default JSON length: " + defaultJsonLength); + System.out.println(); } @BeforeEach @@ -63,8 +67,8 @@ class JsonOptimizationUnitTest { @Test void testDefaultNoNull() throws IOException { printBanner(TEST_LABEL_DEFAULT_JSON_NO_NULL); - CustomerNoNull[] defaultNoNull = CustomerNoNull.fromCustomers(customers); - byte[] plainJson = createPlainJson(TEST_LABEL_DEFAULT_JSON_NO_NULL, defaultNoNull); + mapper.setSerializationInclusion(Include.NON_NULL); + byte[] plainJson = createPlainJson(TEST_LABEL_DEFAULT_JSON_NO_NULL, customers); compressJson(TEST_LABEL_DEFAULT_JSON_NO_NULL, plainJson); } @@ -79,8 +83,9 @@ class JsonOptimizationUnitTest { @Test void testShortNamesNoNull() throws IOException { printBanner(TEST_LABEL_SHORT_NAMES_NO_NULL); - CustomerShortNamesNoNull[] shorterOnesNoNull = CustomerShortNamesNoNull.fromCustomers(customers); - byte[] shorterJson = createPlainJson(TEST_LABEL_SHORT_NAMES_NO_NULL, shorterOnesNoNull); + CustomerShortNames[] shorterOnes = CustomerShortNames.fromCustomers(customers); + mapper.setSerializationInclusion(Include.NON_NULL); + byte[] shorterJson = createPlainJson(TEST_LABEL_SHORT_NAMES_NO_NULL, shorterOnes); compressJson(TEST_LABEL_SHORT_NAMES_NO_NULL, shorterJson); } @@ -135,13 +140,15 @@ class JsonOptimizationUnitTest { } void compressJson(String label, byte[] plainJson) throws IOException { - ByteArrayOutputStream outpuStream = new ByteArrayOutputStream(); - GZIPOutputStream gzipStream = new GZIPOutputStream(outpuStream); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + GZIPOutputStream gzipStream = new GZIPOutputStream(outputStream); gzipStream.write(plainJson); gzipStream.close(); - byte[] gzippedJson = outpuStream.toByteArray(); - double percent = Math.round(gzippedJson.length * 100d / defaultJsonLength); - System.out.println(label + " GZIPped length: " + LENGTH_FORMATTER.format(gzippedJson.length / 1024) + outputStream.close(); + byte[] gzippedJson = outputStream.toByteArray(); + double length = gzippedJson.length / 1024d; + double percent = gzippedJson.length * 100d / defaultJsonLength; + System.out.println(label + " GZIPped length: " + LENGTH_FORMATTER.format(length) + "kB (" + PERCENT_FORMATTER.format(percent) + "%)"); assertTrue(plainJson.length > gzippedJson.length, label + " should be longer than GZIPped data"); } @@ -152,8 +159,9 @@ class JsonOptimizationUnitTest { System.out.println(prettyWritter.writeValueAsString(customers[0])); byte[] feedback = mapper.writeValueAsBytes(customers); - double percent = Math.round(feedback.length * 100d / defaultJsonLength); - System.out.println(label + " length: " + LENGTH_FORMATTER.format(feedback.length / 1024) + double length = feedback.length / 1024d; + double percent = feedback.length * 100d / defaultJsonLength; + System.out.println(label + " length: " + LENGTH_FORMATTER.format(length) + "kB (" + PERCENT_FORMATTER.format(percent) + "%)"); assertTrue(feedback.length > 1, label + " should be there");