From fb4041073bc3bd9e154f2def47b4bbcf70b9b2bd Mon Sep 17 00:00:00 2001 From: Karsten Silz Date: Sat, 5 Sep 2020 18:48:35 +0100 Subject: [PATCH] BAEL-3326, "Optimizing JSON Schema for production use": Optimized imports and formatted source. --- .../baeldung/jsonoptimization/Customer.java | 2 +- .../CustomerDeserializer.java | 32 +++++----- .../jsonoptimization/CustomerNoNull.java | 16 ++--- .../jsonoptimization/CustomerSerializer.java | 8 +-- .../jsonoptimization/CustomerShortNames.java | 58 ++++++++++++------- .../CustomerShortNamesNoNull.java | 18 +++--- .../jsonoptimization/CustomerSlim.java | 13 ++--- 7 files changed, 82 insertions(+), 65 deletions(-) diff --git a/json/src/main/java/com/baeldung/jsonoptimization/Customer.java b/json/src/main/java/com/baeldung/jsonoptimization/Customer.java index 81a74971ea..85451731e9 100644 --- a/json/src/main/java/com/baeldung/jsonoptimization/Customer.java +++ b/json/src/main/java/com/baeldung/jsonoptimization/Customer.java @@ -116,7 +116,7 @@ public class Customer { public static Customer[] fromMockFile() throws IOException { ObjectMapper objectMapper = new ObjectMapper(); - InputStream jsonFile = new FileInputStream("src/test/resources/json_optimization_mock_data.json"); + InputStream jsonFile = new FileInputStream("src/test/resources/json_optimization_mock_data.json"); Customer[] feedback = objectMapper.readValue(jsonFile, Customer[].class); return feedback; } diff --git a/json/src/main/java/com/baeldung/jsonoptimization/CustomerDeserializer.java b/json/src/main/java/com/baeldung/jsonoptimization/CustomerDeserializer.java index 1f478ed446..04ab15556a 100644 --- a/json/src/main/java/com/baeldung/jsonoptimization/CustomerDeserializer.java +++ b/json/src/main/java/com/baeldung/jsonoptimization/CustomerDeserializer.java @@ -2,39 +2,43 @@ package com.baeldung.jsonoptimization; import java.io.IOException; -import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.ObjectCodec; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.deser.std.StdDeserializer; -import com.fasterxml.jackson.databind.ser.std.StdSerializer; -public class CustomerDeserializer extends StdDeserializer { +public class CustomerDeserializer extends StdDeserializer { private static final long serialVersionUID = 1L; public CustomerDeserializer() { this(null); } - + public CustomerDeserializer(Class t) { super(t); } - + @Override public Customer deserialize(JsonParser parser, DeserializationContext deserializer) throws IOException { Customer feedback = new Customer(); ObjectCodec codec = parser.getCodec(); JsonNode node = codec.readTree(parser); - - feedback.setId(node.get(0).asLong()); - feedback.setFirstName(node.get(1).asText()); - feedback.setLastName(node.get(2).asText()); - feedback.setStreet(node.get(3).asText()); - feedback.setPostalCode(node.get(4).asText()); - feedback.setCity(node.get(5).asText()); - feedback.setState(node.get(6).asText()); + + feedback.setId(node.get(0) + .asLong()); + feedback.setFirstName(node.get(1) + .asText()); + feedback.setLastName(node.get(2) + .asText()); + feedback.setStreet(node.get(3) + .asText()); + feedback.setPostalCode(node.get(4) + .asText()); + feedback.setCity(node.get(5) + .asText()); + feedback.setState(node.get(6) + .asText()); JsonNode phoneNumber = node.get(7); feedback.setPhoneNumber(phoneNumber.isNull() ? null : phoneNumber.asText()); JsonNode email = node.get(8); diff --git a/json/src/main/java/com/baeldung/jsonoptimization/CustomerNoNull.java b/json/src/main/java/com/baeldung/jsonoptimization/CustomerNoNull.java index 1ee76f762a..62cf526f78 100644 --- a/json/src/main/java/com/baeldung/jsonoptimization/CustomerNoNull.java +++ b/json/src/main/java/com/baeldung/jsonoptimization/CustomerNoNull.java @@ -4,19 +4,19 @@ 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++) { + 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()); @@ -26,11 +26,11 @@ public class CustomerNoNull extends Customer { 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/CustomerSerializer.java b/json/src/main/java/com/baeldung/jsonoptimization/CustomerSerializer.java index 7e58010640..0f631ee85b 100644 --- a/json/src/main/java/com/baeldung/jsonoptimization/CustomerSerializer.java +++ b/json/src/main/java/com/baeldung/jsonoptimization/CustomerSerializer.java @@ -6,21 +6,21 @@ import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.ser.std.StdSerializer; -public class CustomerSerializer extends StdSerializer { +public class CustomerSerializer extends StdSerializer { private static final long serialVersionUID = 1L; public CustomerSerializer() { this(null); } - + public CustomerSerializer(Class t) { super(t); } - + @Override public void serialize(Customer customer, JsonGenerator jsonGenerator, SerializerProvider serializer) throws IOException { jsonGenerator.writeStartArray(); - jsonGenerator.writeNumber(customer.getId()); + jsonGenerator.writeNumber(customer.getId()); jsonGenerator.writeString(customer.getFirstName()); jsonGenerator.writeString(customer.getLastName()); jsonGenerator.writeString(customer.getStreet()); diff --git a/json/src/main/java/com/baeldung/jsonoptimization/CustomerShortNames.java b/json/src/main/java/com/baeldung/jsonoptimization/CustomerShortNames.java index ffa10f786d..2a47a4bbac 100644 --- a/json/src/main/java/com/baeldung/jsonoptimization/CustomerShortNames.java +++ b/json/src/main/java/com/baeldung/jsonoptimization/CustomerShortNames.java @@ -2,97 +2,113 @@ package com.baeldung.jsonoptimization; import java.util.Objects; -import com.fasterxml.jackson.annotation.JsonGetter; -import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; public class CustomerShortNames { private long id; - + @JsonProperty("f") private String firstName; - + @JsonProperty("l") private String lastName; - + @JsonProperty("s") private String street; - + @JsonProperty("p") private String postalCode; - + @JsonProperty("c") private String city; - + @JsonProperty("a") private String state; - + @JsonProperty("o") private String phoneNumber; - + @JsonProperty("e") private String email; - + public long getId() { return id; } + public void setId(long id) { this.id = id; } + public String getFirstName() { return firstName; } + public void setFirstName(String firstName) { this.firstName = firstName; } + public String getLastName() { return lastName; } + public void setLastName(String lastName) { this.lastName = lastName; } + public String getStreet() { return street; } + public void setStreet(String street) { this.street = street; } + public String getPostalCode() { return postalCode; } + public void setPostalCode(String postalCode) { this.postalCode = postalCode; } + public String getCity() { return city; } + public void setCity(String city) { this.city = city; } + public String getState() { return state; } + public void setState(String state) { this.state = state; } + public String getPhoneNumber() { return phoneNumber; } + public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } + public String getEmail() { return email; } + public void setEmail(String email) { this.email = email; } - + @Override public int hashCode() { return Objects.hash(city, email, firstName, id, lastName, phoneNumber, postalCode, state, street); } + @Override public boolean equals(Object obj) { if (this == obj) { @@ -105,20 +121,20 @@ public class CustomerShortNames { return Objects.equals(city, other.city) && Objects.equals(email, other.email) && Objects.equals(firstName, other.firstName) && id == other.id && Objects.equals(lastName, other.lastName) && Objects.equals(phoneNumber, other.phoneNumber) && Objects.equals(postalCode, other.postalCode) && Objects.equals(state, other.state) && Objects.equals(street, other.street); } - + @Override public String toString() { return "CustomerWithShorterAttributes [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", street=" + street + ", postalCode=" + postalCode + ", city=" + city + ", state=" + state + ", phoneNumber=" + phoneNumber + ", email=" + email + "]"; - } + } public static CustomerShortNames[] fromCustomers(Customer[] customers) { - CustomerShortNames[] feedback = new CustomerShortNames[customers.length]; - - for(int i = 0; i < customers.length; i++) { + CustomerShortNames[] feedback = new CustomerShortNames[customers.length]; + + for (int i = 0; i < customers.length; i++) { Customer aCustomer = customers[i]; CustomerShortNames newOne = new CustomerShortNames(); - + newOne.setId(aCustomer.getId()); newOne.setFirstName(aCustomer.getFirstName()); newOne.setLastName(aCustomer.getLastName()); @@ -128,11 +144,11 @@ public class CustomerShortNames { 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 index d3e9648a98..1b62fb2c06 100644 --- a/json/src/main/java/com/baeldung/jsonoptimization/CustomerShortNamesNoNull.java +++ b/json/src/main/java/com/baeldung/jsonoptimization/CustomerShortNamesNoNull.java @@ -1,24 +1,22 @@ package com.baeldung.jsonoptimization; import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonProperty; @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++) { + 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()); @@ -28,10 +26,10 @@ public class CustomerShortNamesNoNull extends CustomerShortNames { 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/CustomerSlim.java b/json/src/main/java/com/baeldung/jsonoptimization/CustomerSlim.java index cdfaa4e329..e2ef4664cf 100644 --- a/json/src/main/java/com/baeldung/jsonoptimization/CustomerSlim.java +++ b/json/src/main/java/com/baeldung/jsonoptimization/CustomerSlim.java @@ -54,21 +54,20 @@ public class CustomerSlim { } public static CustomerSlim[] fromCustomers(Customer[] customers) { - CustomerSlim[] feedback = new CustomerSlim[customers.length]; - - for(int i = 0; i < customers.length; i++) { + CustomerSlim[] feedback = new CustomerSlim[customers.length]; + + for (int i = 0; i < customers.length; i++) { Customer aCustomer = customers[i]; CustomerSlim newOne = new CustomerSlim(); - + newOne.setId(aCustomer.getId()); newOne.setName(aCustomer.getFirstName() + " " + aCustomer.getLastName()); newOne.setAddress(aCustomer.getStreet() + ", " + aCustomer.getCity() + " " + aCustomer.getState() + " " + aCustomer.getPostalCode()); - + feedback[i] = newOne; } - + return feedback; } - }