From 7000cfa685eb94f24a56d2d2be317ae6de80283e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?b=CA=B0edoh=E2=82=82=20sw=C3=A9?= Date: Fri, 5 Apr 2024 23:15:45 +0500 Subject: [PATCH] Add missing files. --- maps/cross.json | 1 + maps/empty.json | 1 + maps/squareroom.json | 1 + maps/triangle.json | 1 + serialize.py | 18 ++++++++++++++++++ 5 files changed, 22 insertions(+) create mode 100644 maps/cross.json create mode 100644 maps/empty.json create mode 100644 maps/squareroom.json create mode 100644 maps/triangle.json create mode 100644 serialize.py 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.")