I was following the link Generate Java class from JSON? to create the POJO classes from json string (and not from schema). I am using jsonschema2pojo jar of version 0.4.10 but could not able to generate the POJO class. My code is as below,
public class App
{
public static void main( String[] args )
{
JCodeModel codeModel = new JCodeModel();
try {
URL source = new URL("file:///C://Users//...//accession.json");
new SchemaMapper().generate(codeModel, "Accession", "com.test", source);
File dir = new File("D://test");
if(dir.exists()){
System.out.println("dir available");
codeModel.build(dir);
}else{
System.out.println("dir not available");
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
So accession.json has json string which need to be converted into POJO. Can anybody please help me here.
I had a similar experience using the command-line tool. In my case, it was the result of not correctly specifying the source type (JSONSCHEMA or JSON; default: JSONSCHEMA).
I think your problem is similar: You're using the default (no-args) constructor for SchemaMapper
. The following steps should solve the problem:
org.jsonschema2pojo.DefaultGenerationConfig
, overriding getSourceType()
to return SourceType.JSON
SchemaMapper(RuleFactory ruleFactory, SchemaGenerator schemaGenerator)
constructor (instead of the no-args constructor).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With