gpt4 book ai didi

json - 从 JSON 模式生成 Java 类

转载 作者:行者123 更新时间:2023-12-01 06:35:21 24 4
gpt4 key购买 nike

我想从给定的 JSON 模式生成 JAVA 类 draft 4 version
我评估了几个工具和 jsonschema2pojo被发现很有用。但它仅支持 json 模式草案 3 版本(尽管 json 模式草案 4 在他们的路线图中)。
任何人都可以建议我从 json 模式生成 java 类的工具或方法(符合 json 模式草案 4)
?
提前致谢。

最佳答案

你可以试试 cog ,一个用 Ruby 编写的通用代码生成器。我在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 主页以获取文档。

关于json - 从 JSON 模式生成 Java 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15499459/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com