gpt4 book ai didi

json - 如何定义 JSON 架构中的属性序列?

转载 作者:行者123 更新时间:2023-12-02 19:55:13 24 4
gpt4 key购买 nike

我有以下 java 对象:

ProcessBean.java

import java.util.List;
import com.fasterxml.jackson.annotation.JsonRootName;

@JsonRootName(value = "process")
public class ProcessBean{
private Integer id;
private String name;
private String description;
private String user;
private String executePermissions;
private String createdDtm;
private String updatedDtm;
private Process tpProcess;
private List<ProcessParamBean> processParameters;

/* --------Getters and Setters ----------*/
}

我需要找到该对象的 JSON 架构,用于在 UI 上显示这些字段。 UI 中字段的顺序由生成的 JSON 架构中属性的顺序决定。

我使用以下代码生成了架构:

DataSpec.java

import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
import com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper;

public class DataSpec {
public static <T> String getDataSpec(Class<T> clazz) {
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
MetauiVisitorContext obj = new MetauiVisitorContext();
visitor.setVisitorContext(obj);
try {
mapper.acceptJsonFormatVisitor(clazz, visitor);
JsonSchema schema = visitor.finalSchema();
return mapper.writeValueAsString(schema);
} catch (JsonProcessingException e) {
e.printStackTrace();
return null;
}
}
}

MetauiVisitorContext.java

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.module.jsonSchema.factories.VisitorContext;

public class MetauiVisitorContext extends VisitorContext{
@Override
public String getSeenSchemaUri(JavaType aSeenSchema) {
return null;
}
}

生成的架构如下所示:

{
"type": "object",
"id": "urn:jsonschema:com:restservices:api:jsonbeans:ProcessBean",
"properties": {
"updatedDtm": {
"type": "string"
},
"createdDtm": {
"type": "string"
},
"name": {
"type": "string"
},
"tpProcess": {
"type": "object",
"id": "urn:jsonschema:com:restservices:api:jsonbeans:Process",
"properties": {
"name": {
"type": "string"
},
"id": {
"type": "integer"
}
}
},
"description": {
"type": "string"
},
"id": {
"type": "integer"
},
"processParameters": {
"type": "array",
"items": {
"type": "object",
"id": "urn:jsonschema:com:restservices:api:jsonbeans:ProcessParamBean",
"properties": {
"updatedDtm": {
"type": "string"
},
"defaultValue": {
"type": "string"
},
"createdDtm": {
"type": "string"
},
"masterParam": {
"type": "object",
"id": "urn:jsonschema:com:restservices:api:jsonbeans:MasterParamBean",
"properties": {
"updatedDtm": {
"type": "string"
},
"createdDtm": {
"type": "string"
},
"name": {
"type": "string"
},
"description": {
"type": "string"
},
"id": {
"type": "integer"
}
}
},
"name": {
"type": "string"
},
"description": {
"type": "string"
},
"processParamId": {
"type": "integer"
},
"id": {
"type": "integer"
},
"userPrompted": {
"type": "boolean"
},
"tags": {
"type": "string"
}
}
}
},
"executePermissions": {
"type": "string"
},
"user": {
"type": "string"
}
}
}

可以看出,JSON 模式中的属性顺序与 Java 对象中定义的字段顺序不匹配,这在我的情况下是必需的。

那么如何确定属性的顺序?

最佳答案

与常规 JSON 或 javascript 对象不同,JSON 架构对象中的属性没有内在顺序。事实上,它并没有改变验证语义。以下架构验证完全相同的对象集合:

架构1:

{
"properties" : {
"prop1" : {
"type" : "string"
},
"prop2" : {
"type" : "number"
}
}
}

架构2:

{
"properties" : {
"prop2" : {
"type" : "number"
},
"prop1" : {
"type" : "string"
}
}
}

如果你想保留一些顺序,你需要在数组中进行。您可以通过在 items 子句中使用对象数组而不是对象来实现此目的。一个例子:

{
"type" : "array",
"items" : [{
"properties" : {
"prop2" : {
"type" : "number"
}
}
}, {
"properties" : {
"prop1" : {
"type" : "string"
}
}
}
]
}

通过这种方式,您可以获得 UI 项目的有序定义。不幸的是,它迫使您将每个属性嵌套在一个对象中。

关于json - 如何定义 JSON 架构中的属性序列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31691247/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com