JSON POJO 架构 - 枚举作为单独的 java 文件
JSON Schema to POJO - Enum as a separate java file
Type.json(枚举)
{
"type" : "string",
"$schema" : "http://json-schema.org/draft-04/schema#",
"title": "type resource",
"name": "type resource",
"description": "List of types",
"id" : "type:v1",
"enum" :
[
"ACC1",
"ACC2"
]
}
Pojo1.json
"properties":{
"type" : {
"$ref" : "Type.json"
},
}
Pojo2.json
"properties":{
"type" : {
"$ref" : "Type.json"
},
}
它不是为枚举创建一个单独的 java 文件,而是在其中一个 POJO 中创建一个枚举,并且这个内部 public 枚举由另一个 POJO 引用。
Pojo2.java
private com.Pojo1.Type type;
如何为枚举创建单独的 java 文件?谢谢
它看起来不像创建枚举,因为单独 类 在 jsonschema2pojo 中作为一个选项支持。
枚举的生成由org.jsonschema2pojo.rules.EnumRule
执行
其中的 javadoc 指出:
A Java {@link Enum} is created, with constants for each of the enum
values present in the schema. The enum name is derived from the nodeName,
and the enum type itself is created as an inner class of the owning type.
In the rare case that no owning type exists (the enum is the root of the
schema), then the enum becomes a public class in its own right.
听起来您需要提出功能请求
在编写 post 时,jsonschema2pojo 库中已经提供了一项功能。在我的调查过程中,我发现了这个 post 并且对无法引用生成的 class 或枚举的答案感到不满意。因此,我在 documentation 中进行了更深入的研究,它确实为我提供了一些对常用 json 模式规范(javaTypes、javaInterfaces、javaEnumNames、javaJsonView、javaName)的额外扩展属性。扩展 属性 javaType 的文档指出:
jsonschema2pojo supports an extension property 'javaType' that applies to schemas and allows you to specify a fully qualified name for your generated Java type.
鉴于这些知识,您的示例需要像这样重写:
Type.json(枚举)
{
"$schema" : "http://json-schema.org/draft-04/schema#",
"type" : "string",
"title": "type resource",
"name": "type resource",
"description": "List of types",
"id" : "type:v1",
"type_enum": {
"javaType" : "com.mycompany.TypeEnum",
"$ref": "TypeEnum.json#/definitions/type_enum"
}
}
我们现在用引用 $ref 替换您的枚举定义,包括专用包路径和 javaType[=37= 的 class 名称].
您的类型 class 中相应枚举的引用 json 架构将如下所示:
TypeEnum.json(枚举)
{
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"type_enum": {
"enum": [
"ACC1",
"ACC2"
],
"description": "All values for the enum in the type class."
}
}
}
Pojo 定义 Pojo1 和 Pojo2 保持不变。您甚至可以像这样在其他 classes 中引用枚举:
TypeV1_1.json(枚举)
{
"$schema" : "http://json-schema.org/draft-04/schema#",
"type" : "string",
"title": "type version 1.1 resource",
"name": "type version 1.1 resource",
"description": "List of version 1.1 types",
"id" : "type:v1_1",
"type_enum": {
"javaType" : "com.mycompany.TypeEnum",
"$ref": "TypeEnum.json#/definitions/type_enum"
}
}
让您始终只为定义的 javaType 生成一个枚举。
有可能我是这样做的:
"atributes": {
"description": "Additional message attributes",
"type": "object",
"properties": {
"action": {
"description": "Action to do over document",
"$ref" : "Action.json#"
}
},
"additionalProperties": false,
"examples": "{\"action\":\"UPSERT\"}"
}
和动作枚举:
{
"title": "Action",
"description": "Action description",
"type": "string",
"enum": [
"BATCH",
"DELETE",
"UNSUPPORTED"
],
"javaEnumNames": [
"UPSERT",
"DELETE",
"UNSUPPORTED"
]
}
Type.json(枚举)
{
"type" : "string",
"$schema" : "http://json-schema.org/draft-04/schema#",
"title": "type resource",
"name": "type resource",
"description": "List of types",
"id" : "type:v1",
"enum" :
[
"ACC1",
"ACC2"
]
}
Pojo1.json
"properties":{
"type" : {
"$ref" : "Type.json"
},
}
Pojo2.json
"properties":{
"type" : {
"$ref" : "Type.json"
},
}
它不是为枚举创建一个单独的 java 文件,而是在其中一个 POJO 中创建一个枚举,并且这个内部 public 枚举由另一个 POJO 引用。
Pojo2.java
private com.Pojo1.Type type;
如何为枚举创建单独的 java 文件?谢谢
它看起来不像创建枚举,因为单独 类 在 jsonschema2pojo 中作为一个选项支持。
枚举的生成由org.jsonschema2pojo.rules.EnumRule
执行其中的 javadoc 指出:
A Java {@link Enum} is created, with constants for each of the enum values present in the schema. The enum name is derived from the nodeName, and the enum type itself is created as an inner class of the owning type. In the rare case that no owning type exists (the enum is the root of the schema), then the enum becomes a public class in its own right.
听起来您需要提出功能请求
在编写 post 时,jsonschema2pojo 库中已经提供了一项功能。在我的调查过程中,我发现了这个 post 并且对无法引用生成的 class 或枚举的答案感到不满意。因此,我在 documentation 中进行了更深入的研究,它确实为我提供了一些对常用 json 模式规范(javaTypes、javaInterfaces、javaEnumNames、javaJsonView、javaName)的额外扩展属性。扩展 属性 javaType 的文档指出:
jsonschema2pojo supports an extension property 'javaType' that applies to schemas and allows you to specify a fully qualified name for your generated Java type.
鉴于这些知识,您的示例需要像这样重写:
Type.json(枚举)
{
"$schema" : "http://json-schema.org/draft-04/schema#",
"type" : "string",
"title": "type resource",
"name": "type resource",
"description": "List of types",
"id" : "type:v1",
"type_enum": {
"javaType" : "com.mycompany.TypeEnum",
"$ref": "TypeEnum.json#/definitions/type_enum"
}
}
我们现在用引用 $ref 替换您的枚举定义,包括专用包路径和 javaType[=37= 的 class 名称]. 您的类型 class 中相应枚举的引用 json 架构将如下所示:
TypeEnum.json(枚举)
{
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"type_enum": {
"enum": [
"ACC1",
"ACC2"
],
"description": "All values for the enum in the type class."
}
}
}
Pojo 定义 Pojo1 和 Pojo2 保持不变。您甚至可以像这样在其他 classes 中引用枚举:
TypeV1_1.json(枚举)
{
"$schema" : "http://json-schema.org/draft-04/schema#",
"type" : "string",
"title": "type version 1.1 resource",
"name": "type version 1.1 resource",
"description": "List of version 1.1 types",
"id" : "type:v1_1",
"type_enum": {
"javaType" : "com.mycompany.TypeEnum",
"$ref": "TypeEnum.json#/definitions/type_enum"
}
}
让您始终只为定义的 javaType 生成一个枚举。
有可能我是这样做的:
"atributes": {
"description": "Additional message attributes",
"type": "object",
"properties": {
"action": {
"description": "Action to do over document",
"$ref" : "Action.json#"
}
},
"additionalProperties": false,
"examples": "{\"action\":\"UPSERT\"}"
}
和动作枚举:
{
"title": "Action",
"description": "Action description",
"type": "string",
"enum": [
"BATCH",
"DELETE",
"UNSUPPORTED"
],
"javaEnumNames": [
"UPSERT",
"DELETE",
"UNSUPPORTED"
]
}