JSON模式和继承 [英] JSon schema and Inheritance

查看:612
本文介绍了JSON模式和继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索过JSON模式与继承Java绑定和所有搜索导致我allOf的用法。

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

使用allOf将有可能解决我的问题,但我想知道如果在JSON模式的构造,我可以使用,这将产生我的Java code。与真正的Java继承B扩展A - 而不是内联的所有从A里面乙属性?

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 ?

我想知道,如果这甚至支持/可行或我只是在做梦。如果不支持,我会很好奇,想知道原因。

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.

推荐答案

OK,很好,我既笔者:

OK, well, I am the author of both:


  • 当前JSON模式验证规范;

  • 和Java库,是最常用的在Java今天JSON Schema验证: JSON-架构验证

  • 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.

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

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

为什么呢?因为作为架构继承没有这样的事当前定义的。

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

在使用 allOf ,您需要在 allOf 匹配的所有模式;如果你是严密的关于什么可以在这个或那个JSON存在,你就已经添加了 additionalProperties 。因此,你不能继承。

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

真正的解决方案是我提出的V5草案机制: $合并 $补丁关键字。这将允许修补模式与任一RFC 7386或RFC 6902(请参见这里获取更多信息),并确实执行架构继承

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

在短:


  • 如果您设置 additionalProperties 和基本的JSON是一个对象,你将不能够定义附加对象成员;

  • 这两个新的关键字,就可以了。

  • if you set additionalProperties to 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.

这篇关于JSON模式和继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆