diff -r 3cd644bfab12 -r ebb11fa58ab9 utils.py --- a/utils.py Tue Sep 22 19:33:47 2009 +0200 +++ b/utils.py Wed Sep 23 08:17:52 2009 +0200 @@ -20,8 +20,6 @@ from calendar import monthrange import decimal -import simplejson - # initialize random seed from current time seed() try: @@ -364,24 +362,25 @@ return True try: - # may not be there is cubicweb-web not there - from simplejson import JSONEncoder, dumps + # may not be there if cubicweb-web not installed + from simplejson import dumps, JSONEncoder except ImportError: pass else: + class CubicWebJsonEncoder(JSONEncoder): """define a simplejson encoder to be able to encode yams std types""" def default(self, obj): - if isinstance(obj, datetime): + if isinstance(obj, pydatetime.datetime): return obj.strftime('%Y/%m/%d %H:%M:%S') - elif isinstance(obj, date): + elif isinstance(obj, pydatetime.date): return obj.strftime('%Y/%m/%d') elif isinstance(obj, pydatetime.time): return obj.strftime('%H:%M:%S') elif isinstance(obj, decimal.Decimal): return float(obj) try: - return simplejson.JSONEncoder.default(self, obj) + return JSONEncoder.default(self, obj) except TypeError: # we never ever want to fail because of an unknown type, # just return None in those cases.