BAEL-3326, "Optimizing JSON Schema for production use":

Optimized imports and formatted source.
This commit is contained in:
Karsten Silz 2020-09-05 18:48:35 +01:00
parent a7dc3199ad
commit 1ab520e63a
8 changed files with 86 additions and 70 deletions

View File

@ -2,14 +2,11 @@ package com.baeldung.jsonoptimization;
import java.io.IOException; import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec; import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode; 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.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
public class CustomerDeserializer extends StdDeserializer<Customer> { public class CustomerDeserializer extends StdDeserializer<Customer> {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@ -28,13 +25,20 @@ public class CustomerDeserializer extends StdDeserializer<Customer> {
ObjectCodec codec = parser.getCodec(); ObjectCodec codec = parser.getCodec();
JsonNode node = codec.readTree(parser); JsonNode node = codec.readTree(parser);
feedback.setId(node.get(0).asLong()); feedback.setId(node.get(0)
feedback.setFirstName(node.get(1).asText()); .asLong());
feedback.setLastName(node.get(2).asText()); feedback.setFirstName(node.get(1)
feedback.setStreet(node.get(3).asText()); .asText());
feedback.setPostalCode(node.get(4).asText()); feedback.setLastName(node.get(2)
feedback.setCity(node.get(5).asText()); .asText());
feedback.setState(node.get(6).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); JsonNode phoneNumber = node.get(7);
feedback.setPhoneNumber(phoneNumber.isNull() ? null : phoneNumber.asText()); feedback.setPhoneNumber(phoneNumber.isNull() ? null : phoneNumber.asText());
JsonNode email = node.get(8); JsonNode email = node.get(8);

View File

@ -13,7 +13,7 @@ public class CustomerNoNull extends Customer {
public static CustomerNoNull[] fromCustomers(Customer[] customers) { public static CustomerNoNull[] fromCustomers(Customer[] customers) {
CustomerNoNull[] feedback = new CustomerNoNull[customers.length]; CustomerNoNull[] feedback = new CustomerNoNull[customers.length];
for(int i = 0; i < customers.length; i++) { for (int i = 0; i < customers.length; i++) {
Customer aCustomer = customers[i]; Customer aCustomer = customers[i];
CustomerNoNull newOne = new CustomerNoNull(); CustomerNoNull newOne = new CustomerNoNull();

View File

@ -2,8 +2,6 @@ package com.baeldung.jsonoptimization;
import java.util.Objects; import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
public class CustomerShortNames { public class CustomerShortNames {
@ -37,54 +35,71 @@ public class CustomerShortNames {
public long getId() { public long getId() {
return id; return id;
} }
public void setId(long id) { public void setId(long id) {
this.id = id; this.id = id;
} }
public String getFirstName() { public String getFirstName() {
return firstName; return firstName;
} }
public void setFirstName(String firstName) { public void setFirstName(String firstName) {
this.firstName = firstName; this.firstName = firstName;
} }
public String getLastName() { public String getLastName() {
return lastName; return lastName;
} }
public void setLastName(String lastName) { public void setLastName(String lastName) {
this.lastName = lastName; this.lastName = lastName;
} }
public String getStreet() { public String getStreet() {
return street; return street;
} }
public void setStreet(String street) { public void setStreet(String street) {
this.street = street; this.street = street;
} }
public String getPostalCode() { public String getPostalCode() {
return postalCode; return postalCode;
} }
public void setPostalCode(String postalCode) { public void setPostalCode(String postalCode) {
this.postalCode = postalCode; this.postalCode = postalCode;
} }
public String getCity() { public String getCity() {
return city; return city;
} }
public void setCity(String city) { public void setCity(String city) {
this.city = city; this.city = city;
} }
public String getState() { public String getState() {
return state; return state;
} }
public void setState(String state) { public void setState(String state) {
this.state = state; this.state = state;
} }
public String getPhoneNumber() { public String getPhoneNumber() {
return phoneNumber; return phoneNumber;
} }
public void setPhoneNumber(String phoneNumber) { public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber; this.phoneNumber = phoneNumber;
} }
public String getEmail() { public String getEmail() {
return email; return email;
} }
public void setEmail(String email) { public void setEmail(String email) {
this.email = email; this.email = email;
} }
@ -93,6 +108,7 @@ public class CustomerShortNames {
public int hashCode() { public int hashCode() {
return Objects.hash(city, email, firstName, id, lastName, phoneNumber, postalCode, state, street); return Objects.hash(city, email, firstName, id, lastName, phoneNumber, postalCode, state, street);
} }
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) { if (this == obj) {
@ -115,7 +131,7 @@ public class CustomerShortNames {
public static CustomerShortNames[] fromCustomers(Customer[] customers) { public static CustomerShortNames[] fromCustomers(Customer[] customers) {
CustomerShortNames[] feedback = new CustomerShortNames[customers.length]; CustomerShortNames[] feedback = new CustomerShortNames[customers.length];
for(int i = 0; i < customers.length; i++) { for (int i = 0; i < customers.length; i++) {
Customer aCustomer = customers[i]; Customer aCustomer = customers[i];
CustomerShortNames newOne = new CustomerShortNames(); CustomerShortNames newOne = new CustomerShortNames();

View File

@ -1,12 +1,10 @@
package com.baeldung.jsonoptimization; package com.baeldung.jsonoptimization;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
@JsonInclude(JsonInclude.Include.NON_NULL) @JsonInclude(JsonInclude.Include.NON_NULL)
public class CustomerShortNamesNoNull extends CustomerShortNames { public class CustomerShortNamesNoNull extends CustomerShortNames {
@Override @Override
public String toString() { public String toString() {
return "CustomerShortNamesNoNull [toString()=" + super.toString() + "]"; return "CustomerShortNamesNoNull [toString()=" + super.toString() + "]";
@ -15,7 +13,7 @@ public class CustomerShortNamesNoNull extends CustomerShortNames {
public static CustomerShortNamesNoNull[] fromCustomers(Customer[] customers) { public static CustomerShortNamesNoNull[] fromCustomers(Customer[] customers) {
CustomerShortNamesNoNull[] feedback = new CustomerShortNamesNoNull[customers.length]; CustomerShortNamesNoNull[] feedback = new CustomerShortNamesNoNull[customers.length];
for(int i = 0; i < customers.length; i++) { for (int i = 0; i < customers.length; i++) {
Customer aCustomer = customers[i]; Customer aCustomer = customers[i];
CustomerShortNamesNoNull newOne = new CustomerShortNamesNoNull(); CustomerShortNamesNoNull newOne = new CustomerShortNamesNoNull();

View File

@ -56,7 +56,7 @@ public class CustomerSlim {
public static CustomerSlim[] fromCustomers(Customer[] customers) { public static CustomerSlim[] fromCustomers(Customer[] customers) {
CustomerSlim[] feedback = new CustomerSlim[customers.length]; CustomerSlim[] feedback = new CustomerSlim[customers.length];
for(int i = 0; i < customers.length; i++) { for (int i = 0; i < customers.length; i++) {
Customer aCustomer = customers[i]; Customer aCustomer = customers[i];
CustomerSlim newOne = new CustomerSlim(); CustomerSlim newOne = new CustomerSlim();
@ -70,5 +70,4 @@ public class CustomerSlim {
return feedback; return feedback;
} }
} }

View File

@ -15,7 +15,6 @@ import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.Version; import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter; import com.fasterxml.jackson.databind.ObjectWriter;