如何定义JSON模式中的属性顺序? [英] How can I define the sequence of properties in JSON Schema?

查看:536
本文介绍了如何定义JSON模式中的属性顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下java对象:

I have following java Object:

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模式中属性的顺序.

I need to find the JSON schema for this object which is used to display these fields on UI. The order of fields in UI is determined by the order of properties in JSON schema generated.

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

I have generated the schema using following code:

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对象中定义的字段的顺序不匹配,这在我的情况下是必需的.

As it can be seen that the order of properties in JSON schema does not match the order of fields defined in Java object which is necessary in my case.

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

So how can I determine the sequence of the properties?

推荐答案

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

There is not an intrinsic order of properties in a JSON schema object, as it happens with a regular JSON or javascript object. Indeed, it does not change the validation semantics. The following schemas validate exactly the same collection of objects:

模式1:

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

模式2:

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

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

If you want to preserve some ordering you need to do it within and array. You could achieve this by using an array of objects instead of an object in the items clause. An example:

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

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

This way you get an ordered definition of UI items. Unfortunately, it forces you to nest each single property in an object.

这篇关于如何定义JSON模式中的属性顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆