[Catalyst] Catalyst::Controller:REST and JSON vs JSONP issue

James Spath jspath at pangeamedia.com
Fri Aug 26 12:41:15 GMT 2011


On Fri, Aug 26, 2011 at 7:32 AM, Dave Howorth
<dhoworth at mrc-lmb.cam.ac.uk> wrote:
> There's a mail from Jim Spath in the pipermail archive that didn't make
> it to the other archives (or to my inbox). It hasn't had any answers so
> I'd suggest he repost it.

Thanks Dave!

Here it is:

So it turns out there are certain characters that are perfectly legal
JSON that are NOT legal JavaScript:

http://timelessrepo.com/json-isnt-a-javascript-subset

We have run into this problem in our application which uses
Catalyst::Controller::REST to output certain data as JSON or JSONP.

Catalyst::Controller::REST first encodes the data as JSON, and then to
create JSONP, simply wraps the JSON in a callback function, and no
attempt is made to deal with possible illegal characters.

I believe a solution would involve replacing the illegal characters in
Catalyst::Action::Serialize::JSON before the string is wrapped in the
callback function.

Has anyone else run into this problem?

> FWIW, IMHO the issue is better fixed in JSON::XS et al than in
> Catalyst::Action::Serialize::JSON. I don't see why JSON can't always
> encode the two problematic characters.

I suppose it could be in JSON::XS .. but JSON::XS is doing nothing
wrong ... the two characters are perfectly valid JSON ... but they are
NOT valid in JavaScript.

Perhaps it could be an option in JSON::XS?

- Jim



More information about the Catalyst mailing list