I have access to a pre-existing json.JSONEncoder subclass called 
GpxJsonEncoder for serialising a specific type (called a GpxModel - 
although that's not important). I can use this specialised JSONEncoder with 
dumps() by specifying the 'cls' argument to json.dumps() and everything 
works as expected:

    import son
    json_data = json.dumps(gpx_model, cls=GpxJsonEncoder)

I'd like to be able to use GpxJsonEncoder with Pyramid by configuring a 
gpx_json renderer for a view which could then just return a GpxModel 
instance.  From reading the documentation I would expect to be able to do:

    config.add_renderer('json_gpx', JSON(cls=GpxJsonEncoder))

since keyword arguments ('cls' in this case) are passed to the underlying 
serializer which is json.dumps().

Unfortunately, this doesn't work as I would expect because Pyramid 
intervenes and passes a 'default' callback argument to the serializer which 
causes serialization to fail within the Pyramid defined default callback:

      File 
"/Users/rjs/dev/virtualenvs/sageroux/lib/python3.3/site-packages/pyramid/renderers.py",
 
line 273, in default
        raise TypeError('%r is not JSON serializable' % (obj,))
    TypeError: <trailer.model.gpx_model.GpxModel object at 0x10a13c310> is 
not JSON serializable

There doesn't seem to be any way to prevent Pyramid interjecting with this 
callback other than defining a customer serializer wrapping json.dumps() 
which accepts, but then ignores, the default argument, like this:

    def gpx_dumps(obj, default, **kwargs):
        # This function is just a wrapper for json.dumps() which ignores the
        # default parameter passed to the serializer by Pyramid, which would
        # otherwise cause the JSONEncoder subclass to fail.
        return json.dumps(obj, **kwargs)

    config.add_renderer('json_gpx', JSON(serializer=gpx_dumps, 
cls=GpxJsonEncoder))

Is there are better solution for using off-the-shelf JSONEncoder 
subclasses?  I think the API for JSON() needs to be slightly more flexible 
to avoid the need for the serializer wrapper. Should this be considered a 
bug or shortcoming in Pyramid?

Rob Smallshire

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/pylons-discuss/-/PDKRO6WCo2wJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.

Reply via email to