python json模块安装_Python vjson包_程序模块 - PyPI - Python中文网

如何使用

使用jsonschema定义对象。:>>> import vjson

>>> _json = vjson.ValidatedJSON({

... 'type': 'object',

... 'properties': {

... 'price': {'type': 'number'},

... 'name': {'type': 'string'},

... },

... })

此对象可以转储/loads()/dump()/load()可序列化对象,如在JSON模块中一样。:>>> _json.loads('{"name" : "Eggs", "price" : 34.99}')

{'name': 'Eggs', 'price': 34.99}

>>> _json.dumps({"name": "Eggs", "price": 34.99})

'{"name": "Eggs", "price": 34.99}'

如果试图传递违反构造函数中指定的jsonschema的数据,则会引发一个vjson.jsonvalidationerror.:>>> _json.loads('{"name" : "Eggs", "price" : "34.99"}')

Traceback (most recent call last):

File "/tmp/test/var/src/develop/vjson/src/vjson/__init__.py", line 32, in _wrap

jsonschema.validate(data, schema)

File "/tmp/test/env/lib/python3.4/site-packages/jsonschema/validators.py", line 478, in validate

cls(schema, *args, **kwargs).validate(instance)

File "/tmp/test/env/lib/python3.4/site-packages/jsonschema/validators.py", line 123, in validate

raise error

jsonschema.exceptions.ValidationError: '34.99' is not of type 'number'

Failed validating 'type' in schema['properties']['price']:

{'type': 'number'}

On instance['price']:

'34.99'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "", line 1, in

File "/tmp/test/var/src/develop/vjson/src/vjson/__init__.py", line 34, in _wrap

raise JSONValidationError(err)

vjson.JSONValidationError: '34.99' is not of type 'number'

Failed validating 'type' in schema['properties']['price']:

{'type': 'number'}

On instance['price']:

'34.99'

>>>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值