defnull/bottle — Issue #287: request for pluggable json encode/decode functions to process response
Author: vaswadhati
Created: 2012-02-16 05:41:00+00:00
State: open

Hi there,

Luv bottle so far. Ran into an expected issue when dealing with json encoding that I have fixed for myself temporarily in my local copy.

Couple of issues so far:
1. It is perfectly alright to json encode a list (or a tuple) for that matter - in the pragmatic world of building Ajaxy web apps. The latest dev version (that I am using) checks if the response is a dict and if so applies json encoding on it. I patched mine to apply json encoding if it is a dict or a list. Not 100% sure about tuples so I left it at that.

2. The response consists of data retrieved from a database and as expected json encoding tripped when it saw datetime and date types. There is a known way to override json package's dumps and loads methods. I took patch (that I had to write when using Py-rs) and just inserted again into my copy of bottle.

Overall, worked around the issue and I am in good shape to continue to use bottle. What nags me is that I had plans of extending my json patch to support direct encoding of database results from the database layer API such as straight dbapi, sqlalchemy - something along the lines of what I found at http://www.mail-archive.com/turbogears-commits@googlegroups.com/msg03926.html - which means I will have to keep patching bottle as new versions get released.

Ideal would be if I could pass json encode and decode functions when Bottle() instantiated - something along the lines of:

myapi = Bottle(json_encode = myencoder, json_decode=mydecoder);

Or some thing along those lines or better. You get the idea.

Making an automatic decision to encode to json based on response type is a pretty significant and important design decision. And I think should come with an option to allow the user to configure what gets encoded and how.

Thanks,