python中json模式验证的实现
jsonschema-extended的Python项目详细描述
jsonschema是JSON Schema的实现 对于python(支持2.7+包括python 3)。
>>>fromjsonschemaimportvalidate>>># A sample schema, like what we'd get from json.load()>>>schema={..."type":"object",..."properties":{..."price":{"type":"number"},..."name":{"type":"string"},...},...}>>># If no exception is raised by validate(), the instance is valid.>>>validate({"name":"Eggs","price":34.99},schema)>>>validate(...{"name":"Eggs","price":"Invalid"},schema...)# doctest: +IGNORE_EXCEPTION_DETAILTraceback(mostrecentcalllast):...ValidationError:'Invalid'isnotoftype'number'
也可以从控制台使用:
$ jsonschema -i sample.json sample.schema
功能
- 全力支持 Draft 6, Draft 4 和 Draft 3
- Lazy validation 它可以反复报告all验证错误。
- 小巧且可扩展
- Programmatic querying 验证失败的属性或项。
发行说明
版本2.6.0放弃了对Python2.6.x(ha)的支持,并包含 错误消息中的一些小改进,以及 ErrorTree。
运行测试套件
如果您安装了tox(可能通过pip install tox或 包管理器),在源签出的目录中运行tox将 在所有python版本上运行jsonschema的测试套件jsonschema 支持。注意,您需要在 命令对每个测试运行测试,否则tox将跳过(并失败) 对那个版本的测试。
当然,您也可以使用 最喜欢的测试运行程序。测试存在于jsonschema.tests包中。
基准
jsonschema的基准使用perf。
运行它们可以通过tox -e perf或通过调用perf 外部命令(在确保它和jsonschema本身都是 已安装):
$ python -m perf jsonschema/benchmarks/test_suite.py --hist --output results.json
要与上一次运行进行比较,请使用:
$ python -m perf compare_to --table reference.json results.json
有关详细信息,请参阅perf文档。
贡献
我是朱利安·伯曼。
jsonschema在GitHub上。
通过Github或其他方式联系,如果你有什么要贡献的, 非常欢迎!
你也可以在freenode(nick:tos9)上找到我 频道,包括#python。
如果你非常感激,你可以通过 谷歌支付与我的github配置文件的电子邮件。