jsonschema2pojo:引用相同类型的对象
jsonschema2pojo: referencing objects of the same type
我需要从 JSON 模式文件生成 Java classes 并遇到了 jsonschema2pojo。但是,我在使用 ref
关键字时遇到了 "problem"。
例如,如果我使用 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" }
}
}
正如预期的那样,它生成了一个名为 class 的名称,包含一个属性 billingAddress
和一个属性 shippingAddress
。
但是,它还生成了两个单独的 classes BillingAddress
和 ShippingAddress
,即使这两个属性都引用了 address
。因此,我宁愿同时拥有 Address
类型的两个属性。
这可以用 jsonschema2pojo 实现吗?
更新
通过 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 并在 billing_address 和运输中使用 $ref地址。我建议您将地址定义分离到一个单独的 json 中,然后在 billing_address 和 shipping_address.
中使用它
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
}
}
}
我需要从 JSON 模式文件生成 Java classes 并遇到了 jsonschema2pojo。但是,我在使用 ref
关键字时遇到了 "problem"。
例如,如果我使用 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" }
}
}
正如预期的那样,它生成了一个名为 class 的名称,包含一个属性 billingAddress
和一个属性 shippingAddress
。
但是,它还生成了两个单独的 classes BillingAddress
和 ShippingAddress
,即使这两个属性都引用了 address
。因此,我宁愿同时拥有 Address
类型的两个属性。
这可以用 jsonschema2pojo 实现吗?
更新
通过 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 并在 billing_address 和运输中使用 $ref地址。我建议您将地址定义分离到一个单独的 json 中,然后在 billing_address 和 shipping_address.
中使用它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
}
}
}