[英]jsonschema2pojo: referencing objects of the same type
I need to generate Java classes from a JSON schema file and came across jsonschema2pojo. 我需要从JSON模式文件生成Java类,并遇到jsonschema2pojo。 However, I encountered a "problem" when using the
ref
keyword. 但是,在使用
ref
关键字时遇到了“问题”。
For example, if I use the following schema from http://spacetelescope.github.io/understanding-json-schema/structuring.html#extending : 例如,如果我使用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" }
}
}
As expected, it generated a class named whatever you want to call it, containing an attribute billingAddress
and an attribute shippingAddress
. 正如所料,它生成了一个名为您想要调用它的类,包含属性
billingAddress
和属性shippingAddress
。
However, it also generated two separate classes BillingAddress
and ShippingAddress
even though both attributes are referencing to address
. 但是,它还生成了两个单独的类
BillingAddress
和ShippingAddress
即使两个属性都引用了address
。 Hence, I would rather have both attributes of type Address
. 因此,我宁愿同时具有
Address
类型的两个属性。
Is this possible to achieve with jsonschema2pojo? 这可以用jsonschema2pojo实现吗?
After getting a better understanding of javaType from here . 从这里更好地了解javaType之后。 I get the expected result by just adding a javaType in your Address definition.
通过在地址定义中添加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" }
}
}
You need to use javaType in your Address.json and use $ref for your billing_address and shipping address. 您需要在Address.json中使用javaType并使用$ ref作为billing_address和送货地址。 I would suggest you to separate the address definition into a separate json and then use that in your billing_address and shipping_address.
我建议你将地址定义分成一个单独的json,然后在你的billing_address和shipping_address中使用它。
{
"$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 }
}
}
{
"$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
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.