Generate Multiple POJO's from single json schema file

2.4k views Asked by At

I am using jsonschema2pojo to generate POJO's. Its working fine. But I want to generate multiple POJO's from single json schema file. Is this possible using jsonschema2pojo plugin.

One way to generate multiple POJO's is to provide multiple json schema files, but i don't want to do that. I want to provide only one json schema file as input.

I am providing following json schema as input:

{
  "Address": {
    "description": "AnAddressfollowingtheconventionofhttp: //microformats.org/wiki/hcard",
    "type": "object",
    "properties": {
        "post-office-box": {
            "type": "string"
        },
        "post-office-address": {
            "type": "string"
        }
    },
    "additionalProperties": false
  },
  "AddressDetails": {
    "description": "AnAddressDetailsfollowingtheconventionofhttp: //microformats.org/wiki/hcard",
    "type": "object",
    "properties": {
        "post-office-box": {
            "type": "string"
        },
        "post-office-address": {
            "type": "string"
        }
    }
  }
}

Above schema is valid schema but nothing gets created. I don't know If I am missing something or its just not possible using jsonschema2pojo.

Also is there a way to generate these entities at run-time?

If anyone has any thoughts on the same, do share.

3

There are 3 answers

0
Bhushan On BEST ANSWER

The solution was to split these into separate files, or reference these schemas from another file like:

{"$ref" : "schemas/myfile.json#AddressDetails"}

Thanx.

0
melladh On

I did a bit of an ugly workaround for this by letting jsonschema2pojo generate an outer "trash" object, which contained

"$schema": "http://json-schema.org/draft-04/schema#",
"description":"Do not use outer object, it's merely a schema wrapper for the inner objects.",
"id":"http://some-path",
"type":"object",
"properties": {
<all your objects go here>
}

...it will leave you with a trash object, but let you define all your objects in one schema. I would also really have liked a cleaner solution though.

0
dsk On

I used this website and I am able to generate multiple POJO from single JSON file. They also have ZIP option to download all the files at once.