Normalizer class¶
- class yggdrasil_rapidjson.Normalizer(json_schema)¶
- Parameters:
json_schema – the JSON schema, specified as a
strinstance or an UTF-8bytes/bytearrayinstance- Raises:
JSONDecodeError – if json_schema is not a valid
JSONvalue
- __call__(json)¶
- Parameters:
json – the
JSONvalue, specified as astrinstance or an UTF-8bytes/bytearrayinstance, that will be normalized- Raises:
JSONDecodeError – if json is not a valid
JSONvalue
The given json value will be normalized accordingly to the schema and returned: a
NormalizationErrorwill be raised if the normalization fails, and the exception will contain three arguments, respectively the type of the error, the position in the schema and the position in theJSONdocument where the error occurred:>>> normalize = Normalizer('{"required": ["a", "b"], "properties": {"b": {"default": 1.0}}}') >>> normalize('{"a": null}') {'a': None, 'b': 1.0} >>> try: ... normalize('{"c": false}') ... except NormalizationError as error: ... print(error.args) ... ('{\n "message": "Object is missing the following members required by the schema: \'[\\"a\\"]\'.",\n "instanceRef": "#",\n "schemaRef": "#"\n}',)
>>> import numpy as np >>> normalize = Normalizer({"type": "array", ... "items": {"type": "scalar", ... "subtype": "int", ... "precision": 8}, ... "minItems": 1}) >>> normalize([1.0, np.uint32(5)]) [1, 5] >>> try: ... normalize([3.2]) ... except NormalizationError as error: ... print(error.args) ... ('{\n "message": "Property has a subtype \'float\' that is not in the following list \'[\\"int\\"]\'.",\n "instanceRef": "#/0",\n "schemaRef": "#/items"\n}',)