java-tutorials/json-2/src/main/java/com/baeldung/jsontojavaclass/JsonToJavaClassConversion.java
vaibhav007jain e45023ec07 BAEL-5071 (#11171)
* commited initial code for hexagonal architecture

* Deleting to check in again

* Deleing to check in again

* Push first code for Hexagonal Architecture

* final code with UT for JSON to Java conversion

* removed hexagonal-architecture code from last commit

* BEL-5071 updated README

* BAEL-5071: Undo README changes and added a nested object in the JSON example.

* BAEL-5071: fixed whitespace/indentation in JsonToJavaClassConversion.java

Co-authored-by: Vaibhav Jain <vaibhav.ashokjain@vodafone.com>
2021-09-09 20:05:29 -05:00

57 lines
2.0 KiB
Java

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);
}
}