[Python] One utility class for JSON output
2014-01-17 16:44
597 查看
from sqlalchemy.orm.attributes import InstrumentedAttribute import datetime class JsonUtils: entity_superclass = 'sqlalchemy.ext.declarative.Base' def __init__(self, depth=1): self.visited_entities = set() self.depth = depth def reset(self): self.visited_entities = set() def tojson(self, o): return self._tojson(o, 1) def _tojson(self, o, level): if o == None: return o elif isinstance(o, (str, int, float)): return o elif isinstance(o, (datetime.datetime, datetime.date, datetime.time)): return str(o) elif isinstance(o, list): return self._tojson_list(o, level) elif isinstance(o, dict): return self._tojson_dict(o, level) elif self._get_superclass(o) == self.entity_superclass: if o not in self.visited_entities and level <= self.depth: self.visited_entities.add(o) level += 1 return self._tojson_entity(o, level) else: raise MaxDepthException() else: return str(o) def _tojson_list(self, l, level): result = [] for i in l: result.append(self._tojson(i, level)) return result def _tojson_dict(self, d, level): result = {} for k in d.keys(): result[k] = self._tojson(d[k], level) return result def _tojson_entity(self, e, level): result = {} cls = e.__class__ attribs = cls.__dict__ for k in attribs.keys(): v = attribs[k] if isinstance(v, InstrumentedAttribute) or k == '_type': try: attrvalue = e.__getattribute__(k) result[k] = self._tojson(attrvalue, level) except MaxDepthException: pass except: print "__getattribute__(%s) failed" % k return result def _get_superclass(self, o): return o.__class__.__base__.__module__ + '.' + o.__class__.__base__.__name__ def fromjson(self, o): pass class MaxDepthException(Exception): pass
相关文章推荐
- 关于python使用One Class&nbs…
- HttpMessageNotWritableException: Could not write JSON: No serializer found for class ****
- Caused by: Unable to locate parent package [json-package] for [class com.you.action.ColumnAction] -
- com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.apache.ibati
- 转json提示No serializer found for class
- Caused by: Unable to locate parent package [json-default] for [class com.you.user.action.StudentActi
- GBPR: Group Preference Based Bayesian Personalized Ranking for One-Class Collaborative Filtering-IJACA 2013_20160421
- python类别变量(class_label)转换为One_Hot的几种方式
- No converter found for return value of type: class org.json.JSONArray解决方法
- Python class and function json
- org.codehaus.jackson.map.JsonMappingException: No serializer found for class org.hibernate.proxy.pojo.javassist.
- Caused by: Unable to locate parent package [json-default] for [class com.you.user.action.StudentActi
- python实现class对象转换成json/字典的方法
- org.codehaus.jackson.map.JsonMappingException: No serializer found for class org.hibernate.proxy....
- SpringBoot中springMVC返回JSON报错-->Could not write JSON: No serializer found for class org.spring...
- Gradle 编译spring源码报错No such property: outputDir for class: org.gradle.api.internal.file.DefaultSourceDirectorySet
- com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class ...
- org.hibernate.PropertyNotFoundException: Could not find a getter for employee in class com.itcast.f_hbm_oneToMany.Department
- 【错误】More than one row with the given identifier was found: 1, for class: