pydata/pandas — pull request #5138 by Komnomnomnom (2013-10-07 06:02:37+00:00)
Title: ENH: json default_handler param

This adds a `default_handler` param to `to_json`, which if passed must be a callable which takes one argument (the object to convert) and returns a serialisable object. The `default_handler` will only be used if an object cannot otherwise be serialised.

Note right now the JSON serialiser has direct handling for:

- Python basic types `bool`, `int`, `long`, `float`.
- Python containers `dict`, `list`, `tuple` and `set`.
- Python byte and unicode strings.
- Python decimal.
- Python `datetime` and `date`.
- Python `None`.
- Numpy arrays.
- Numpy scalars
- `NaN` and `NaT`
- Pandas `Index`, `Series`, `DataFrame` and `Timestamp`
- any subclasses of the above.

If an object is not recognised the fallback behaviour with this PR would be:

1. if a ``toDict`` method is defined by the unrecognised object that
will be called and its returned ``dict`` will be JSON serialised.
2. if a ``default_handler`` has been passed to ``to_json`` that will
be called to convert the object.
3. otherwise an attempt is made to convert the object to a ``dict`` by
iterating over its attributes.