python - How can I configure Pyramid's JSON encoding? -


i'm trying return function this:

@view_config(route_name='createnewaccount', request_method='get', renderer='json') def returnjson(color, message=none):     return  json.dumps({ "color" : "color", "message" : "message" }, default=json_util.default) 

because of pyramid's own json encoding, it's coming out double-encoded this:

"{\"color\": \"color\", \"message\": \"message\"}" 

how can fix this? need use default argument (or equivalent) because it's required mongo's custom types.

it seems dictionary being json-encoded twice, equivalent of:

json.dumps(json.dumps({ "color" : "color", "message" : "message" })) 

perhaps python framework automatically json-encodes result? try instead:

def returnjson(color, message=none):   return { "color" : "color", "message" : "message" } 

edit:

to use custom pyramid renderer generates json way want, try (based on renderer docs , renderer sources).

in startup:

from pyramid.config import configurator pyramid.renderers import json  config = configurator() config.add_renderer('json_with_custom_default', json(default=json_util.default)) 

then have 'json_with_custom_default' renderer use:

@view_config(route_name='createnewaccount', request_method='get', renderer='json_with_custom_default') 

edit 2

another option return response object renderer shouldn't modify. e.g.

from pyramid.response import response def returnjson(color, message):   json_string = json.dumps({"color": color, "message": message}, default=json_util.default)   return response(json_string) 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -