Avoid the underscore classes

1,277 views
Skip to first unread message

hari...@gmail.com

unread,
Apr 10, 2015, 9:25:15 AM4/10/15
to jsonschema...@googlegroups.com
I am trying to generate pojos for a json like this 

{
   
"order": {
       
"id": "1234",
       
"objType": "order",
       
"priceInfo": {
           
"currencyFormat": "$0.00",
           
"currency": "USD",
           
"amount": "70.91",
           
"isDiscounted": true
       
},
       
"shippingGroups": [
           
{
               
"id": "1234",
               
"objType": "shippingGroup",
               
"priceInfo": {
                   
"currencyFormat": "$0.00",
                   
"currency": "USD"
               
}
           
}
       
]
   
}
}

the generator ends up creating two clasees PriceInfo and PriceInfo_

What would be the best way to just generate one PriceInfo. Also in some cases i might have the second priceInfo object have an extra property. I would like to have one PriceInfo with 3 properties rather then two price info objects.


I am willing to modify the code and submit a Pull request.

Thanks in advance for your time.
Harish

Joe Littlejohn

unread,
Apr 10, 2015, 9:37:19 AM4/10/15
to jsonschema...@googlegroups.com

Hi Harish. The way to achieve this is to define priceInfo once in the schema, then use a $ref when you want to refer to priceInfo again.

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

hari...@gmail.com

unread,
Apr 14, 2015, 11:27:57 PM4/14/15
to jsonschema...@googlegroups.com
Thanks Joe. Appreciate the tip.  i got it working correctly with the schema below. Hopefully it will benefit someone.


{
   
"$schema": "http://json-schema.org/draft-04/schema#",
   
"id": "/",
   
"type": "object",
   
"properties": {
       
"order": {
           
"id": "order",
           
"type": "object",
           
"properties": {
               
"id": {
                   
"id": "id",
                   
"type": "string"
               
},
               
"objType": {
                   
"id": "objType",
                   
"type": "string"
               
},
               
"priceInfo": {
                   
"$ref": "#/definitions/priceInfo"
               
},
               
"additionalProperties": false
           
},
           
"shippingGroups": {
               
"id": "shippingGroups",
               
"type": "array",
               
"items": {
                   
"id": "0",
                   
"type": "object",
                   
"properties": {
                       
"id": {
                           
"id": "id",
                           
"type": "string"
                       
},
                       
"objType": {
                           
"id": "objType",
                           
"type": "string"
                       
},
                       
"priceInfo": {
                           
"$ref": "#/definitions/priceInfo"
                       
}
                   
},
                   
"additionalProperties": false
               
}
           
}
       
},
       
"additionalProperties": false
   
},
   
"additionalProperties": false,
   
"definitions": {
       
"priceInfo": {
           
"id": "priceInfo",
           
"type": "object",
           
"properties": {
               
"currencyFormat": {
                   
"id": "currencyFormat",
                   
"type": "string"
               
},
               
"currency": {
                   
"id": "currency",
                   
"type": "string"
               
}
           
},
           
"additionalProperties": false
       
}
   
}
}

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

HUANG Wei

unread,
May 13, 2015, 11:36:02 PM5/13/15
to jsonschema...@googlegroups.com, hari...@gmail.com
Harish, I have the same problem, how do you generate the JSON schema from JSON?

hari...@gmail.com

unread,
May 13, 2015, 11:56:37 PM5/13/15
to jsonschema...@googlegroups.com, hari...@gmail.com
You can use this site 


And when i use it , i select Single Schema and uncheck everything.

HUANG Wei

unread,
May 14, 2015, 12:20:32 AM5/14/15
to jsonschema...@googlegroups.com, hari...@gmail.com
Hi again, what about the $ref part? It's it done manually?

Dan Tran

unread,
Sep 25, 2015, 2:28:05 AM9/25/15
to jsonschema2pojo-users, hari...@gmail.com
I have one more interesting scenario

{
  "type" : "object",
  "properties" : {
    "attributes" : {
      "type" : "object",
      "additionalProperties" : {
        "type" : "string"
      }
    },
    "links" : {
      "type" : "array",
      "required" : true,
      "items" : {
        "type" : "object",
        "link": {
          "$ref": "#/definitions/link"
        }
      }
    },
    "id" : {
      "type" : "string",
      "required" : true
    },
  "definitions": {
    "link": {
      "type" : "object",
      "properties" : {
        "rel" : {
          "type" : "string"
        },
        "href" : {
          "type" : "string"
        },
        "id" : {
          "type" : "string"
        }
      }
    }
  }
}

It generates a Link classname  ( for the array) and Link_ for the item class.  Is there a way to force the array class to be named as Links???

Very much in need for this to work

Thanks


Dan Tran

unread,
Sep 25, 2015, 4:37:36 AM9/25/15
to jsonschema2pojo-users, hari...@gmail.com
2 more issues

  1. The generated java class for 'links' is very vague, it has nothing to with the array of Link_ class
  2. If I place the 'link' schema to another file and reference it via "$ref": "Link.json". Then only one class generated - the vague array class

Thanks

-Dan

thu...@emc.com

unread,
Sep 25, 2015, 11:51:44 AM9/25/15
to jsonschema2pojo-users, hari...@gmail.com
You can use javaType to force jsonschema2pojo to generate the name you desire.

-Thuy

thu...@emc.com

unread,
Sep 25, 2015, 12:23:29 PM9/25/15
to jsonschema2pojo-users, hari...@gmail.com
Here is my revised schema for your example.  I also attached the Example.jsonschema based on the content below and its generated Java classes.

{
  "type" : "object",
  "properties" : {
    "attributes" : {
      "type" : "object",
      "additionalProperties" : {
        "type" : "string"
      }
    },
    "links" : {
      "type" : "array",
      "required" : true,
      "items" : {
        "type" : "object",
        "link": {
          "$ref": "#/definitions/Link"

        }
      }
    },
    "id" : {
      "type" : "string",
      "required" : true
    }
  },
  "additionalProperties" : false,
 
  "definitions": {
    "Link": {

      "type" : "object",
      "properties" : {
        "rel" : {
          "type" : "string"
        },
        "href" : {
          "type" : "string"
        },
        "id" : {
          "type" : "string"
        }
      },
      "additionalProperties" : false

    }
  }
}

On Thursday, September 24, 2015 at 11:28:05 PM UTC-7, Dan Tran wrote:
sample.zip

Dan Tran

unread,
Sep 25, 2015, 12:50:48 PM9/25/15
to jsonschema2pojo-users, hari...@gmail.com, thu...@emc.com
the working schema is 

{
  "type" : "object",
  "properties" : {
    "attributes" : {
      "type" : "object",
      "additionalProperties" : {
        "type" : "string"
      }
    },
    "links" : {
      "type" : "array",
      "required" : true,
      "items" : {
          "$ref": "#/definitions/Link"
        }
      }
    },
    "id" : {
      "type" : "string",
      "required" : true
    }
  },
  "additionalProperties" : false,
  
 {

      "type" : "object",
      "properties" : {
        "rel" : {
          "type" : "string"
        },
        "href" : {
          "type" : "string"
        },
        "id" : {
          "type" : "string"
        }
      },
      "additionalProperties" : false

    }
  }
}

for the external file ref to work, the content is

{

      "type" : "object",
      "properties" : {
        "rel" : {
          "type" : "string"
        },
        "href" : {
          "type" : "string"
        },
        "id" : {
          "type" : "string"
        }
      },
      "additionalProperties" : false

 }

There was mistake of my part in this external file

thu...@emc.com

unread,
Sep 25, 2015, 12:54:41 PM9/25/15
to jsonschema2pojo-users, hari...@gmail.com, thu...@emc.com
You don't need an external file for Attributes.  You could include the definitions in the "definitions" section.  See my attached zip that contains the revised sample for your schema.

sandeep...@gmail.com

unread,
Nov 25, 2015, 5:51:58 AM11/25/15
to jsonschema2pojo-users, hari...@gmail.com
If I have only JSON and not JSONSchema how to avoid creation of multiple classes and create just one PriceInfo class.

Dan Tran

unread,
Nov 25, 2015, 12:33:48 PM11/25/15
to jsonschema2pojo-users, hari...@gmail.com
There is a couple of solution discussed previously on the same thread.  Have you looked thru it?

Dan Tran

unread,
Nov 25, 2015, 12:47:03 PM11/25/15
to jsonschema2pojo-users, hari...@gmail.com
How about using javaType.


On Friday, April 10, 2015 at 6:25:15 AM UTC-7, hari...@gmail.com wrote:

Sandeep sandy

unread,
Nov 25, 2015, 9:48:05 PM11/25/15
to jsonschema...@googlegroups.com, hari...@gmail.com
No one answered for the JSON, Some one answered for JSONSchema but not only for JSON.

--
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/F4LEdCONdaI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jsonschema2pojo-...@googlegroups.com.



--
SANDEEP SANDY

Sandeep sandy

unread,
Nov 27, 2015, 3:58:25 AM11/27/15
to jsonschema2pojo-users, hari...@gmail.com
Can You please give me the answer how to avoid underscore classes( _ ) from JSON (not from JSONSchema)...


On Friday, April 10, 2015 at 6:55:15 PM UTC+5:30, hari...@gmail.com wrote:

Nitesh Sawant

unread,
Dec 17, 2024, 12:49:50 PM12/17/24
to jsonschema2pojo-users
 Step1 :  Remove java files with names having __
 Step2 : Replace __[0-9]+ occurences from java files
Reply all
Reply to author
Forward
0 new messages