summaryrefslogtreecommitdiffstats
path: root/module/remote/json_converter.py
diff options
context:
space:
mode:
authorGravatar RaNaN <Mast3rRaNaN@hotmail.de> 2012-09-22 16:21:17 +0200
committerGravatar RaNaN <Mast3rRaNaN@hotmail.de> 2012-09-22 16:21:17 +0200
commit2bbdd6cf3a0ea429583a9e4b840e68c238e08a46 (patch)
tree3adf00866aa7fe5ac59bd5430754ad9dfcbc72b8 /module/remote/json_converter.py
parentadded websocket backend, removed thrift (diff)
downloadpyload-2bbdd6cf3a0ea429583a9e4b840e68c238e08a46.tar.xz
web socket api handler
Diffstat (limited to 'module/remote/json_converter.py')
-rw-r--r--module/remote/json_converter.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/module/remote/json_converter.py b/module/remote/json_converter.py
new file mode 100644
index 000000000..57e85fd16
--- /dev/null
+++ b/module/remote/json_converter.py
@@ -0,0 +1,39 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from module.common.json_layer import json
+from ttypes import BaseObject
+
+# json encoder that accepts TBase objects
+class BaseEncoder(json.JSONEncoder):
+
+ def default(self, o):
+ if isinstance(o, BaseObject):
+ ret = {"@class" : o.__class__.__name__}
+ for att in o.__slots__:
+ ret[att] = getattr(o, att)
+ return ret
+
+ return json.JSONEncoder.default(self, o)
+
+class BaseDecoder(json.JSONDecoder):
+
+ def __init__(self, *args, **kwargs):
+ json.JSONDecoder.__init__(self, *args, **kwargs)
+ self.object_hook = self.convertObject
+
+ def convertObject(self, dct):
+ if '@class' in dct:
+ # TODO: convert
+ pass
+
+ return dct
+
+def dumps(*args, **kwargs):
+ kwargs['cls'] = BaseEncoder
+ return json.dumps(*args, **kwargs)
+
+
+def loads(*args, **kwargs):
+ kwargs['cls'] = BaseDecoder
+ return json.loads(*args, **kwargs) \ No newline at end of file