4

Ok so I've been dealing with a PHP 5.3 server returning a hand-made JSON (because in 5.3 there's no JSON_UNESCAPE_UNICODE in the json_encode function) and after reading this thread and making some tests, I think I've found a problem in jQuery's parseJSON function.

Suppose I have the following JSON:

{
    "hello": "hi\nlittle boy?"
}

If you check it using jsonlint.com you can see it's valid JSON. However, if you try the following, you get an error message:

$(function(){
    try{
        $.parseJSON('{ "hello": "hi\nlittle boy?" }');
    } catch (exception) {
        alert(exception.message);
    }    
});​

Link to the fiddle.

I've opened a bug report at jQuery, because I think it's a proper bug. What do you think?

Community
  • 1
  • 1
José Tomás Tocino
  • 9,873
  • 5
  • 44
  • 78

1 Answers1

12

It's not a bug, it has to do with how the string literal is handled in JavaScript. When you have:

'{ "hello": "hi\nlittle boy?" }'

...your string will get parsed into:

{ "hello": "hi
little boy?" }

...before it is passed to parseJSON(). And that clearly is not valid JSON, since the \n has been converted to a literal newline character in the middle of the "hi little boy?" string.

You want the '\n' sequence to make it to the parseJSON() function before being converted to a literal newline. For that to happen, it needs to be escaped twice in the literal string. Like:

'{ "hello": "hi\\nlittle boy?" }'

Example: http://jsfiddle.net/m8t89/2/

aroth
  • 54,026
  • 20
  • 135
  • 176
  • Mmm I'm not really sure about it not being a bug though. Despite how string literals are handled in JavaScript, jQuery should check if a character is a newline character and accept it, because it's possible to do it: http://jsfiddle.net/UTVcW/ – José Tomás Tocino Oct 07 '12 at 02:24
  • @JoseTomasTocino - They could do that, yes. But then their JSON parser would not be following the [JSON spec](http://www.json.org/), which explicitly excludes literal "control characters" (such as 'carriage return' and 'linefeed') from its definition of a valid string. So if this is a bug, it's a bug in the spec, not a bug in jQuery's (or anybody else's) implementation of it. – aroth Oct 07 '12 at 02:29
  • Well, in the JSON Spec, the [string sequence](http://www.json.org/string.gif) seems to accept "\n" characters (that would actually be two characters, I get it). It would be sensible to add an exception in jQuery's parser so if there's a single-character "\n", they associate it to the two-chracter "\n" and accept the incoming string. I can't think of any case in which acting otherwise could be harmful. – José Tomás Tocino Oct 07 '12 at 02:35
  • Ok, looks like both Chrome and firefox javascript engines behave the same way, saying the json is erroneous. This is pretty much a lost battle. – José Tomás Tocino Oct 07 '12 at 02:40