p3he/serialize.py

19 lines
699 B
Python

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.")