Jsonschema2pojo does not generate pojo classes from json string

I followed the link Create Java class from JSON? to create POJO classes from json string (not from schema). I am using jsonschema2pojo jar version 0.4.10, but could not create the POJO class. My code is as follows:

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 a json string that needs to be converted to POJO. Can someone please help me here.

+4
source share
4 answers

I had a similar experience using the command line tool. In my case, this was the result of an incorrect source type (JSONSCHEMA or JSON, default: JSONSCHEMA).

, : (no-args) SchemaMapper. :

  • org.jsonschema2pojo.DefaultGenerationConfig, getSourceType() SourceType.JSON
  • SchemaMapper(RuleFactory ruleFactory, SchemaGenerator schemaGenerator) ( no-args).
+6

, . , Jason Schema. Jason, : SourceType.JSON .

class App
{   
    public static void main( String[] args )
        {
            JCodeModel codeModel = new JCodeModel();        
            try {
                URL source= new URL("file:///D:/temp.json");
                GenerationConfig config = new DefaultGenerationConfig() {
                    @Override
                    public boolean isGenerateBuilders() {
                    return true;
                    }
                    public SourceType getSourceType(){
                        return SourceType.JSON;
                    }
                    };
                    SchemaMapper mapper =new SchemaMapper(new RuleFactory(config, new GsonAnnotator(), new SchemaStore()), new SchemaGenerator());
                    mapper.generate(codeModel, "Accession", "com.test", source);
                File dir = new File("D://");
                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();
            }       
        }
}

, . !

+4

. :

GenerationConfig, getSourceType:

static class MyConfig extends DefaultGenerationConfig {
    @Override
    public SourceType getSourceType() {
        return SourceType.JSON;
    }
}

Then I initialized the build process as follows:

private void parseFileExample() {               
    URL source = new URL("file:/tmp/input/blah.json");
    JCodeModel codeModel = new JCodeModel();
    MyConfig generationConfig = new MyConfig();
    RuleFactory ruleFactory = new RuleFactory(generationConfig, new GsonAnnotator(), new SchemaStore());
    SchemaGenerator generator = new SchemaGenerator();

    SchemaMapper mapper = new SchemaMapper(ruleFactory, generator);
    mapper.generate(codeModel, "MyClass", "com.drakedroid", source);

    codeModel.build(new File("/tmp/output/"));
}

The trick here was to use a URL . Mapper.generate did not work when I only passed a string.

+1
source

Thanks @Kapil, your answer helped me. This program allows us to create POJO classes according to predefined JSON. We can also use it at runtime when the JSON response is unknown, write the JSON response to the file and read it accordingly using the following code.

public class JSONExample {

  public static void main(String... args)  {

    JCodeModel codeModel = new JCodeModel();

    try
    {
        // In sample.json I have already pasted a JSON
        File file=new File("//root//AndroidStudioProjects//MyApplication//sample.json");
        URL source = file.toURI().toURL();

        GenerationConfig config = new DefaultGenerationConfig() {
            @Override
            public boolean isGenerateBuilders() 
            {
                return true;
            }
            public SourceType getSourceType()
            {
                return SourceType.JSON;
            }
        };

        SchemaMapper mapper = new SchemaMapper(new RuleFactory(config, new Jackson2Annotator(), new SchemaStore()), new SchemaGenerator());
        try {
            // The ClassName is the main class that will contain references to other generated class files
            // com.example is the package name 
            mapper.generate(codeModel, "ClassName", "com.example", source);
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            // Directory where classes will be genetrated
            File file1=new File("//root//AndroidStudioProjects//MyApplication//");
            if (file1.exists())
            {
                System.out.println("dir available");
                codeModel.build(file1);
            }
            else
            {
                System.out.println("dir not available");
            }

            System.out.println(""+file1+" Exists "+file1.exists());


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    catch(Exception e)
    {
        e.printStackTrace();
    }
 }
}
0
source

All Articles