tags:

views:

39

answers:

1

Im having trouble with json in mako. I do this:

${ to_json( dict( a = 1, b = 2 ) ) }

where to_json is:

<%!
    import simplejson as json

    def to_json( d ):
        return json.dumps( d )
%>

however, instead of giving me

{"a": "1", "b": "2"}

its giving me

{&quot;a&quot;: 1, &quot;b&quot;: 2}

so mako changes the " to " somewhere

what should i be doing instead?

in contrast, heres a test script

import simplejson as json

print json.dumps( dict( a=1,b=2 ) )

output

{"a": 1, "b": 2}

edit

i changed my function to

<%!
    import simplejson as json

    def to_json( d ):
        return "{\"a\": 1}"
%>

and it changes the " to &quot;, so its an issue with mako, it seems.

+1  A: 

seems like theres an auto filter somewhere, so when i changed

${ to_json( dict( a = 1, b = 2 ) ) }

to

${ to_json( dict( a = 1, b = 2 ) ) | n }

to turn off filters, it is okay, thanks

Timmy
Be thankful for the filter, it's there to help keep javascript injection/XSS from biting you! But a pain when you don't want it.
Bill Gribble
not to say im not thankful, just didn't know!
Timmy