Scala中的JSON模式验证器 [英] JSON schema validator in Scala

查看:192
本文介绍了Scala中的JSON模式验证器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要验证我收到的某些JSON输入的架构.我不清楚如何解决整个问题.但这是我到目前为止收集的:

I need to validate the schema of certain JSON input I receive. I am not clear about how to go about the whole thing. But this is what I have gathered so far :

  1. 我需要使用 http://json之类的东西为所有输入准备架构-schema.org/implementations.html

然后,我需要一个类似 https://github.com/fge/json的验证器-schema-validator

Then I need a validator like https://github.com/fge/json-schema-validator

我需要将json输入和架构提供给验证程序,并获得结果.

I need to give the json input to and the schema to the validator and get the result.

但是我的问题是我需要使用一个可以导入和使用json-schema-validator https://github.com/fge/json-schema-validator .我也不清楚如何使用它.我不知道它接受的格式,所需的类和方法等.

However my question is that I need to use a jar which I can import and use of the json-schema-validator https://github.com/fge/json-schema-validator . Also I am not clear on how to use it. I don't know the format it accepts , the classes and methods needed etc.

  1. 验证者对Scala的支持有多好?

推荐答案

我不会费心手动收集JSON模式验证器的罐子(这样做,不是很有趣).最好为此使用工具(例如maven,sbt,gradle或ivy). 如果要在OSGi环境中使用它,则可能需要使用不同(可能不是最新的)版本.

I would not go through the pain of manually collecting the jars of json schema validator (done that, not fun). It is better use a tool for that (like maven, sbt, gradle or ivy). In case you want to use it in an OSGi environment, you might need to use a different (probably not up-to-date) version.

用法:

val factory: JsonSchemaFactory = JsonSchemaFactory.getDefault
val validator: JsonValidator = factory.getValidator
val schemaJson: com.fasterxml.jackson.databind.JsonNode = yourJsonSchemaInJackson2Format
val report: ProcessingReport = validator.validate(schemaJson, yourJsonInJackson2Format)
//check your report.

PS:如果要手动收集依赖项,可以从

PS.: In case you want to collect the dependencies manually, you can go through the dependencies transitively starting on this page.

这篇关于Scala中的JSON模式验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆