Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsonschema2pojo not generating pojo classes from json string

Tags:

java

json

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.

like image 918
Parag Dharmadhikari Avatar asked Mar 31 '15 23:03

Parag Dharmadhikari


1 Answers

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:

  1. Subclass org.jsonschema2pojo.DefaultGenerationConfig, overriding getSourceType() to return SourceType.JSON
  2. Use an instance of that subclass for the SchemaMapper(RuleFactory ruleFactory, SchemaGenerator schemaGenerator) constructor (instead of the no-args constructor).
like image 181
Zephyr Avatar answered Sep 28 '22 02:09

Zephyr