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 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<Customer> {
private static final long serialVersionUID = 1L;
@ -28,13 +25,20 @@ public class CustomerDeserializer extends StdDeserializer<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);

View File

@ -13,7 +13,7 @@ public class CustomerNoNull extends Customer {
public static CustomerNoNull[] fromCustomers(Customer[] customers) {
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];
CustomerNoNull newOne = new CustomerNoNull();

View File

@ -2,8 +2,6 @@ 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 {
@ -37,54 +35,71 @@ public class CustomerShortNames {
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;
}
@ -93,6 +108,7 @@ public class CustomerShortNames {
public int hashCode() {
return Objects.hash(city, email, firstName, id, lastName, phoneNumber, postalCode, state, street);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
@ -115,7 +131,7 @@ public class CustomerShortNames {
public static CustomerShortNames[] fromCustomers(Customer[] customers) {
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];
CustomerShortNames newOne = new CustomerShortNames();

View File

@ -1,12 +1,10 @@
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() + "]";
@ -15,7 +13,7 @@ public class CustomerShortNamesNoNull extends CustomerShortNames {
public static CustomerShortNamesNoNull[] fromCustomers(Customer[] customers) {
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];
CustomerShortNamesNoNull newOne = new CustomerShortNamesNoNull();

View File

@ -56,7 +56,7 @@ public class CustomerSlim {
public static CustomerSlim[] fromCustomers(Customer[] customers) {
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];
CustomerSlim newOne = new CustomerSlim();
@ -70,5 +70,4 @@ public class CustomerSlim {
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.Test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;