use cuibicweb json encoder for json_dumps, still catch the TypeError in the request-less jsonize stable
authorAurelien Campeas <aurelien.campeas@logilab.fr>
Mon, 19 Oct 2009 20:26:22 +0200
branchstable
changeset 3741 3864c7dfddf0
parent 3736 07196bda2456
child 3742 20f429eb5f46
use cuibicweb json encoder for json_dumps, still catch the TypeError in the request-less jsonize
web/__init__.py
--- a/web/__init__.py	Mon Oct 19 18:36:20 2009 +0200
+++ b/web/__init__.py	Mon Oct 19 20:26:22 2009 +0200
@@ -10,15 +10,13 @@
 __docformat__ = "restructuredtext en"
 _ = unicode
 
-from decimal import Decimal
-from datetime import datetime, date, timedelta
 from simplejson import dumps
 
 from logilab.common.deprecation import deprecated
 
 from urllib import quote as urlquote
 from cubicweb.web._exceptions import *
-
+from cubicweb.utils import CubicWebJsonEncoder
 
 INTERNAL_FIELD_VALUE = '__cubicweb_internal_field__'
 
@@ -47,22 +45,16 @@
 FACETTES = set()
 
 
-
 def json_dumps(value):
-    if isinstance(value, Decimal):
-        value = float(value)
-    elif isinstance(value, (date, datetime)):
-        value = value.strftime('%Y-%m-%d %H:%M')
-    elif isinstance(value, timedelta):
-        value = (value.days * 24*60*60) + value.seconds
-    try:
-        return dumps(value)
-    except TypeError:
-        return dumps(repr(value))
+    return dumps(value, cls=CubicWebJsonEncoder)
 
 def jsonize(function):
     def newfunc(*args, **kwargs):
-        return json_dumps(function(*args, **kwargs))
+        try:
+            value = function(*args, **kwargs)
+            return json_dumps(value)
+        except TypeError:
+            return json_dumps(repr(value))
     return newfunc
 
 @deprecated('use req.build_ajax_replace_url() instead')