總結一下最近用到的技術(2)--JsonSchema和JsonSchemaValidator

咱們最先接觸xml的時候會使用一個dtd文件去定義xml裏能夠有哪些元素和屬性等,後來發展到xml schama(是一個xsd文件,在dtd的基礎上提供了命名空間等更強大的功能)java

如今,RESTful接口和JSON大行其道,咱們能夠把JSON直接存儲到數據庫中,提供了比原先的關係表更容易擴展的能力(雖然JSON串的存儲仍然用到了關係表)。RESTful接口直接返回JSON數據,比返回xml更加簡潔和易讀。git

xml中能夠有xml schema對xml數據進行定義和校驗,一樣的,JSON中也有相應的叫作JSON schema的機制,來對JSON數據進行描述和定義,而且提供了相應的機制來檢驗某個JSON字符串是否符合JSON schema的定義。github

JSON schema的語法接下來我會單獨寫一篇文章去總結一下,下邊是一個JSON schema的簡單例子,是咱們的項目中用到的:數據庫

{
    "$schema":"http://json-schema.org/draft-04/schema",
    "type":"object",
    "properties": {
        "name": {
            "type":"string"
        },
        
        "versions": {
            "type":"array",
            "items": {
                "type":"object",
                "properties": {
                    "id": {
                        "type":"string"
                    },
                    
                    "version": {
                        "type":"integer"
                    },
                    
                    "comment": {
                        "type":"string"
                    }
                },
                
                "required":["id", "version"],
                "minItems":1
            }
        }
    },
    
    "required":["name", "versions"]
}

根對象是Object類型的,有兩個屬性,name和version 其中name是一個字符串,而version是一個數組,數組中的元素對象類型的,包括 字符串類型的id,整形的version和字符串類型的comment。apache

JSON scheam規定了JSON 字符串中應該有哪些元素和它們的類型是什麼,也規定了哪些元素必須存在,元素的範圍等等。json

要檢驗一個給定的JSON字符串是否符合一個給定的JSON schema,在java中咱們能夠使用 json-schame-validator https://github.com/fge/json-schema-validator數組

下邊是JSON schema 校驗的相關代碼:ui

import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JsonLoader;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;

public class JsonShemaValidator {

    private static Log log = LogFactory.getLog(JsonShemaValidator.class);

    private final static JsonSchemaFactory factory = JsonSchemaFactory.byDefault();

    /**
     * validate instance and Schema,here including two functions. as follows:
     * first: the Draft v4 will check the syntax both of schema and instance.
     * second: instance validation.
     * 
     * @param mainSchema
     * @param instance
     * @return
     * @throws IOException
     * @throws ProcessingException
     */
    public static ProcessingReport validatorSchema(String mainSchema, String instance) throws IOException, ProcessingException {
        JsonNode mainNode = JsonLoader.fromString(mainSchema);
        JsonNode instanceNode = JsonLoader.fromString(instance);
        JsonSchema schema = factory.getJsonSchema(mainNode);
        ProcessingReport processingReport = schema.validate(instanceNode);
        log.info(processingReport);

        return processingReport;
    }

}

從返回的ProcessingReport對象中能夠獲得校驗的結果:成功與否,以及出錯信息等等spa

相關文章
相關標籤/搜索