从JSON模式生成java类

时间:2013-03-19 12:25:07

标签: json code-generation jsonschema

我想从给定的JSON Schema draft 4 version

生成JAVA类

我评估了几个工具,发现jsonschema2pojo很有用。但它仅支持json schema draft-3版本(尽管json schema draft 4在他们的路线图中)。

任何人都可以建议我使用json模式生成java类的工具或方法(符合json schema draft4) ? 提前谢谢。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用Ruby编写的通用代码生成器cog。我在github上放了一个名为json2java的简单项目,演示了如何使用cog从json数据生成Java类。

不确定你想要做什么,但这就是我的假设。 json数据看起来像这样

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

相应的Java类看起来像这样

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: }
    }

}

如果您想尝试使用cog,请按照此gem install cog进行安装,然后运行此cog gen之类的生成器。查看cog主页以获取文档。

相关问题