Bottle framework: how to return datetime in JSON response


When I try to return JSON containing datetime value, I’m getting

  File "/usr/lib/python2.7/json/", line 178, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: datetime.datetime(2014, 2, 1, 0, 0) is not JSON serializable

Which is normal. Is there an easy way to add an object hook to bottle like

from bson import json_util
import json
json.dumps(anObject, default=json_util.default)

to get datetime values converted?

Asked By: Maksym Polshcha



Interesting question! I can see a couple of ways of doing this. The one would be to write a custom plugin that wraps the JSONPlugin:

from bottle import route, run, install, JSONPlugin
from bson import json_util

class JSONDefaultPlugin(JSONPlugin):
    def __init__(self):
        super(JSONDefaultPlugin, self).__init__()
        self.plain_dump = self.json_dumps
        self.json_dumps = lambda body: self.plain_dump(body, default=json_util.default)

Which can then be used like this:

def index(name):
    return {'test': datetime.datetime(2014, 2, 1, 0, 0)}

run(host='localhost', port=8080)

And will give output like this:

{"test": {"$date": 1391212800000}}

Another, shorter, way is to simply specify the json_loads parameter when instantiating the JSONPlugin class:

import json
from bson import json_util

install(JSONPlugin(json_dumps=lambda body: json.dumps(body, default=json_util.default)))

This produces the same result.


This all makes a little more sense when you look at the source code for bottle (some parts removed below for brevity):

class JSONPlugin(object):
    name = 'json'
    api  = 2

    def __init__(self, json_dumps=json_dumps):
        self.json_dumps = json_dumps

    def apply(self, callback, route):
        dumps = self.json_dumps
        if not dumps: return callback
        def wrapper(*a, **ka):

            if isinstance(rv, dict):
            elif isinstance(rv, HTTPResponse) and isinstance(rv.body, dict):
                rv.body = dumps(rv.body)
                rv.content_type = 'application/json'
            return rv

        return wrapper

All we need to do is make sure the call to dumps there receives the default keyword argument you wish to provide.

Answered By: Herman Schaaf

A few short additions:

  1. If you prefer using the standard library only, you can do:
import json
from datetime import datetime
from functools import partial

from bottle import run

from bottle import JSONPlugin, Bottle

json_plugin = JSONPlugin(json_dumps=partial(json.dumps, default=str))
app = Bottle()
  1. If you need more sophisticated representation of datetime:
def json_serial(obj):
    """JSON serializer for objects not serializable by default json code"""

    if isinstance(obj, (datetime, date)):
        # replace this with what ever format you need
        return obj.isoformat() 
    raise TypeError ("Type %s not serializable" % type(obj))

json_plugin = JSONPlugin(json_dumps=partial(json.dumps, default=json_serial))
  1. Finally, if you prefer brevity:
    Because the JSON plugin is inserted to the default Bottle instance, you can do:
app = Bottle()
app.plugins[0].json_dumps = partial(json.dumps, default=str)
Answered By: oz123
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.