6

I would like to generate JAVA classes from a given JSON Schema draft 4 version

I evaluated couple of tools and jsonschema2pojo was found to be useful. But it supports json schema draft-3 version only(although json schema draft 4 is in their roadmap).

Can anyone suggest me a tool or a way to generate java classes from a json schema (compliant to json schema draft4) ? Thanks in advance.

Community
  • 1
  • 1
Saraswathi
  • 61
  • 1
  • 3

1 Answers1

0

You might try cog, a general purpose code generator written in Ruby. I put a simple project on github called json2java which demonstrates how cog might be used to generate Java classes from json data.

Not sure exactly what you want to do, but here is what I assumed. The json data would look something like this

{
    "classname": "Sample",
    "methods": [
        {
            "name": "foo",
            "rtype": "void",
            "params": [
                {
                    "name": "arg1",
                    "type": "int"
                }
            ]
        },
        {
            "name": "bar",
            "rtype": "int",
            "params": []
        }
    ]
}

And the corresponding Java class would look something like this

public class Sample {

    void foo(int arg1) {
// keep: foo {
        // While the interface in this example is generated,
        // the method bodies are preserved between multiple invocations
        // of the generator.

        // It doesn't have to be done this way, the method bodies can be
        // generated aswell, all depends on what your json data encodes
// keep: }
    }

    int bar() {
// keep: bar {
        return 1;
// keep: }
    }

}

If you want to try cog, install it like this gem install cog, and run generators like this cog gen. Check out the cog homepage for documentation.

Kevin Tonon
  • 965
  • 10
  • 18
  • Thanks Kevin.I am looking to generate java classes using maven from a json "schema"(not json string) that complies withe json schema draft 4 version.I will look into the tool you have suggested , if it helps me. – Saraswathi Apr 12 '13 at 05:26
  • Yes, the question wasn't parsing json alone, but generating a class using the JSON Schema, which is a formal specification of the structure of the JSON and supports validation, etc. – sofend Nov 08 '17 at 07:51