Extending JSON renderer produces "not defined" error

265 views
Skip to first unread message

Zak

unread,
Nov 9, 2012, 10:36:36 PM11/9/12
to pylons-...@googlegroups.com
I'm trying to follow the instructions here to configure the JSON renderer. I want to add a "default=json_util.default" parameter to the JSON render's "json.dumps" line. Here is the config line I'm using to do that:

config.add_renderer('MongoSafeJSON', JSON(default=json_util.default))

The compiler then throws the error "NameError: global name 'JSON' is not defined". Am I missing an import? Is there some part of the instructions I'm missing? Am I even configuring this correctly to product the results I want?

Michael Merickel

unread,
Nov 9, 2012, 11:08:23 PM11/9/12
to Pylons
I'm assuming you're failing to import "from pyramid.renderers import JSON".


--
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/-/vyHbjZT1fAkJ.
To post to this group, send email to pylons-...@googlegroups.com.
To unsubscribe from this group, send email to pylons-discus...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.

Zak

unread,
Nov 9, 2012, 11:24:39 PM11/9/12
to pylons-...@googlegroups.com
Thanks, that got rid of the undefined name error, but now the server is throwing a 200 Internal Server Error when I try to use "MongoSafeJSON". There's no error description. I assume there is something wrong with my default=json_util.default line...does the renderer know specifically to add that argument to its json.dumps line?

Michael Merickel

unread,
Nov 9, 2012, 11:33:43 PM11/9/12
to Pylons
Maybe the docs aren't entirely clear, but they do state that the JSON renderer has a serializer which must accept a "default" and any OTHER "kwargs". This is saying that default is not an acceptable option to pass as a kwarg.

If you want to supply a default, you can pass it as an adapter for "object".

JSON(adapters=[(object, json_util.default)])

But you'd be better off breaking your default apart and adding individual adapters for specific types.


On Fri, Nov 9, 2012 at 10:24 PM, Zak <zakd...@gmail.com> wrote:
Thanks, that got rid of the undefined name error, but now the server is throwing a 200 Internal Server Error when I try to use "MongoSafeJSON". There's no error description. I assume there is something wrong with my default=json_util.default line...does the renderer know specifically to add that argument to its json.dumps line?

--
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/-/DjyQkVaaf7EJ.

Zak

unread,
Nov 10, 2012, 12:00:01 AM11/10/12
to pylons-...@googlegroups.com
When you say "breaking your default apart" do you mean write my own custom renderer or add adapters to the built-in JSON renderer?

Michael Merickel

unread,
Nov 10, 2012, 12:05:02 AM11/10/12
to Pylons
I'm saying that you probably have a "default" function that does a bunch of isinstance calls or whatnot. You can use it the way I suggested, adding it as an adapter for "object", or you can break it apart and add individual adapters for each of the types. You can do whatever you want of course, but it feels like you might be ignoring the `add_adapter` method on the JSON instance.


On Fri, Nov 9, 2012 at 11:00 PM, Zak <zakd...@gmail.com> wrote:
When you say "breaking your default apart" do you mean write my own custom renderer or add adapters to the built-in JSON renderer?

--
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/-/Hflq2S06r_wJ.

Zak

unread,
Nov 10, 2012, 12:10:56 AM11/10/12
to pylons-...@googlegroups.com
Interesting, I'll definitely take a look at using add_adapter to my root object. Thanks for your help.
Reply all
Reply to author
Forward
0 new messages