[go: up one dir, main page]

Menu

[d66369]: / src / memento.py  Maximize  Restore  History

Download this file

46 lines (35 with data), 1.2 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# -*- coding: utf-8 -*-
'''
Created on 27.07.2012
@author: vlkv
'''
import sys
import json
import datetime
class Serializable(object):
def toJson(self):
raise NotImplementedError()
@staticmethod
def fromJson(objState):
raise NotImplementedError()
class Encoder(json.JSONEncoder):
def __init__(self, indent=4, sort_keys=True):
json.JSONEncoder.__init__(self, indent=indent, sort_keys=sort_keys)
def default(self, obj):
if isinstance(obj, Serializable):
return obj.toJson()
if isinstance(obj, datetime.datetime):
return {"__datetime__": obj.__repr__()}
else:
return json.JSONEncoder.default(self, obj)
def Decoder():
return json.JSONDecoder(object_hook=hook)
def hook(objState):
if "__class__" in objState and "__module__" in objState:
__import__(objState["__module__"])
cls = getattr(sys.modules[objState["__module__"]], objState["__class__"])
return cls.fromJson(objState)
elif "__datetime__" in objState:
return eval(objState["__datetime__"])
else:
return objState