43 lines
1.3 KiB
Java
43 lines
1.3 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 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;
|
||
|
}
|
||
|
|
||
|
}
|