diff --git a/json-2/pom.xml b/json-2/pom.xml index fbae40b6a3..26eab8fb4c 100644 --- a/json-2/pom.xml +++ b/json-2/pom.xml @@ -14,6 +14,11 @@ + + org.jsonschema2pojo + jsonschema2pojo-core + 1.1.1 + com.jsoniter jsoniter diff --git a/json-2/src/main/java/com/baeldung/jsontojavaclass/JsonToJavaClassConversion.java b/json-2/src/main/java/com/baeldung/jsontojavaclass/JsonToJavaClassConversion.java new file mode 100644 index 0000000000..65eecd0d38 --- /dev/null +++ b/json-2/src/main/java/com/baeldung/jsontojavaclass/JsonToJavaClassConversion.java @@ -0,0 +1,42 @@ +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 Object convertJsonToJavaClass(URL inputJson, File outputJavaClassDirectory, String packageName, String className) 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, className, packageName, inputJson); + + jcodeModel.build(outputJavaClassDirectory); + return mapper; + } + +} diff --git a/json-2/src/main/java/com/baeldung/jsontojavaclass/pojo/SamplePojo.java b/json-2/src/main/java/com/baeldung/jsontojavaclass/pojo/SamplePojo.java new file mode 100644 index 0000000000..4c8a719de0 --- /dev/null +++ b/json-2/src/main/java/com/baeldung/jsontojavaclass/pojo/SamplePojo.java @@ -0,0 +1,214 @@ + +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 topics = new ArrayList(); + @JsonIgnore + private Map additionalProperties = new HashMap(); + + @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 getTopics() { + return topics; + } + + @JsonProperty("topics") + public void setTopics(List topics) { + this.topics = topics; + } + + public SamplePojo withTopics(List topics) { + this.topics = topics; + return this; + } + + @JsonAnyGetter + public Map 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) ? "" : this.name)); + sb.append(','); + sb.append("area"); + sb.append('='); + sb.append(((this.area == null) ? "" : this.area)); + sb.append(','); + sb.append("author"); + sb.append('='); + sb.append(((this.author == null) ? "" : this.author)); + sb.append(','); + sb.append("id"); + sb.append('='); + sb.append(((this.id == null) ? "" : this.id)); + sb.append(','); + sb.append("salary"); + sb.append('='); + sb.append(((this.salary == null) ? "" : this.salary)); + sb.append(','); + sb.append("topics"); + sb.append('='); + sb.append(((this.topics == null) ? "" : this.topics)); + sb.append(','); + sb.append("additionalProperties"); + sb.append('='); + sb.append(((this.additionalProperties == 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)))); + } + +} diff --git a/json-2/src/test/java/com/baeldung/jsontojavaclass/JsonToJavaClassConversionUnitTest.java b/json-2/src/test/java/com/baeldung/jsontojavaclass/JsonToJavaClassConversionUnitTest.java new file mode 100644 index 0000000000..8dbfb14b45 --- /dev/null +++ b/json-2/src/test/java/com/baeldung/jsontojavaclass/JsonToJavaClassConversionUnitTest.java @@ -0,0 +1,38 @@ +package com.baeldung.jsontojavaclass; + +import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +class JsonToJavaClassConversionUnitTest { + + private JsonToJavaClassConversion jsonToJavaConversion = new JsonToJavaClassConversion(); + + @Test + void whenProvideInputJSON_thenGenerateJavaClass() throws MalformedURLException, IOException { + + String packageName = "com.baeldung.jsontojavaclass.pojo"; + + // load input JSON file + String jsonPath = "src/test/resources/"; + File inputJson = new File(jsonPath + "sample_input.json"); + + // create the local directory for generating the Java Class file + String outputPath = "src/main/java/"; + File outputJavaClassDirectory = new File(outputPath); + outputJavaClassDirectory.mkdirs(); + + String className = "SamplePojo"; + + Object object = jsonToJavaConversion.convertJsonToJavaClass(inputJson.toURI() + .toURL(), outputJavaClassDirectory, packageName, className); + System.out.println(object); + + Assertions.assertNotNull(object); + + } + +} diff --git a/json-2/src/test/resources/sample_input.json b/json-2/src/test/resources/sample_input.json new file mode 100644 index 0000000000..b6e90da357 --- /dev/null +++ b/json-2/src/test/resources/sample_input.json @@ -0,0 +1,13 @@ +{ + "name": "Baeldung", + "area": "tech blogs", + "author": "Eugen", + "id": 32134, + "salary": 70000, + "topics": [ + "java", + "kotlin", + "cs", + "linux" + ] +} \ No newline at end of file