I’m getting this error:
TypeError: datetime.datetime(2012, 2, 12, 0, 47, 6, 542000) is not JSON serializable
when jinja is trying to parse this line:
var root_node_info = eval({{ nd|tojson|safe }});
nd contains a bson object from my mongo database. One of the fields is a datetime object. How can I get flask to serialize this properly?
This is my mongokit model(in case its relevant)
class Item(Document):
structure = {
"tldr": unicode,
"body": unicode,
"user": unicode,
"time_submitted": datetime.datetime,
"upvotes": int,
"downvotes": int,
"tags": [unicode]
}
validators = {
}
indexes = [
{'fields':['user']},
{'fields':['tags']}
]
use_dot_notation = True
required_fields = ['body', 'user', 'time_submitted']
default_values = {'time_submitted': datetime.datetime.utcnow}
def __repr__(self):
return '<item %r>' % (self._id)
JSON does not handle
datetimeobjects. Standard practice is to encode them as strings in ISO format. This SO question about JSON provides examples. You will need to register the new JSON encoder filter yourself.