[Dancer-users] to_json not JSON-ifying the entire variable

P Kishor punk.kish at gmail.com
Mon Oct 11 05:18:19 CEST 2010


On Thu, Oct 7, 2010 at 5:03 PM, P Kishor <punk.kish at gmail.com> wrote:
> I have a data object in JavaScript that I am JSONifying, sending to
> the server (using jQuery ajax) and storing in a SQLite table. In
> SQLite, the values look like so (I have prettified it by adding
> returns and tabs)
>
> [
>    {
>        "a":{"aa":0,"ab":448960000,"ac":"140075520.00"},
>        "b":1,
>        "c":"foo"
>    }
> ]
>
> I retrieve it via ajax again, and use Dancer's to_json method to send
> the result back. In Firebug, it shows up as
>
> [
>    {
>        "a":"{\"aa\":0,\"ab\":448960000,\"ac\":\"140075520.00\"}",
>        "b":1,
>        "c":"foo"
>    }
> ]
>
> In other words, to_json seems to stringify all 2nd-level and deeper
> values. As a result, while the entire value is returned as a JSON
> object, its internal parts are recognized as strings. That doesn't
> seem right, no? Its like drinking a cup of coffee, but three sips in
> finding it to be hot chocolate.
>
> I can get around it by reparsing the strings as JSON, but it really
> shouldn't be so. The data are stored in the SQLite table as a string,
> so to_json should JSON-ify it all the way through, and send it back as
> a fully-formed JSON object.
>


The above is really proving to be a bit of a headache for me. Because
to_json is escaping the quotes in the variable, I am not able to
reconstruct the JSON object back in my browser. I am using the json
converter from http://www.json.org/json2.js to convert, but I am
getting a "syntax error" when I try to use `JSON.parse(data)` where
data has been retrieved by the browser.

Any suggestions.

-- 
Puneet Kishor


More information about the Dancer-users mailing list