예제 #1
0
def test_encode_decode():
    r"""Test encode/decode for valid objects."""
    for x in _valid_objects.values():
        y = datatypes.encode(x)
        z = datatypes.decode(y)
        assert_equal(z, x)
        datatypes.encode_data(x)
예제 #2
0
 def default(self, o):
     r"""Encoder that allows for expansion types."""
     from yggdrasil.metaschema import MetaschemaTypeError
     from yggdrasil.metaschema.datatypes import encode_data
     try:
         return encode_data(o)
     except MetaschemaTypeError:
         raise TypeError("Cannot encode %s" % o)
예제 #3
0
def test_encode_decode():
    r"""Test encode/decode for valid objects."""
    for x in _valid_objects.values():
        y = datatypes.encode(x)
        z = datatypes.decode(y)
        assert_equal(z, x)
        t = datatypes.encode_type(x)
        d = datatypes.encode_data(x)
        w = datatypes.decode_data(d, t)
        assert_equal(w, x)
    assert_raises(ValueError, datatypes.decode_data, b'', None)
예제 #4
0
 def default(self, o):
     r"""Encoder that allows for expansion types."""
     from yggdrasil.metaschema.datatypes import (encode_data,
                                                 MetaschemaTypeError)
     try:
         return encode_data(o)
     except MetaschemaTypeError:
         if _use_rapidjson:
             raise TypeError("Cannot encode %s" % o)
         else:
             return _json_encoder.default(self, o)
예제 #5
0
def test_encode_decode(nested_approx):
    r"""Test encode/decode for valid objects."""
    for x in _valid_objects.values():
        y = datatypes.encode(x)
        z = datatypes.decode(y)
        assert (z == nested_approx(x))
        t = datatypes.encode_type(x)
        d = datatypes.encode_data(x)
        w = datatypes.decode_data(d, t)
        assert (w == nested_approx(x))
    with pytest.raises(ValueError):
        datatypes.decode_data(b'', None)
예제 #6
0
    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 encode_data(obj, cls.get_temptype(typedef))
    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.

        """
        container = cls._container_type()
        for k, v in cls._iterate(obj):
            vtypedef = None
            if cls._json_property in typedef:
                vtypedef = cls._get_element(typedef[cls._json_property], k, None)
            vbytes = encode_data(v, typedef=vtypedef)
            cls._assign(container, k, vbytes)
        return container