Converting from plain json to POJOs in JAVA Api

260 views
Skip to first unread message

haowei...@gmail.com

unread,
Apr 10, 2015, 6:34:58 PM4/10/15
to jsonschema...@googlegroups.com
Hello,

I'm trying to convert plain Json(not Json Schema) to POJOs. I tried that through the online tool and it worked. But in Java code it does not generate the files. Do I need to set the source type to Json? Which method should I call to achieve that?

Thanks,
Haowei

jan.kn...@me.com

unread,
Jul 10, 2015, 5:05:01 AM7/10/15
to jsonschema...@googlegroups.com, haowei...@gmail.com
Hey,
since you haven't got an answer this is how I did it.
First you have to convert your JSON to a JSON Schema and than pass the toString of ObjectNode into the SchemaMapper.

Hope this helps for ppl looking for the same answer:

SchemaGenerator sg = new SchemaGenerator();

ObjectNode jsonschema = sg.schemaFromExample(source); //source json

new SchemaMapper().generate(codeModel, jsonname, "generated."+jsonname, jsonschema.toString());

codeModel.build(new File(workDir));


Cheers

Jan

sandeep...@gmail.com

unread,
Nov 25, 2015, 5:55:25 AM11/25/15
to jsonschema2pojo-users, haowei...@gmail.com

<plugin>
<groupId>org.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
<version>0.4.16</version>
<configuration>
<sourceDirectory>${basedir}/schema</sourceDirectory>
<targetPackage>com.example.types</targetPackage>
<sourceType>json</sourceType>
<annotationStyle>gson</annotationStyle>
</configuration>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-core</artifactId>
<version>0.4.16</version>
</dependency>
</dependencies>

sandeep...@gmail.com

unread,
Nov 25, 2015, 5:56:57 AM11/25/15
to jsonschema2pojo-users, haowei...@gmail.com
pom.xml:
======

And in Git Bash or cmd prompt run "mvn compile(or) package".
It will generate the pojos.

On Saturday, April 11, 2015 at 4:04:58 AM UTC+5:30, haowei...@gmail.com wrote:
Reply all
Reply to author
Forward
0 new messages