Java_JsonSchema_Validate

package groovystudy

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.JsonNode
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
import com.github.fge.jsonschema.core.load.uri.URITranslatorConfiguration
import com.github.fge.jsonschema.core.load.configuration.LoadingConfiguration
/**
 *
 * @author asun
 */
class JsonSchemaCheck {
    static String SchemaFolder

    static JsonNode getJsonByString(String jsonStr) {
        def mapper = new ObjectMapper()
        JsonNode json = mapper.readValue(jsonStr, JsonNode.class)
        return json
    }

    static JsonNode getJsonByFileName(String schemaFileName) {
        def mapper = new ObjectMapper()
        JsonNode json = mapper.readValue(new File(SchemaFolder, schemaFileName), JsonNode.class)
        return json
    }

    public static void SetSchemaFolder(String schemaFolder) {
        this.SchemaFolder = schemaFolder.replaceAll("\\", "/")
    }

    static void JsonNode(String jsonStr, String schemaFileName) {
        final def translatorCfg = URITranslatorConfiguration.newBuilder()
            .setNamespace(new URI(SchemaFolder)).freeze()

        final def cfg = LoadingConfiguration.newBuilder()
            .setURITranslatorConfiguration(translatorCfg).freeze()

        final def factory = JsonSchemaFactory.newBuilder()
            .setLoadingConfiguration(cfg).freeze()

        def json = getJsonByString(jsonStr)

        def schema = factory.getJsonSchema(schemaFileName)

        ProcessingReport report
        report = schema.validate(json)

        if(!report.success) {
            throw new Exception("Schema $schemaFileName Validation failed :" +report)
        }
    }

    public static void main(args) {
        def filePath = "file:/C:/Users/asun/Desktop/JsonSchemaValidation_Example/"
        SchemaFolder = filePath
        def schemaFileName = "jsonSchemaFile_Example1.json"
        def jsonStr = '''
            {
            "_meta":{
                    "response_status": 200100,
                    "hint": "Books successfully returned"
            },
            "books": [{
                    "author": "Jack",
                    "id": "1000",
                    "name": "groovy",
                    "price": "30"
            },
            {
                    "author": "Jimmy",
                    "id": "1001",
                    "name": "java",
                    "price": "32"
            },
            {
                    "author": "Jimmy",
                    "id": "1002",
                    "name": "c#",
                    "price": "32"
            },
            {
                    "author": "Jimmy",
                    "id": "1003",
                    "name": "html",
                    "price": "32"
            },
            {
                    "author": "Jimmy",
                    "id": "1004",
                    "name": "english",
                    "price": "32"
            },
            {
                    "author": "Jimmy",
                    "id": "1005",
                    "name": "sport",
                    "price": "32"
            },
            {
                    "author": "Jimmy",
                    "id": "1006",
                    "name": null,
                    "price": "32"
            }
            ]
        }
        '''

        JsonSchemaCheck.JsonNode(jsonStr, schemaFileName)
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值