how to configure generate pojos from schema

744 views
Skip to first unread message

David Marcillo

unread,
Sep 30, 2015, 10:30:47 AM9/30/15
to jsonschema2pojo-users
I am new to json schema to pojo.  I downloaded source code to configure SchemaMapper generate() with the following :
Annotation style: GSON, Include getters and setters, Include constructors, etc ...  

The online example provides a configuration interface, any examples of how to accomplish this by calling the jsonschema2pojo jar  ?

Joe Littlejohn

unread,
Sep 30, 2015, 10:39:37 AM9/30/15
to jsonschema...@googlegroups.com
Hi David,

This code snippet should help. It shows an example of how to set a config option:


Easiest way to set any config option is to extend the DefaultGenerationConfig and return the values you want. I'm going to update the wiki example to this snippet on the next release (https://github.com/joelittlejohn/jsonschema2pojo/issues/427)

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.

David Marcillo

unread,
Sep 30, 2015, 11:42:16 AM9/30/15
to jsonschema2pojo-users
Hi Joe,

Yes !  thank you for the example, very easy to override default properties.

I am using $ref in the json schema for a few objects,

example "requestor", "shipper", "consignee" all "$ref" :  "#/definitions/facility" ... 

The online version generates objects for all three. 
The jar only generates the first object. 

How do I force generate all objects ?
--
David M. 
---------------------------------------------------------------------------------------------------------------------------------------


On Wednesday, September 30, 2015 at 10:39:37 AM UTC-4, Joe Littlejohn wrote:
Hi David,

This code snippet should help. It shows an example of how to set a config option:


Easiest way to set any config option is to extend the DefaultGenerationConfig and return the values you want. I'm going to update the wiki example to this snippet on the next release (https://github.com/joelittlejohn/jsonschema2pojo/issues/427)

Cheers
On 30 September 2015 at 15:30, David Marcillo <dmarc...@gmail.com> wrote:
I am new to json schema to pojo.  I downloaded source code to configure SchemaMapper generate() with the following :
Annotation style: GSON, Include getters and setters, Include constructors, etc ...  

The online example provides a configuration interface, any examples of how to accomplish this by calling the jsonschema2pojo jar  ?

--
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-users+unsub...@googlegroups.com.

Joe Littlejohn

unread,
Sep 30, 2015, 11:49:29 AM9/30/15
to jsonschema...@googlegroups.com
Is there a reason you want to generate three identical types?

Using $ref can produce some interesting results if you don't give each schema a single, definitive fully-qualified class name, because the output is dependent on the order in which things are read. I'd recommend using javaType inside #/definitions/facility.



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.

David Marcillo

unread,
Sep 30, 2015, 12:29:12 PM9/30/15
to jsonschema2pojo-users
Hi Joe,

I am also new to json schema, so I had to look up the javaType option.

just a little background ;
I am familiar with maintaining XML Schema, and generating a set of pojos to facilitate xml usage.
I am looking for tools that will help us maintain json schema and generate pojo to facilitate json usage.

In the previous case the developer can get confused by creating "Requestor" objects and using that aobject as "Shipper" or "Consignee".    It's a bit more clear to generate three objects that may be similar.

I am trying to figure out how in a json schema, i create a base object named "facility" and create other object like "Shipper" that has other fields specific to a "Shipper" role, as well as other "Consignee" object that extends "facility" and also has other fields.
 
Sorry for the long response .......  


--------------------------------------------------------------------------------------------------------------------------------------------------------
To unsubscribe from this group and stop receiving emails from it, send an email to jsonschema2pojo-users+unsub...@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-users+unsub...@googlegroups.com.

Joe Littlejohn

unread,
Sep 30, 2015, 12:46:25 PM9/30/15
to jsonschema...@googlegroups.com
Hi David,

javaType is a convenience that jsonschema2pojo provides, rather than part of JSON Schema. More info here:


The only way you can truly, consistently force this is to create 3 types is to not use a ref, and duplicate the schema instead.

I don't think your example is quite right though. If you use javaType in your #/definitions/facility schema, then what you can achieve is:

Facility requestor;
Facility shipper;
Facility consignee;

instead of

Requestor requestor
Requestor shipper;
Requestor consignee;

If these truly have a common type (facility) then I don't really think that this is confusing. If you're using a ref purely to avoid duplicating schema rules (and there is in fact no relationship between these types) then I agree this could be confusing.

One other option: jsonschema2pojo does support the extends keyword from JSON Schema v3. When it encounters this it creates a class to represent the parent and another class that extends the parent. Again, for consistent results I'd recommend you use javaType inside the target of the ref. So, I'm imagining something along the lines of:

"requestor" : {
  "extends" : {
    "$ref" : "#/definitions/facility"
  }
},
"shipper" : {
  "extends" : {
    "$ref" : "#/definitions/facility"
  }
},
"consignee" : {
  "extends" : {
    "$ref" : "#/definitions/facility"
  }
},

Hope this helps
 

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.

--
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.

David Marcillo

unread,
Sep 30, 2015, 1:55:06 PM9/30/15
to jsonschema2pojo-users
Hi Joe,

Yes !
I used "javaType" on the base object and it generated a base class.

I can not get the "extends" to work.

for example I used ;
"requestor": {
            "type" : "object",
            "extends" : {
                "$ref" : "#/definitions/facility"
            }
        },

I get error message "Path not present: extends" 
I tried created an external file in the same directory as the schema ... 
"#/facility_schema.txt""   but continue to get the same error.
To unsubscribe from this group and stop receiving emails from it, send an email to jsonschema2pojo-users+unsub...@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-users+unsub...@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-users+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages