JSONTools Validator的使用
- 此包的主要用途:
- Parser: Parse JSON text files and convert these to a Java model.
- Renderer: Render a Java representation into text.
- Serializer: Serialize plain POJO clusters to a JSON representation. The goal is to provide a serializing mechanism which can cope with all kinds of Java datastructures (recursion, references, primitive types, ...) .
- Mapper: Map POJO to JSON, this time the JSON text should be as clean as possible. This tool is the best choice when data has to be communicated between Java and other programming languages who can parse JSON.
- Validator: Validate the contents of a JSON file using a JSON schema. </ol>
以下來介紹一下Validator的使用
1.可以查看jsontools-core-1.7-sources.jar文件中的源代碼validator-validator.json,這里介紹了json schema驗證文件的寫法。
(1)name:規則名稱
(2)type:驗證的類型,包括let、and、or、array、properties、ref、content、string、number、bool等
(3)let類型:
*后指定的是let所執行的驗證規則,通常這是驗證文件中的最外層元素,并告訴當驗證執行時,執行的是哪個規則;
rules是一個數組,里面存放所定義的規則
(4)and類型:
rules后指定的是一個數組,內是幾個規則,它們之間是and的關系
(5)or類型:
rules后指定的是一個數組,內是幾個規則,它們之間是or的關系
(6)array類型:
代表數組
(7)properties類型:
pairs后指定的是一個數組,其內可以指定屬性;key指定屬性名,optional指定屬性是否可選,rule指定屬性的規則
(8)ref類型:
可以引用文件中定義的規則;
*后可以指定引用的規則名稱
(9)content類型:
內容類型;
rule后可以指定content的規則
(10)string類型:
代表字符串
(11)number類型:
代表數字
(12)bool類型:
代表布爾值
另外,還有其他的規則類型,可以分析com.sdicons.json.validator包,來得知其中的含義。
2.簡單模式
假設json格式為:
{
"links":[{"text":"文本","url":"鏈接"},{"text":"文本","url":"鏈接"}]
}
則相應的json schema為:
{
"name":"links-validator",
"type":"let",
"*":"links-rule",
"rules":
[
{
"name":"link-rule",
"type":"properties",
"pairs":[
{"key":"text","optional":false,"rule":{"type":"string"}},
{"key":"url","optional":false,"rule":{"type":"string"}}
]
}
,
{
"name":"links-rule",
"type":"properties",
"pairs":[
{"key":"links","optional":false,"rule":{
"type":"and",
"rules":[
{"type":"array"},
{"type":"content","rule":{"type":"ref","*":"link-rule"}}
]
}
}
]
}
]
}
3.復雜模式
假設json格式為:
{
"actors":[{"text":"文字","url":"鏈接"},{"text":"文字","url":"鏈接"}],
"targets":[{"text":"文字","url":"鏈接"},{"text":"文字","url":"鏈接"}],
"resources":[{"text":"文字","url":"鏈接"},{"text":"文字"},{"text":"文字","url":"鏈接","src":"圖片路徑"}],
"props":[{"key":"key1","value":"value1"},{"key":"key2","value":"value2"}]
}
則相應的json schema為:
{
"name":"body-validator",
"type":"let",
"*":"body-rule",
"rules":
[
{
"name":"link-rule",
"type":"properties",
"pairs":[
{"key":"text","optional":false,"rule":{"type":"string"}},
{"key":"url","optional":false,"rule":{"type":"string"}}
]
}
,
{
"name":"msg-rule",
"type":"properties",
"pairs":[
{"key":"text","optional":false,"rule":{"type":"string"}}
]
}
,
{
"name":"image-rule",
"type":"properties",
"pairs":[
{"key":"text","optional":false,"rule":{"type":"string"}},
{"key":"url","optional":false,"rule":{"type":"string"}},
{"key":"src","optional":false,"rule":{"type":"string"}}
]
}
,
{
"name":"actor-rule",
"type":"and",
"rules":[
{"type":"array"},
{"type":"content","rule":{"type":"ref","*":"link-rule"}}
]
}
,
{
"name":"target-rule",
"type":"and",
"rules":[
{"type":"array"},
{"type":"content","rule":{"type":"ref","*":"link-rule"}}
]
}
,
{
"name":"res-rule",
"type":"and",
"rules":[
{"type":"array"},
{"type":"properties",
"pairs":[
{"key":"key","optional":false,"rule":{"type":"string"}},
{"key":"value","optional":false,"rule":{"type":"string"}}
]
}
]
}
,
{
"name":"prop-rule",
"type":"and",
"rules":[
{"type":"array"},
{"type":"content","rule":{
"type":"or",
"rules":[
{"type":"ref","*":"link-rule"},
{"type":"ref","*":"msg-rule"},
{"type":"ref","*":"image-rule"}
]
}
,
{
"name":"body-rule",
"type":"properties",
"pairs":[
{"key":"actors","optional":false,"rule":{"type":"ref","*":"actor-rule"}},
{"key":"targets","optional":false,"rule":{"type":"ref","*":"target-rule"}},
{"key":"resources","optional":false,"rule":{"type":"ref","*":"res-rule"}},
{"key":"props","optional":false,"rule":{"type":"ref","*":"prop-rule"}}
]
}
]
}
以下是驗證的java代碼:
public JSONObject getJsonSchema() {
try {
InputStream is = getClass().getResourceAsStream("/schema.json");
JSONParser parser = new JSONParser(is);
JSONValue value = parser.nextValue();
JSONObject obj = (JSONObject)value;
return obj;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static boolean validateJson(String json) {
try {
Reader reader = new StringReader(json);
JSONParser parser = new JSONParser(reader);
JSONValue value = parser.nextValue();
JSONObject obj = getJsonSchema();
JSONValidator validator = new JSONValidator(obj);
validator.validate(value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}