【问题标题】:JSON Schema multiple instances of a propertyJSON Schema 属性的多个实例
【发布时间】:2020-07-11 10:43:52
【问题描述】:

我正在尝试定义一个 json 模式,其中有多个属性,Java 应用程序端点的同一对象的实例。

我正在尝试做的一个简单示例是这样的

{
"$schema": "http://json-schema.org/draft-07/schema",
"type": "object",

"properties": {
    "last": {
        "$ref": "#/$def/node"
    },
    "next": {
        "$ref": "#/$def/node"
    }
},
"$def": {
    "node": {
        "type": "object",
        "properties": {
            "item": "object"
        }
    }
}}

问题在于,当我将其传递给 jsonschema2pojo 时,它会将其解释为 lastnext 是不同的类,而不是公共 node 对象的实例。

------------------------com.example.Last.java----- ------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Last {

@SerializedName("item")
@Expose
public Object item;

}

------------------------com.example.Next.java----- ------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Next {

@SerializedName("item")
@Expose
public Object item;

}

-----------------------------------com.example.Node.java----- ------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Node {

@SerializedName("last")
@Expose
public Last last;
@SerializedName("next")
@Expose
public Next next;

}

是否可以在 json-schema 中有多个属性实例,我会表达吗?或者这是 jsonschema2pojo 工具/插件的限制?或者 json-schema 是否需要将多个实例放入一个数组中?

【问题讨论】:

  • JSON Schema 与 JSON 有关。 JSON Schema 本身与类或任何形式的代码生成无关。与代码生成有关的任何事情都取决于您选择使用的单个工具。可能是它记录了执行您想要的特定方式,但很可能不支持您想要的。抱歉,我无法提供更多帮助。
  • 该工具有一个我们可以使用的扩展,但需要架构中的特定条目 - 使其成为工具特定而不是通用架构。不过,知道这是一个工具限制会有所帮助。回到白板...
  • 希望几年后这应该会更容易......但是,是的,几年对今天没有帮助=]
  • @JasonWarner 如果您使用definitions 而不是$def,您的运气可能会更好。在 2019-09 草案中,definitions 已更改为 $defs,但您使用的是 Draft-07。使用非标准关键字进行定义可能会混淆工具。
  • @JasonDesrosiers 我尝试在架构的原始草案中使用definitions。仅在参考规范后,我才将其更改为 $def。它对工具没有任何影响——它对它们都一视同仁。似乎更改只是对约定的标准化,而不是对映射到源模式的底层功能的更改。

标签: java json jsonschema jsonschema2pojo


【解决方案1】:

您似乎必须通过在定义中声明特定的"javaType" 来强制使用相同的类型,不幸的是,这是特定于工具的(根据您的 cmets,您希望避免这种情况)。

以下工作对jsonschema2pojo.org

输入:JSON 架构

{
  "type": "object",
  "properties": {
      "last": {
        "$ref": "#/definitions/node"
      },
      "next": {
        "$ref": "#/definitions/node"
      }
  },
  "definitions": {
      "node": {
          "javaType" : "com.example.Node",
          "type": "object",
          "properties": {
            "item": {}
          }
      }
  }
}

输出:com.example.Example.java

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example {

    @SerializedName("last")
    @Expose
    public Node last;
    @SerializedName("next")
    @Expose
    public Node next;

}

输出:com.example.Node.java

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Node {

    @SerializedName("item")
    @Expose
    public Object item;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-24
    • 2012-07-28
    • 2014-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-22
    • 1970-01-01
    相关资源
    最近更新 更多