From 2bbdd6cf3a0ea429583a9e4b840e68c238e08a46 Mon Sep 17 00:00:00 2001 From: RaNaN Date: Sat, 22 Sep 2012 16:21:17 +0200 Subject: web socket api handler --- module/remote/json_converter.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 module/remote/json_converter.py (limited to 'module/remote/json_converter.py') 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 -- cgit v1.2.3