diff --git a/maps/cross.json b/maps/cross.json new file mode 100644 index 0000000..c4156e0 --- /dev/null +++ b/maps/cross.json @@ -0,0 +1 @@ +[{"__type__": "objSegment", "isFinite": false, "pointA": {"__type__": "complex", "real": 1.0, "imag": 0.0}, "pointB": {"__type__": "complex", "real": 0.0, "imag": 0.0}, "color": [255, 0, 0]}, {"__type__": "objSegment", "isFinite": false, "pointA": {"__type__": "complex", "real": 0.0, "imag": 1.0}, "pointB": {"__type__": "complex", "real": 0.0, "imag": 0.0}, "color": [0, 255, 0]}] \ No newline at end of file diff --git a/maps/empty.json b/maps/empty.json new file mode 100644 index 0000000..50b31d4 --- /dev/null +++ b/maps/empty.json @@ -0,0 +1 @@ +[{"__type__": "objSegment", "isFinite": true, "pointA": {"__type__": "complex", "real": 0.0, "imag": 0.0}, "pointB": {"__type__": "complex", "real": 0.0, "imag": 0.0}, "color": [0, 0, 0]}] \ No newline at end of file diff --git a/maps/squareroom.json b/maps/squareroom.json new file mode 100644 index 0000000..5c94cc5 --- /dev/null +++ b/maps/squareroom.json @@ -0,0 +1 @@ +[{"__type__": "objSegment", "isFinite": true, "pointA": {"__type__": "complex", "real": 0.6, "imag": 0.0}, "pointB": {"__type__": "complex", "real": 0.0, "imag": 0.6}, "color": [255, 0, 0]}, {"__type__": "objSegment", "isFinite": true, "pointA": {"__type__": "complex", "real": 0.6, "imag": 0.0}, "pointB": {"__type__": "complex", "real": 0.0, "imag": -0.6}, "color": [0, 255, 0]}, {"__type__": "objSegment", "isFinite": true, "pointA": {"__type__": "complex", "real": -0.6, "imag": 0.0}, "pointB": {"__type__": "complex", "real": 0.0, "imag": -0.6}, "color": [0, 0, 255]}, {"__type__": "objSegment", "isFinite": true, "pointA": {"__type__": "complex", "real": -0.6, "imag": 0.0}, "pointB": {"__type__": "complex", "real": 0.0, "imag": 0.6}, "color": [255, 255, 0]}] \ No newline at end of file diff --git a/maps/triangle.json b/maps/triangle.json new file mode 100644 index 0000000..504a811 --- /dev/null +++ b/maps/triangle.json @@ -0,0 +1 @@ +[{"__type__": "objSegment", "isFinite": true, "pointA": {"__type__": "complex", "real": 0.38529100476842437, "imag": 0.0}, "pointB": {"__type__": "complex", "real": -0.2372040619364459, "imag": 0.36629343026935063}, "color": [255, 0, 0]}, {"__type__": "objSegment", "isFinite": true, "pointA": {"__type__": "complex", "real": -0.2372040619364459, "imag": 0.36629343026935063}, "pointB": {"__type__": "complex", "real": -0.22354939429564802, "imag": -0.26820653903460523}, "color": [0, 255, 0]}, {"__type__": "objSegment", "isFinite": true, "pointA": {"__type__": "complex", "real": -0.22354939429564802, "imag": -0.26820653903460523}, "pointB": {"__type__": "complex", "real": 0.39868528559672944, "imag": 0.017873216691274733}, "color": [0, 0, 255]}] \ No newline at end of file diff --git a/serialize.py b/serialize.py new file mode 100644 index 0000000..0deb3cb --- /dev/null +++ b/serialize.py @@ -0,0 +1,18 @@ +import levels + +def object_hook(dct): + if '__type__' not in dct: + return dct + if dct['__type__'] == 'complex': + return complex(dct['real'], dct['imag']) + if dct['__type__'] == 'objSegment': + return levels.Segment(dct['isFinite'], dct['pointA'], dct['pointB'], tuple(dct['color'])) + return dct + +def default(obj): + if isinstance(obj, complex): + return {"__type__": "complex", "real": obj.real, "imag": obj.imag} + elif isinstance(obj, levels.Segment): + return {"__type__": "objSegment", "isFinite": obj.isFinite, "pointA": obj.pointA, "pointB": obj.pointB, "color": obj.color} + else: + raise TypeError("This type is not JSON serializable.")