【问题标题】:jsonschema2pojo: referencing objects of the same typejsonschema2pojo:引用相同类型的对象
【发布时间】:2015-05-17 18:18:22
【问题描述】:

我需要从 JSON 模式文件生成 Java 类并且遇到了 jsonschema2pojo。但是,我在使用ref 关键字时遇到了“问题”。

例如,如果我使用来自http://spacetelescope.github.io/understanding-json-schema/structuring.html#extending 的以下架构:

{
  "$schema": "http://json-schema.org/draft-04/schema#",

  "definitions": {
    "address": {
      "type": "object",
      "properties": {
        "street_address": { "type": "string" },
        "city":           { "type": "string" },
        "state":          { "type": "string" }
      },
      "required": ["street_address", "city", "state"]
    }
  },

  "type": "object",

  "properties": {
    "billing_address": { "$ref": "#/definitions/address" },
    "shipping_address": { "$ref": "#/definitions/address" }
  }
}

正如预期的那样,它生成了一个你想怎么称呼它的类,包含一个属性billingAddress 和一个属性shippingAddress

但是,它也生成了两个单独的类BillingAddressShippingAddress,尽管这两个属性都引用了address。因此,我宁愿拥有Address 类型的两个属性。

这可以通过 jsonschema2pojo 实现吗?

【问题讨论】:

    标签: java json jsonschema2pojo


    【解决方案1】:

    更新

    在从here 对 javaType 有了更好的理解之后。我只需在您的地址定义中添加一个 javaType 即可获得预期的结果。

    {
      "$schema": "http://json-schema.org/draft-04/schema#",
    
      "definitions": {
          "address": {
          "type": "object",
          "javaType": "Address",
          "properties": {
            "street_address": { "type": "string" },
            "city":           { "type": "string" },
            "state":          { "type": "string" }
          },
          "required": ["street_address", "city", "state"]
        }
      },
      "type": "object",
      "properties": {
        "billing_address": { "$ref": "#/definitions/address" },
        "shipping_address": { "$ref": "#/definitions/address" }
      }
    }
    

    用两个文件回答

    您需要在您的 Address.json 中使用 javaType 并使用 $ref 作为您的 billing_address 和送货地址。我建议您将地址定义分成一个单独的 json,然后在您的 billing_address 和 shipping_address 中使用它。


    地址.json

    {
        "$schema": "http://json-schema.org/draft-03/hyper-schema",
        "additionalProperties": false,
        "javaType": "whatever-package-name-you-have.Address"
        "type": "object",
        "properties": {
        "street_address": { "type": "string", "required":true},
        "city":           { "type": "string", "required":true },
        "state":          { "type": "string", "required":true }
      }
    }
    

    MainClass.json

    {
        "$schema": "http://json-schema.org/draft-03/hyper-schema",
        "additionalProperties": false,
        "type": "object",
        "properties": {
         "billing_address": {
               "$ref":"Address.json",
               "type": "object",
               "required": false
              },
         "shipping_address": {
               "$ref":"Address.json",
               "type": "object",
               "required": false
              }
         }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-09
      • 1970-01-01
      • 2012-08-13
      • 2013-02-17
      • 1970-01-01
      • 2014-09-08
      • 1970-01-01
      • 2012-06-30
      相关资源
      最近更新 更多