Java 11497 (#12399)

* Added/created parent module (json-modules)

* moved json(submodule) to json-modules(parent)

* moved json-2(submodule) to json-modules(parent)

* moved json-path(submodule) to json-modules(parent)

* moved gson(submodule) to json-modules(parent)

* deleted sub-modules that we moved to json-modules

Co-authored-by: panagiotiskakos <panagiotis.kakos@libra-is.com>
This commit is contained in:
panos-kakos
2022-06-24 17:28:34 +01:00
committed by GitHub
parent 86d5df3e85
commit 7866faf761
163 changed files with 377 additions and 336 deletions

View File

@@ -0,0 +1,22 @@
package com.baeldung.jsoniter.model;
public class Name {
private String firstName;
private String surname;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
}

View File

@@ -0,0 +1,26 @@
package com.baeldung.jsoniter.model;
import com.jsoniter.annotation.JsonProperty;
import com.jsoniter.fuzzy.MaybeStringIntDecoder;
public class Student {
@JsonProperty(decoder = MaybeStringIntDecoder.class)
private int id;
private Name name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Name getName() {
return name;
}
public void setName(Name name) {
this.name = name;
}
}

View File

@@ -0,0 +1,123 @@
package com.baeldung.jsonoptimization;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Customer {
private long id;
private String firstName;
private String lastName;
private String street;
private String postalCode;
private String city;
private String state;
private String phoneNumber;
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) {
return true;
}
if (!(obj instanceof Customer)) {
return false;
}
Customer other = (Customer) obj;
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 "Customer [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", street=" + street + ", postalCode=" + postalCode + ", city=" + city + ", state=" + state + ", phoneNumber=" + phoneNumber + ", email=" + email + "]";
}
public static Customer[] fromMockFile() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
InputStream jsonFile = new FileInputStream("src/test/resources/json_optimization_mock_data.json");
Customer[] feedback = objectMapper.readValue(jsonFile, Customer[].class);
return feedback;
}
}

View File

@@ -0,0 +1,49 @@
package com.baeldung.jsonoptimization;
import java.io.IOException;
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.deser.std.StdDeserializer;
public class CustomerDeserializer extends StdDeserializer<Customer> {
private static final long serialVersionUID = 1L;
public CustomerDeserializer() {
this(null);
}
public CustomerDeserializer(Class<Customer> 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());
JsonNode phoneNumber = node.get(7);
feedback.setPhoneNumber(phoneNumber.isNull() ? null : phoneNumber.asText());
JsonNode email = node.get(8);
feedback.setEmail(email.isNull() ? null : email.asText());
return feedback;
}
}

View File

@@ -0,0 +1,34 @@
package com.baeldung.jsonoptimization;
import java.io.IOException;
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<Customer> {
private static final long serialVersionUID = 1L;
public CustomerSerializer() {
this(null);
}
public CustomerSerializer(Class<Customer> t) {
super(t);
}
@Override
public void serialize(Customer customer, JsonGenerator jsonGenerator, SerializerProvider serializer) throws IOException {
jsonGenerator.writeStartArray();
jsonGenerator.writeNumber(customer.getId());
jsonGenerator.writeString(customer.getFirstName());
jsonGenerator.writeString(customer.getLastName());
jsonGenerator.writeString(customer.getStreet());
jsonGenerator.writeString(customer.getPostalCode());
jsonGenerator.writeString(customer.getCity());
jsonGenerator.writeString(customer.getState());
jsonGenerator.writeString(customer.getPhoneNumber());
jsonGenerator.writeString(customer.getEmail());
jsonGenerator.writeEndArray();
}
}

View File

@@ -0,0 +1,155 @@
package com.baeldung.jsonoptimization;
import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonProperty;
public class CustomerShortNames {
@JsonProperty("i")
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) {
return true;
}
if (!(obj instanceof CustomerShortNames)) {
return false;
}
CustomerShortNames other = (CustomerShortNames) obj;
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++) {
Customer aCustomer = customers[i];
CustomerShortNames newOne = new CustomerShortNames();
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;
}
}

View File

@@ -0,0 +1,73 @@
package com.baeldung.jsonoptimization;
import java.util.Objects;
public class CustomerSlim {
private long id;
private String name;
private String address;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public int hashCode() {
return Objects.hash(address, id, name);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof CustomerSlim)) {
return false;
}
CustomerSlim other = (CustomerSlim) obj;
return Objects.equals(address, other.address) && id == other.id && Objects.equals(name, other.name);
}
@Override
public String toString() {
return "CustomerSlim [id=" + id + ", name=" + name + ", address=" + address + "]";
}
public static CustomerSlim[] fromCustomers(Customer[] customers) {
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;
}
}

View File

@@ -0,0 +1,37 @@
package com.baeldung.jsonoptimization;
import java.io.IOException;
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.deser.std.StdDeserializer;
public class CustomerSlimDeserializer extends StdDeserializer<CustomerSlim> {
private static final long serialVersionUID = 1L;
public CustomerSlimDeserializer() {
this(null);
}
public CustomerSlimDeserializer(Class<CustomerSlim> t) {
super(t);
}
@Override
public CustomerSlim deserialize(JsonParser parser, DeserializationContext deserializer) throws IOException {
CustomerSlim feedback = new CustomerSlim();
ObjectCodec codec = parser.getCodec();
JsonNode node = codec.readTree(parser);
feedback.setId(node.get(0)
.asLong());
feedback.setName(node.get(1)
.asText());
feedback.setAddress(node.get(2)
.asText());
return feedback;
}
}

View File

@@ -0,0 +1,28 @@
package com.baeldung.jsonoptimization;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
public class CustomerSlimSerializer extends StdSerializer<CustomerSlim> {
private static final long serialVersionUID = 1L;
public CustomerSlimSerializer() {
this(null);
}
public CustomerSlimSerializer(Class<CustomerSlim> t) {
super(t);
}
@Override
public void serialize(CustomerSlim customer, JsonGenerator jsonGenerator, SerializerProvider serializer) throws IOException {
jsonGenerator.writeStartArray();
jsonGenerator.writeNumber(customer.getId());
jsonGenerator.writeString(customer.getName());
jsonGenerator.writeString(customer.getAddress());
jsonGenerator.writeEndArray();
}
}

View File

@@ -0,0 +1,81 @@
package com.baeldung.jsonoptimization;
import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonProperty;
public class CustomerSlimShortNames {
@JsonProperty("i")
private long id;
@JsonProperty("n")
private String name;
@JsonProperty("a")
private String address;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public int hashCode() {
return Objects.hash(address, id, name);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof CustomerSlimShortNames)) {
return false;
}
CustomerSlimShortNames other = (CustomerSlimShortNames) obj;
return Objects.equals(address, other.address) && id == other.id && Objects.equals(name, other.name);
}
@Override
public String toString() {
return "CustomerSlim [id=" + id + ", name=" + name + ", address=" + address + "]";
}
public static CustomerSlimShortNames[] fromCustomers(Customer[] customers) {
CustomerSlimShortNames[] feedback = new CustomerSlimShortNames[customers.length];
for (int i = 0; i < customers.length; i++) {
Customer aCustomer = customers[i];
CustomerSlimShortNames newOne = new CustomerSlimShortNames();
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;
}
}

View File

@@ -0,0 +1,56 @@
package com.baeldung.jsontojavaclass;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import org.jsonschema2pojo.DefaultGenerationConfig;
import org.jsonschema2pojo.GenerationConfig;
import org.jsonschema2pojo.Jackson2Annotator;
import org.jsonschema2pojo.SchemaGenerator;
import org.jsonschema2pojo.SchemaMapper;
import org.jsonschema2pojo.SchemaStore;
import org.jsonschema2pojo.SourceType;
import org.jsonschema2pojo.rules.RuleFactory;
import com.sun.codemodel.JCodeModel;
public class JsonToJavaClassConversion {
public static void main(String[] args) {
String packageName = "com.baeldung.jsontojavaclass.pojo";
String basePath = "src/main/resources";
File inputJson = new File(basePath + File.separator + "input.json");
File outputPojoDirectory = new File(basePath + File.separator + "convertedPojo");
outputPojoDirectory.mkdirs();
try {
new JsonToJavaClassConversion().convertJsonToJavaClass(inputJson.toURI().toURL(), outputPojoDirectory, packageName, inputJson.getName().replace(".json", ""));
} catch (IOException e) {
System.out.println("Encountered issue while converting to pojo: " + e.getMessage());
e.printStackTrace();
}
}
public void convertJsonToJavaClass(URL inputJsonUrl, File outputJavaClassDirectory, String packageName, String javaClassName) throws IOException {
JCodeModel jcodeModel = new JCodeModel();
GenerationConfig config = new DefaultGenerationConfig() {
@Override
public boolean isGenerateBuilders() {
return true;
}
@Override
public SourceType getSourceType() {
return SourceType.JSON;
}
};
SchemaMapper mapper = new SchemaMapper(new RuleFactory(config, new Jackson2Annotator(config), new SchemaStore()), new SchemaGenerator());
mapper.generate(jcodeModel, javaClassName, packageName, inputJsonUrl);
jcodeModel.build(outputJavaClassDirectory);
}
}

View File

@@ -0,0 +1,213 @@
package com.baeldung.jsontojavaclass.pojo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"name",
"area",
"author",
"id",
"salary",
"topics"
})
@Generated("jsonschema2pojo")
public class SamplePojo {
@JsonProperty("name")
private String name;
@JsonProperty("area")
private String area;
@JsonProperty("author")
private String author;
@JsonProperty("id")
private Integer id;
@JsonProperty("salary")
private Integer salary;
@JsonProperty("topics")
private List<String> topics = new ArrayList<String>();
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("name")
public String getName() {
return name;
}
@JsonProperty("name")
public void setName(String name) {
this.name = name;
}
public SamplePojo withName(String name) {
this.name = name;
return this;
}
@JsonProperty("area")
public String getArea() {
return area;
}
@JsonProperty("area")
public void setArea(String area) {
this.area = area;
}
public SamplePojo withArea(String area) {
this.area = area;
return this;
}
@JsonProperty("author")
public String getAuthor() {
return author;
}
@JsonProperty("author")
public void setAuthor(String author) {
this.author = author;
}
public SamplePojo withAuthor(String author) {
this.author = author;
return this;
}
@JsonProperty("id")
public Integer getId() {
return id;
}
@JsonProperty("id")
public void setId(Integer id) {
this.id = id;
}
public SamplePojo withId(Integer id) {
this.id = id;
return this;
}
@JsonProperty("salary")
public Integer getSalary() {
return salary;
}
@JsonProperty("salary")
public void setSalary(Integer salary) {
this.salary = salary;
}
public SamplePojo withSalary(Integer salary) {
this.salary = salary;
return this;
}
@JsonProperty("topics")
public List<String> getTopics() {
return topics;
}
@JsonProperty("topics")
public void setTopics(List<String> topics) {
this.topics = topics;
}
public SamplePojo withTopics(List<String> topics) {
this.topics = topics;
return this;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
public SamplePojo withAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
return this;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(SamplePojo.class.getName()).append('@').append(Integer.toHexString(System.identityHashCode(this))).append('[');
sb.append("name");
sb.append('=');
sb.append(((this.name == null)?"<null>":this.name));
sb.append(',');
sb.append("area");
sb.append('=');
sb.append(((this.area == null)?"<null>":this.area));
sb.append(',');
sb.append("author");
sb.append('=');
sb.append(((this.author == null)?"<null>":this.author));
sb.append(',');
sb.append("id");
sb.append('=');
sb.append(((this.id == null)?"<null>":this.id));
sb.append(',');
sb.append("salary");
sb.append('=');
sb.append(((this.salary == null)?"<null>":this.salary));
sb.append(',');
sb.append("topics");
sb.append('=');
sb.append(((this.topics == null)?"<null>":this.topics));
sb.append(',');
sb.append("additionalProperties");
sb.append('=');
sb.append(((this.additionalProperties == null)?"<null>":this.additionalProperties));
sb.append(',');
if (sb.charAt((sb.length()- 1)) == ',') {
sb.setCharAt((sb.length()- 1), ']');
} else {
sb.append(']');
}
return sb.toString();
}
@Override
public int hashCode() {
int result = 1;
result = ((result* 31)+((this.area == null)? 0 :this.area.hashCode()));
result = ((result* 31)+((this.author == null)? 0 :this.author.hashCode()));
result = ((result* 31)+((this.topics == null)? 0 :this.topics.hashCode()));
result = ((result* 31)+((this.name == null)? 0 :this.name.hashCode()));
result = ((result* 31)+((this.id == null)? 0 :this.id.hashCode()));
result = ((result* 31)+((this.additionalProperties == null)? 0 :this.additionalProperties.hashCode()));
result = ((result* 31)+((this.salary == null)? 0 :this.salary.hashCode()));
return result;
}
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if ((other instanceof SamplePojo) == false) {
return false;
}
SamplePojo rhs = ((SamplePojo) other);
return ((((((((this.area == rhs.area)||((this.area!= null)&&this.area.equals(rhs.area)))&&((this.author == rhs.author)||((this.author!= null)&&this.author.equals(rhs.author))))&&((this.topics == rhs.topics)||((this.topics!= null)&&this.topics.equals(rhs.topics))))&&((this.name == rhs.name)||((this.name!= null)&&this.name.equals(rhs.name))))&&((this.id == rhs.id)||((this.id!= null)&&this.id.equals(rhs.id))))&&((this.additionalProperties == rhs.additionalProperties)||((this.additionalProperties!= null)&&this.additionalProperties.equals(rhs.additionalProperties))))&&((this.salary == rhs.salary)||((this.salary!= null)&&this.salary.equals(rhs.salary))));
}
}

View File

@@ -0,0 +1,32 @@
package com.baeldung.jsonvalidation;
import java.io.IOException;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.google.gson.TypeAdapter;
public class GsonValidator {
final TypeAdapter<JsonElement> strictAdapter = new Gson().getAdapter(JsonElement.class);
public boolean isValid(String json) {
try {
JsonParser.parseString(json);
} catch (JsonSyntaxException e) {
return false;
}
return true;
}
public boolean isValidStrict(String json) {
try {
strictAdapter.fromJson(json);
} catch (JsonSyntaxException | IOException e) {
return false;
}
return true;
}
}

View File

@@ -0,0 +1,18 @@
package com.baeldung.jsonvalidation;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonValidator {
final ObjectMapper mapper = new ObjectMapper();
public boolean isValid(String json) {
try {
mapper.readTree(json);
} catch (JacksonException e) {
return false;
}
return true;
}
}

View File

@@ -0,0 +1,30 @@
package com.baeldung.jsonvalidation;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class JsonValidator {
public boolean isValidObject(String json) {
try {
new JSONObject(json);
} catch (JSONException e) {
return false;
}
return true;
}
public boolean isValidJson(String json) {
try {
new JSONObject(json);
} catch (JSONException e) {
try {
new JSONArray(json);
} catch (JSONException ne) {
return false;
}
}
return true;
}
}