java JSon 架构和继承

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27410216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:46:39  来源:igfitidea点击:

JSon schema and Inheritance

javajsoninheritancebindingjsonschema

提问by user3748879

I have searched on json schema with java bindings with inheritance and all searches led me to the usage of "allOf".

我搜索了带有继承的 java 绑定的 json 模式,所有搜索都让我使用了“allOf”。

Using allOf would potentially solve my problem, but I am wondering if there is a construct in json schema that I can use which will generate my java code with real java inheritance "B extends A" - rather than inlining all properties from A inside B ?

使用 allOf 可能会解决我的问题,但我想知道 json 模式中是否有我可以使用的构造,它将生成具有真正的 Java 继承“B 扩展 A”的 Java 代码 - 而不是内联 B 中 A 的所有属性?

I am wondering if this is even supported / doable or I am just dreaming. If not supported, I would be curious to know the reason.

我想知道这是否得到支持/可行,或者我只是在做梦。如果不支持,我很想知道原因。

回答by fge

OK, well, I am the author of both:

好吧,我是两者的作者:

  • the current JSON Schema validation spec;
  • and the Java library which is the most used for JSON Schema validation in Java today: json-schema-validator.
  • 当前的 JSON 模式验证规范;
  • 以及当今 Java 中最常用于 JSON Schema 验证的 Java 库:json-schema-validator

So I can answer your question, and the basic answer is no.

所以我可以回答你的问题,基本答案是否定的。

Why? Because there is no such thing as schema inheritance currently defined.

为什么?因为目前没有定义模式继承这样的东西。

When using allOf, you require that all schemas in allOfmatch; and if you are strict about what can exist in this or that JSON, you'll have added additionalPropertiesto false. As such, you cannot inherit.

使用时allOf,您需要allOf匹配所有模式;如果您对这个或那个 JSON 中可以存在的内容很严格,那么您将添加additionalPropertiesfalse. 因此,您不能继承。

The real solution is a mechanism I proposed for draft v5: the $mergeand $patchkeywords. These would allow to patch schemas with either of RFC 7386 or RFC 6902 (see herefor more info) and indeed implement schema inheritance.

真正的解决方案是我为 v5 草案提出的一种机制:$merge$patch关键字。这些将允许使用 RFC 7386 或 RFC 6902(有关更多信息,请参见此处)修补模式并确实实现模式继承。

In short:

简而言之:

  • if you set additionalPropertiesto false, and your basic JSON is an object, you won't be able to define additional object members;
  • with these two new keywords, you can.
  • 如果您设置additionalPropertiesfalse,并且您的基本 JSON 是一个对象,则您将无法定义其他对象成员;
  • 有了这两个新关键字,就可以了。

回答by myuce

"jsonschema2pojo" project contains notations for this purpose.

jsonschema2pojo”项目包含用于此目的的符号。

On the JSON schema, just include something like this;

在 JSON 模式上,只包含这样的内容;

"extendsJavaClass" : "com.somecompany.SomeBaseClass",

i.e.

IE

{
  "title": "....",
  "description": "....",
  "type": "object",
  "extendsJavaClass" : "com.somecompany.SomeBaseClass",
  "properties": {
    "...": {
      "items": {
        "$ref": "#/definitions/...."
      },
      "type": "array"
    }
    .......
}

then the class generated by the project will have its "extends" clause as;

那么项目生成的类将有它的“扩展”子句;

/**
 * ...
 * <p>
 * ...
 * 
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    ...
})
public class MyNewClass
    extends SomeBaseClass
{
...
}

Also you can search for similar notations in here.

您也可以在此处搜索类似的符号。

PS: Theese notations are not "standard JSON schema constructs". They are added for the sake of "just doing it" until a standard way of doing it is possible.

PS:这些符号不是“标准的 JSON 模式结构”。添加它们是为了“只是做它”,直到可以使用标准的方法。

Hope it helps..

希望能帮助到你..

回答by Ravi M

you have 3 options, I am using jsonschema2pojo : 1.0.0-alpha2

你有 3 个选项,我使用的是 jsonschema2pojo:1.0.0-alpha2

"extends": {
    "$ref": "MyObject.json"
}

or

或者

"extends": {
    "type": "object",
    "javaType": "com.mycompany.model.MyObject"
}

or

或者

 "extendsJavaClass" : "com.mycompany.model.MyObject"