##// END OF EJS Templates
extended JSON encoder to use __json__ method if available
marcink -
r2152:e14ae843 beta
parent child Browse files
Show More
@@ -34,7 +34,12 b' from rhodecode import __platform__, PLAT'
34 #==============================================================================
34 #==============================================================================
35
35
36
36
37 def __obj_dump(obj):
37 def _obj_dump(obj):
38 """
39 Custom function for dumping objects to JSON
40
41 :param obj:
42 """
38 DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%S"
43 DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%S"
39 DATE_FORMAT = "%Y-%m-%d"
44 DATE_FORMAT = "%Y-%m-%d"
40 if isinstance(obj, complex):
45 if isinstance(obj, complex):
@@ -47,6 +52,11 b' def __obj_dump(obj):'
47 return list(obj)
52 return list(obj)
48 elif isinstance(obj, OrderedDict):
53 elif isinstance(obj, OrderedDict):
49 return obj.as_dict()
54 return obj.as_dict()
55 elif hasattr(obj, '__json__'):
56 if callable(obj.__json__):
57 return obj.__json__()
58 else:
59 return obj.__json__
50 else:
60 else:
51 raise NotImplementedError
61 raise NotImplementedError
52
62
@@ -57,7 +67,7 b' try:'
57 class ExtendedEncoder(json.JSONEncoder):
67 class ExtendedEncoder(json.JSONEncoder):
58 def default(self, obj):
68 def default(self, obj):
59 try:
69 try:
60 return __obj_dump(obj)
70 return _obj_dump(obj)
61 except NotImplementedError:
71 except NotImplementedError:
62 pass
72 pass
63 return json.JSONEncoder.default(self, obj)
73 return json.JSONEncoder.default(self, obj)
@@ -68,7 +78,7 b' except ImportError:'
68
78
69 def extended_encode(obj):
79 def extended_encode(obj):
70 try:
80 try:
71 return __obj_dump(obj)
81 return _obj_dump(obj)
72 except NotImplementedError:
82 except NotImplementedError:
73 pass
83 pass
74 raise TypeError("%r is not JSON serializable" % (obj,))
84 raise TypeError("%r is not JSON serializable" % (obj,))
@@ -381,6 +381,9 b' class User(Base, BaseModel):'
381
381
382 def __json__(self):
382 def __json__(self):
383 return dict(
383 return dict(
384 user_id=self.user_id,
385 first_name=self.name,
386 last_name=self.lastname,
384 email=self.email,
387 email=self.email,
385 full_name=self.full_name,
388 full_name=self.full_name,
386 full_name_or_username=self.full_name_or_username,
389 full_name_or_username=self.full_name_or_username,
General Comments 0
You need to be logged in to leave comments. Login now