def test_Message():
    import serial_json

    class Item(serial_json.Message):  # Base classes of Message are automatically registered
        name: str
        value: int

    item = Item('abc', 123)
    item2 = serial_json.loads(serial_json.dumps(item))
    assert item == item2
    assert item.name == item2.name
    assert item.value == item2.value

    # Test additional messages with only using kwargs to set attrs
    class NewItem(serial_json.Message):  # Base classes of Message are automatically registered
        name2: str
        value2: int

    new_item = NewItem('abc', 123)
    new_item2 = serial_json.loads(serial_json.dumps(new_item))
    assert new_item != item
    assert new_item2 != item2
    assert new_item == new_item2
    assert new_item.name2 == new_item2.name2
    assert new_item.value2 == new_item2.value2
def test_date():
    import datetime
    import serial_json as json

    d = datetime.date(year=2020, month=1, day=3)
    # assert serial_json.dumps(d) == '{"value": "2020-01-03", "SERIALIZER_TYPE": "datetime.date"}'
    assert json.loads(json.dumps(d)) == d
Example #3
0
def test_dataclass_serial_json():
    from serial_json import dumps, loads, dataclass, field_property

    @dataclass
    class MyClass:
        x: int
        y: int

        @field_property(default=1)
        def z(self):
            return self._z

        @z.setter
        def z(self, value):
            self._z = value

    m = MyClass(0, 0)
    text = dumps(m)
    assert text == '{"x": 0, "y": 0, "z": 1, "SERIALIZER_TYPE": "test_dataclass_serial_json.<locals>.MyClass"}', text

    loaded = loads(text)
    assert loaded == m
    assert loaded.x == m.x
    assert loaded.y == m.y
    assert loaded.z == m.z
Example #4
0
def run_readme_custom_class():
    import serial_json

    @serial_json.register  # Register this class using __getstate__ and __setstate__ by default
    class MyClass(object):
        def __init__(self, x=0, y=0):
            self.x = x
            self.y = y

        def __eq__(self, other):
            """Compare objects."""
            try:
                return self.x == other.x and self.y == other.y
            except (AttributeError, Exception):
                return False

        def __getstate__(self):
            return {'x': self.x, 'y': self.y}

        def __setstate__(self, state):
            self.x = state.get('x', 0)
            self.y = state.get('y', 0)

    my_value = MyClass()
    assert serial_json.loads(serial_json.dumps(my_value)) == my_value
def test_time():
    import datetime
    import serial_json
    import serial_json.datetime_support  # Not needed in normal use

    d = datetime.time(hour=1, minute=40, second=50)
    assert serial_json.loads(serial_json.dumps(d)) == d
Example #6
0
def run_readme_custom_funcs():
    import serial_json

    class MyClass(object):
        def __init__(self, x=0, y=0):
            self.x = x
            self.y = y

        def __eq__(self, other):
            """Compare objects."""
            try:
                return self.x == other.x and self.y == other.y
            except (AttributeError, Exception):
                return False

    def cls_to_dict(obj):
        return {'x': obj.x, 'y': obj.y}

    def cls_from_dict(obj):
        return MyClass(**obj)

    # Register the serialize and deserialize functions
    serial_json.register(MyClass, cls_to_dict, cls_from_dict)

    my_value = MyClass()
    assert serial_json.loads(serial_json.dumps(my_value)) == my_value
def test_date():
    import datetime
    import serial_json
    import serial_json.datetime_support  # Not needed in normal use

    d = datetime.date(year=2020, month=1, day=3)
    # assert serial_json.dumps(d) == '{"value": "2020-01-03", "SERIALIZER_TYPE": "datetime.date"}'
    assert serial_json.loads(serial_json.dumps(d)) == d
def test_ndarray():
    import numpy as np
    import serial_json
    import serial_json.bytes_support  # Not needed in normal use
    import serial_json.numpy_support  # Not needed in normal use

    n = np.array([(1, 2, 3, 4), (5, 6, 7, 8)], dtype=np.dtype('<i4'))
    obj = serial_json.loads(serial_json.dumps(n))
    assert type(obj) == type(n)
    assert np.all(obj == n)
    assert obj.dtype == n.dtype
    assert obj.shape == n.shape

    n = np.array([(1.2, 2.3, 3.4, 4.5), (5.6, 6.7, 7.8, 8.9)],
                 dtype=np.dtype('<f4'))
    obj = serial_json.loads(serial_json.dumps(n))
    assert type(obj) == type(n)
    assert np.all(obj == n)
    assert obj.dtype == n.dtype
    assert obj.shape == n.shape
def test_datetime():
    import datetime
    import serial_json
    import serial_json.datetime_support  # Not needed in normal use

    d = datetime.datetime(year=2020,
                          month=1,
                          day=3,
                          hour=1,
                          minute=40,
                          second=50)
    assert serial_json.loads(serial_json.dumps(d)) == d
def test_np_structured_array():
    import numpy as np
    import serial_json
    import serial_json.bytes_support  # Not needed in normal use
    import serial_json.numpy_support  # Not needed in normal use

    n = np.array([(1, 2.3, True, 'abc'), (5, 6.7, False, 'def')],
                 dtype=np.dtype([('a', '<i4'), ('b', '<f4'), ('c', '<u1'),
                                 ('d', '|O')]))
    obj = serial_json.loads(serial_json.dumps(n))
    assert type(obj) == type(n)
    assert np.all(obj == n)
    assert obj.dtype == n.dtype
    assert obj.shape == n.shape
Example #11
0
def parse(value):
    try:
        return serial_json.loads(value)
    except:
        return value
Example #12
0
def test_dataclass_nested():
    from serial_json import dumps, loads, dataclass, field_property

    @dataclass
    class Point:
        x: int
        y: int

        @field_property(default=1)
        def z(self):
            return self._z

        @z.setter
        def z(self, value):
            self._z = value

    @dataclass
    class Location:
        name: str
        point: Point = Point(0, 0, 0)

        @field_property(default=Point(1, 1, 0))
        def point2(self):
            return self._point2

        @point2.setter
        def point2(self, value):
            if isinstance(value, (list, tuple)) and len(value) >= 2:
                value = Point(*value)
            elif isinstance(value, dict):
                value = Point(**value)

            if not isinstance(value, Point):
                raise TypeError('Given value must be a Point!')
            self._point2 = value

    m2 = Location('hello')
    assert m2.name == 'hello'
    assert m2.point.x == 0
    assert m2.point.y == 0
    assert m2.point.z == 0

    assert m2.point2 == Point(1, 1, 0)
    assert m2.point2.x == 1
    assert m2.point2.y == 1
    assert m2.point2.z == 0

    m2.point.x = 1
    m2.point.y = 1
    m2.point.z = 1
    assert m2.point.x == 1
    assert m2.point.y == 1
    assert m2.point.z == 1

    # Check that class default did not change
    assert m2.point != Location.point
    assert m2.point.x != Location.point.x
    assert m2.point.y != Location.point.y
    assert m2.point.z != Location.point.z

    text = dumps(m2)
    assert text == '{"name": "hello", "point": {"x": 1, "y": 1, "z": 1, ' \
                   '"SERIALIZER_TYPE": "test_dataclass_nested.<locals>.Point"}, "point2": {"x": 1, "y": 1, "z": 0, ' \
                   '"SERIALIZER_TYPE": "test_dataclass_nested.<locals>.Point"}, ' \
                   '"SERIALIZER_TYPE": "test_dataclass_nested.<locals>.Location"}', text

    loaded = loads(text)
    assert loaded == m2
    assert loaded.name == m2.name
    assert loaded.point == m2.point
    assert loaded.point.x == m2.point.x
    assert loaded.point.y == m2.point.y
    assert loaded.point.z == m2.point.z
Example #13
0
def test_bytes():
    import serial_json as json

    bts = b'12345'
    assert json.loads(json.dumps(bts)) == bts
def test_bytes():
    import serial_json
    import serial_json.bytes_support  # Not needed in normal use

    bts = b'12345'
    assert serial_json.loads(serial_json.dumps(bts)) == bts
Example #15
0
def test_time():
    import datetime
    import serial_json as json

    d = datetime.time(hour=1, minute=40, second=50)
    assert json.loads(json.dumps(d)) == d
 def run_serial_json():
     serial_json.loads(n_serial)
Example #17
0
def test_datetime():
    import datetime
    import serial_json as json

    d = datetime.datetime(year=2020, month=1, day=3, hour=1, minute=40, second=50)
    assert json.loads(json.dumps(d)) == d