Source code for yggdrasil.metaschema.datatypes.JSONMetaschemaType

import numbers
from yggdrasil.metaschema.datatypes.MetaschemaType import MetaschemaType


[docs]class JSONMetaschemaTypeBase(MetaschemaType): r"""Base type for default JSON types.""" name = 'json' description = 'A json base type.' specificity = -1 # These types are evaluated last _replaces_existing = True
[docs] @classmethod def encode_data(cls, obj, typedef): r"""Encode an object's data. Args: obj (object): Object to encode. typedef (dict): Type definition that should be used to encode the object. Returns: string: Encoded object. """ return obj
[docs] @classmethod def decode_data(cls, obj, typedef): r"""Decode an object. Args: obj (string): Encoded object to decode. typedef (dict): Type definition that should be used to decode the object. Returns: object: Decoded object. """ return obj
[docs]class JSONBooleanMetaschemaType(JSONMetaschemaTypeBase): r"""JSON base boolean type.""" name = 'boolean' description = 'JSON boolean type.' python_types = (bool, ) example_data = True
[docs] @classmethod def normalize(cls, obj): r"""Normalize an object, if possible, to conform to this type. Args: obj (object): Object to normalize. Returns: object: Normalized object. """ if isinstance(obj, str): if obj.lower() == 'true': obj = True elif obj.lower() == 'false': obj = False return obj
[docs]class JSONIntegerMetaschemaType(JSONMetaschemaTypeBase): r"""JSON base integer type.""" name = 'integer' description = 'JSON integer type.' python_types = (int,) # TODO: Find a better way to signify this for creating the table cross_language_support = False example_data = int(1)
[docs] @classmethod def normalize(cls, obj): r"""Normalize an object, if possible, to conform to this type. Args: obj (object): Object to normalize. Returns: object: Normalized object. """ try: obj = int(obj) except BaseException: pass return obj
[docs]class JSONNullMetaschemaType(JSONMetaschemaTypeBase): r"""JSON base null type.""" name = 'null' description = 'JSON null type.' python_types = (type(None), ) example_data = None
[docs]class JSONNumberMetaschemaType(JSONMetaschemaTypeBase): r"""JSON base number type. Developer Notes: This covers the JSON default for floating point or integer values. """ name = 'number' description = 'JSON number type.' python_types = (numbers.Number, ) example_data = 1.0
[docs] @classmethod def normalize(cls, obj): r"""Normalize an object, if possible, to conform to this type. Args: obj (object): Object to normalize. Returns: object: Normalized object. """ try: obj = float(obj) except BaseException: pass return obj
[docs]class JSONStringMetaschemaType(JSONMetaschemaTypeBase): r"""JSON base string type. Developer Notes: Encoding dependent on JSON library. """ name = 'string' description = 'JSON string type.' python_types = (str,) example_data = 'hello'
[docs] @classmethod def normalize(cls, obj): r"""Normalize an object, if possible, to conform to this type. Args: obj (object): Object to normalize. Returns: object: Normalized object. """ if not isinstance(obj, (list, tuple, dict)): return str(obj) return obj