drupal_to_js escapes apostrophes in strings, despite wrapping the string in quotes. The result string will look something like:

"a \'test\' string"

This causes errors with the json-framework Obj-C library, which is not looking for escaped apostrophes in a string surrounded by double quotes. A recommended fix: replace addslashes($var) with addcslashes($var, '\\"')). Additional characters may need escaping, but this seems to work for a relatively large dataset.

Comments about compatibility with other libraries are encouraged.

CommentFileSizeAuthor
#1 479368_drupal_to_js.patch597 bytesburningdog

Comments

burningdog’s picture

Status: Active » Needs review
StatusFileSize
new597 bytes

Patch attached, same as the D6 patch (waiting to be tested) here: http://drupal.org/node/479368#comment-2562104

heine’s picture

Status: Needs review » Needs work

Indeed, RFC 4627 is quite clear on the characters that may be escaped by the reverse solidus and a single quote is not among them:

 string = quotation-mark *char quotation-mark

         char = unescaped /
                escape (
                    %x22 /          ; "    quotation mark  U+0022
                    %x5C /          ; \    reverse solidus U+005C
                    %x2F /          ; /    solidus         U+002F
                    %x62 /          ; b    backspace       U+0008
                    %x66 /          ; f    form feed       U+000C
                    %x6E /          ; n    line feed       U+000A
                    %x72 /          ; r    carriage return U+000D
                    %x74 /          ; t    tab             U+0009
                    %x75 4HEXDIG )  ; uXXXX                U+XXXX

         escape = %x5C              ; \

         quotation-mark = %x22      ; "
heine’s picture

Version: 5.17 » 6.x-dev

Bumping to D6.

burningdog’s picture

Status: Needs work » Needs review

Sorry, I didn't make that clear: it's not the apostrophe that's the problem, it's the backslash escaping the apostrophe.

Take this JSON:

{ "value": "Shouldn\'t this work?" }

It doesn't validate at http://www.jsonlint.com/ To get it to validate, take out the backslash:

{ "value": "Shouldn't this work?" }

My patch at http://drupal.org/node/479368#comment-2562192 fixes this for D6.

heine’s picture

Status: Needs review » Needs work

I'm not arguing against the backslash escape being a problem ("Indeed, RFC 4627 is quite clear on the characters that may be escaped by the reverse solidus and a single quote is not among them"), I just don't see how your patch can work as per my comment on the other issue.

heine’s picture

Status: Needs work » Closed (duplicate)

Let's mark this a 'duplicate' of #479368: D7: Create RFC compliant HTML safe JSON for Drupal 5 and 6.

burningdog’s picture

@Heine, I re-rolled the patch as per your comments on the duplicate - it should work now :)