Maven Plugin For JSON Files

1,770 views
Skip to first unread message

Gautam Singh

unread,
May 10, 2014, 4:00:35 PM5/10/14
to jsonschema...@googlegroups.com
Hi, 

I'm trying to use jsonschema2pojo to generate POJO files from JSON Files and the plugin is not working for me. I'm able to create POJOs from JSON Schema Files but not from JSON Files. 

I'm using the following pom.xml configuration 

<plugins>
                <plugin>
                    <groupId>org.jsonschema2pojo</groupId>
                    <artifactId>jsonschema2pojo-maven-plugin</artifactId>
                    <version>0.4.2</version>
                    <configuration>
                        <sourceDirectory>${basedir}/src/main/resources/schema2</sourceDirectory>
                        <outputDirectory>${basedir}/src/main/java</outputDirectory>
                        <targetPackage>pojos</targetPackage>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>generate</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <source>1.6</source>
                        <target>1.6</target>
                    </configuration>
                </plugin>

                    <plugin>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>2.3.2</version>
                    </plugin>
                   </plugins>

Is it possible to use JSON files to generate the POJOs ? Any help would be highly appreciated. 

Thanks, 

G. 

Joe Littlejohn

unread,
May 10, 2014, 4:07:17 PM5/10/14
to jsonschema...@googlegroups.com
Hi Gautam

To use JSON as your input instead of JSON Schema, you simply need to add this to your plugin configuration:

<sourceType>json</sourceType>

Cheers


--
You received this message because you are subscribed to the Google Groups "jsonschema2pojo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jsonschema2pojo-...@googlegroups.com.
Visit this group at http://groups.google.com/group/jsonschema2pojo-users.
For more options, visit https://groups.google.com/d/optout.

Gautam Singh

unread,
May 10, 2014, 4:12:17 PM5/10/14
to jsonschema...@googlegroups.com
Thanks Joe. It works like a charm now. 
To unsubscribe from this group and stop receiving emails from it, send an email to jsonschema2pojo-users+unsub...@googlegroups.com.

lyan...@gmail.com

unread,
Oct 14, 2015, 11:37:40 AM10/14/15
to jsonschema2pojo-users
Hi, 

How can I generate the pojo classes in different packages with the json files in different folders? For example, I want to generate the the java objects for request and response in different packages with the json files stores in request/ and response/ folder separately. 

Thanks a lot,
Lisa
To unsubscribe from this group and stop receiving emails from it, send an email to jsonschema2pojo-users+unsub...@googlegroups.com.

Joe Littlejohn

unread,
Oct 14, 2015, 3:55:07 PM10/14/15
to jsonschema...@googlegroups.com
I'd recommend that you either:

1. Invoke the plugin more than once or

2. Point the plugin at the parent directory (it works recursively) and use javaType to make sure your classes have the right FQCN


To unsubscribe from this group and stop receiving emails from it, send an email to jsonschema2pojo-...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "jsonschema2pojo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jsonschema2pojo-...@googlegroups.com.

sandeep...@gmail.com

unread,
Nov 19, 2015, 5:38:52 AM11/19/15
to jsonschema2pojo-users
Hi All,
  
      I am new to creation of pojo classes from jsonschema/json using maven plugin. Can you send the sample code how is it possible.

Thanks
Sandeep

Tatu Saloranta

unread,
Nov 19, 2015, 12:34:40 PM11/19/15
to jsonschema...@googlegroups.com
What have you checked out so far, what approaches tried? What failed?
Or is this is the case of "do my homework"? It is customary to do due diligence first.

-+ Tatu +-

--

sandeep...@gmail.com

unread,
Nov 23, 2015, 11:32:41 AM11/23/15
to jsonschema2pojo-users
Hi,

      I tried to create the pojos from JSON(not JSONSchema) using maven plugin. I am able to create pojos. But there is a problem like 

   JSONs:
   =========
  {
"name" : "sandeep",
"id" : 12,
"address" : {
"add1" : "ab",
"add2" : "cd",
"state" : "up",
"country" : "India"
}
  }

 {
"name" : "rajesh",
"id" : 13,
"address" : {
"add1" : "xy",
"state" : "ap"
}
 }

 classes:
 ========

  public class Address{
  private String add1;
  private String add2;
  private String state;
  private String country;

 //setters and getters

 }

  public class Address__ {
  private String add1;
  private String state;
  }


        How could I generate only one class(Address) include all the properties in all Jsons(merge all the updates for the same class generated first time).


On Sunday, May 11, 2014 at 1:30:35 AM UTC+5:30, Gautam Singh wrote:

sandeep...@gmail.com

unread,
Nov 23, 2015, 11:35:17 AM11/23/15
to jsonschema2pojo-users
Try to add one property <configuration><sourceType>json</sourceType></configuration>.

It will generate the pojos from Json.


On Sunday, May 11, 2014 at 1:30:35 AM UTC+5:30, Gautam Singh wrote:

Joe Littlejohn

unread,
Nov 23, 2015, 12:30:28 PM11/23/15
to jsonschema...@googlegroups.com
Your best option is to give jsonschema2pojo *one* json file that contains a superset of fields for Address. You could create this by merging your Json files before giving them to this plugin. 

--

Sandeep sandy

unread,
Nov 24, 2015, 1:19:27 AM11/24/15
to jsonschema...@googlegroups.com
Previously I generated pojos through Ant using swagger at that time I didn't get any problem. But while trying to create those pojos from same JSON file I am getting duplicate classes contains additional properties in the new class by adding "___" for class names. There is no way to avoid this means clubbing at the runtime(classes) by maven.

--
You received this message because you are subscribed to a topic in the Google Groups "jsonschema2pojo-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jsonschema2pojo-users/XiDSEFxic_M/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jsonschema2pojo-...@googlegroups.com.



--
SANDEEP SANDY
Reply all
Reply to author
Forward
0 new messages