How can I configure JSON encoding for Pyramid?

I am trying to return a function like 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) 

Due to Pyramid's own JSON encoding, it exits binary encoding as follows:

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

How can i fix this? I need to use the default argument (or equivalent) as it is required for Mongo custom types.

+6
source share
4 answers

The dictionary seems to be JSON encoded twice, which is equivalent to:

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

Perhaps your Python system automatically JSON encodes the result? Try instead:

 def returnJSON(color, message=None): return { "color" : "color", "message" : "message" } 

EDIT:

To use the Pyramid custom renderer that generates JSON the way you want, try this (based on visualization documents and visualization sources ).

At startup:

 from pyramid.config import Configurator from pyramid.renderers import JSON config = Configurator() config.add_renderer('json_with_custom_default', JSON(default=json_util.default)) 

Then you use the renderer 'json_with_custom_default' to use:

 @view_config(route_name='CreateNewAccount', request_method='GET', renderer='json_with_custom_default') 

EDIT 2

Another option would be to return a Response object, which it should not modify. For instance.

 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) 
+8
source

In addition to other excellent answers, I would like to point out that if you do not want the data returned by the view function to be passed through json.dumps, you should not use renderer = "json" in view configuration :)

So instead

 @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) 

you can just use

 @view_config(route_name='CreateNewAccount', request_method='GET', renderer='string') def returnJSON(color, message=None): return json.dumps({ "color" : "color", "message" : "message" }, default=json_util.default) 

string renderer will just pass the string data returned by your as-is function. However, registering custom rendering is a more convenient approach (see @orip answer).

+2
source

You did not say, but I assume that you are just using the standard json module.

The json module does not define a class for JSON; it uses the standard Python dict as a "native" representation of your data. json.dumps() encodes a dict as a JSON string; json.loads() takes a JSON string and returns a dict .

So instead:

 def returnJSON(color, message=None): return json.dumps({ "color" : "color", "message" : "message" }, default=json_util.default) 

Try to do this:

 def returnJSON(color, message=None): return { "color" : "color", "message" : "message" } 

Just return a simple dict . See how you like your iPhone app.

+1
source

You discard the string of the Python object (dictionary) that you give it.

The manual for json.dumps states:

Serialize obj to a formatted JSON string.

To convert back from a string, you will need to use the Python JSON load function, which loads the string into a JSON object.

However, it looks like what you are trying to do is an encode python dictionary for JSON.

 def returnJSON(color, message=None): return json.encode({ "color" : color, "message" : message }) 
0
source

Source: https://habr.com/ru/post/917393/


All Articles