开发者

Encoding JSON in Mako?

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, &am开发者_StackOverflow中文版p;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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜