JSONTools Validator的使用

相云
2023-12-01

此开发包的主页:http://jsontools.berlios.de/

  1. 此包的主要用途:
  2. Parser: Parse JSON text files and convert these to a Java model.
  3. Renderer: Render a Java representation into text.
  4. 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, ...) .
  5. 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.
  6. Validator: Validate the contents of a JSON file using a JSON schema.

以下来介绍一下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;
        }
    }

 类似资料: