【问题标题】:How can I define the sequence of properties in JSON Schema?如何在 JSON Schema 中定义属性序列?
【发布时间】:2015-07-29 04:29:40
【问题描述】:

我有以下 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 ----------*/
}

我需要找到用于在 UI 上显示这些字段的对象的 JSON 模式。 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 jsonschema


    【解决方案1】:

    在 JSON 模式对象中没有固有的属性顺序,因为它发生在常规 JSON 或 javascript 对象中。实际上,它不会改变验证语义。以下模式验证完全相同的对象集合:

    架构 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 项的有序定义。不幸的是,它迫使您将每个属性嵌套在一个对象中。

    【讨论】:

      【解决方案2】:

      订单超出了 json-schema 的范围。为此,您需要查看 json-forms https://jsonforms.io/,标准,它是 json-schema 的扩展。它由三个独立的 JSON 数据组成:

      1. 架构
      2. 价值观
      3. ui 模式

      ui-schema 包含有关 ui 和表示的信息,例如属性(字段)的顺序及其视觉表示,例如“隐藏”、“只读”。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-22
        • 2018-10-16
        • 1970-01-01
        • 2022-12-17
        • 1970-01-01
        • 1970-01-01
        • 2011-10-24
        • 1970-01-01
        相关资源
        最近更新 更多