Bug 46057 - Bad end-of-line management in JSON.parse
Summary: Bad end-of-line management in JSON.parse
Status: RESOLVED INVALID
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: 528+ (Nightly build)
Hardware: PC All
: P2 Normal
Assignee: Nobody
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-09-19 11:55 PDT by Guilaume Ayoub
Modified: 2010-09-20 12:20 PDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Guilaume Ayoub 2010-09-19 11:55:08 PDT
Using the '\n' string (not the EOL character, the string with '\' and 'n') in JSON.parse gives an error. The error happens both in the javascript console and when parsing a sting outside the console.

Example:
1. Open the javascript console
2. Type: JSON.parse('["\n"]')

This should create a list containing a string with the EOL character, but it gives an error. The bug appears with Epiphany (Linux) and Safari (Windows). No bug with Firefox or Chrome.
Comment 1 Oliver Hunt 2010-09-20 11:58:34 PDT
Firefox and Chrome are both incorrect -- When you do JSON.parse('"\n"') you are asking for the JSON parser to accept a new line in the middle of a string literal.

Because you have not escaped the new line character in the string that you pass to JSON, you have only escaped it in the JS parser.  The string passed to JSON.parse must include the escapes so you actually need to do

JSON.parse('"\\n"')

The \\ escapes the \ when the script is parse, so you are then passing the string "\n" to the JSON parser.
Comment 2 Oliver Hunt 2010-09-20 12:07:25 PDT
And it's fixed in firefox 4, and i've filed http://code.google.com/p/v8/issues/detail?id=870
Comment 3 Guilaume Ayoub 2010-09-20 12:20:07 PDT
Thank you for your very clear explanation, I'm sorry for the noise.