JsonschemaUtil.java
2.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.skua.common.util.jsonschema;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class JsonschemaUtil {
/**
* 生成JsonSchema
*
* @param descrip
* @param propertyList
* @return
*/
public static JSONObject getJsonSchema(JsonSchemaDescrip descrip, List<CommonProperty> propertyList) {
JSONObject obj = new JSONObject();
obj.put("$schema", descrip.get$schema());
obj.put("type", descrip.getType());
obj.put("title", descrip.getTitle());
List<String> requiredArr = descrip.getRequired();
obj.put("required", requiredArr);
JSONObject properties = new JSONObject();
for (CommonProperty commonProperty : propertyList) {
Map<String, Object> map = commonProperty.getPropertyJson();
properties.put(map.get("key").toString(), map.get("prop"));
}
obj.put("properties", properties);
//鬼知道这里为什么报错 com.skua.modules.system.model.DictModel cannot be cast to com.skua.modules.system.model.DictModel
//log.info("---JSONSchema--->"+obj.toJSONString());
return obj;
}
/**
* 生成JsonSchema 用于子对象
* @param title 子对象描述
* @param requiredArr 子对象必填属性名集合
* @param propertyList 子对象属性集合
* @return
*/
public static JSONObject getSubJsonSchema(String title,List<String> requiredArr,List<CommonProperty> propertyList) {
JSONObject obj = new JSONObject();
obj.put("type", "object");
obj.put("view", "tab");
obj.put("title", title);
if(requiredArr==null) {
requiredArr = new ArrayList<String>();
}
obj.put("required", requiredArr);
JSONObject properties = new JSONObject();
for (CommonProperty commonProperty : propertyList) {
Map<String, Object> map = commonProperty.getPropertyJson();
properties.put(map.get("key").toString(), map.get("prop"));
}
obj.put("properties", properties);
//log.info("---JSONSchema--->"+obj.toString());
return obj;
}
}