equal
deleted
inserted
replaced
477 |
477 |
478 class CubicWebJsonEncoder(json.JSONEncoder): |
478 class CubicWebJsonEncoder(json.JSONEncoder): |
479 """define a json encoder to be able to encode yams std types""" |
479 """define a json encoder to be able to encode yams std types""" |
480 |
480 |
481 def default(self, obj): |
481 def default(self, obj): |
482 if hasattr(obj, 'eid'): |
482 if hasattr(obj, '__json_encode__'): |
483 d = obj.cw_attr_cache.copy() |
483 return obj.__json_encode__() |
484 d['eid'] = obj.eid |
|
485 return d |
|
486 if isinstance(obj, datetime.datetime): |
484 if isinstance(obj, datetime.datetime): |
487 return ustrftime(obj, '%Y/%m/%d %H:%M:%S') |
485 return ustrftime(obj, '%Y/%m/%d %H:%M:%S') |
488 elif isinstance(obj, datetime.date): |
486 elif isinstance(obj, datetime.date): |
489 return ustrftime(obj, '%Y/%m/%d') |
487 return ustrftime(obj, '%Y/%m/%d') |
490 elif isinstance(obj, datetime.time): |
488 elif isinstance(obj, datetime.time): |
498 except TypeError: |
496 except TypeError: |
499 # we never ever want to fail because of an unknown type, |
497 # we never ever want to fail because of an unknown type, |
500 # just return None in those cases. |
498 # just return None in those cases. |
501 return None |
499 return None |
502 |
500 |
503 def json_dumps(value): |
501 def json_dumps(value, **kwargs): |
504 return json.dumps(value, cls=CubicWebJsonEncoder) |
502 return json.dumps(value, cls=CubicWebJsonEncoder, **kwargs) |
505 |
503 |
506 |
504 |
507 class JSString(str): |
505 class JSString(str): |
508 """use this string sub class in values given to :func:`js_dumps` to |
506 """use this string sub class in values given to :func:`js_dumps` to |
509 insert raw javascript chain in some JSON string |
507 insert raw javascript chain in some JSON string |