python - datetime.date(2014, 4, 25) is not JSON serializable in Django -
this question has answer here:
i followed how overcome "datetime.datetime not json serializable" in python? not helping
i tried code
>>> import datetime >>> =datetime.date(2014, 4, 25) >>> bson import json_util >>> b = json.dumps(a,default = json_util.default) traceback (most recent call last): file "<console>", line 1, in <module> file "/usr/lib/python2.7/json/__init__.py", line 250, in dumps sort_keys=sort_keys, **kw).encode(obj) file "/usr/lib/python2.7/json/encoder.py", line 207, in encode chunks = self.iterencode(o, _one_shot=true) file "/usr/lib/python2.7/json/encoder.py", line 270, in iterencode return _iterencode(o, 0) file "/home/.../python2.7/site-packages/bson/json_util.py", line 256, in default raise typeerror("%r not json serializable" % obj) typeerror: datetime.date(2014, 4, 25) not json serializable
can me datetime.date
serializer , deserializer.
you can this:
def date_handler(obj): return obj.isoformat() if hasattr(obj, 'isoformat') else obj print json.dumps(data, default=date_handler)
from here.
update per j.f.sebastian comment
def date_handler(obj): if hasattr(obj, 'isoformat'): return obj.isoformat() else: raise typeerror print json.dumps(data, default=date_handler)
Comments
Post a Comment