From: Thomas 'PointedEars' Lahn on
Lasse Reichstein Nielsen wrote:

> Lasse Reichstein Nielsen <lrn.unread(a)gmail.com> writes:
>> Thomas 'PointedEars' Lahn <PointedEars(a)web.de> writes:
>>> Is this from json2.js? If yes, then it is not acceptable. To begin
>>> with, it does not regard "\"" valid JSON even though it is.
>>
>> It didn't use to be valid.
>> Originally, a JSON text had to be either an object or an array, but not
>> a simple value.
>> This was changed at some point (I'm guessing during ES5 development) so
>> that the grammar on json.org and the one in the ES5 spec allow JSON text
>> to be any JSON value.
>> JSON2 implements the original version.
>
> Silly me, answering before checking.
> It actually does allow "\"" as valid JSON.

But it does so in a bogus way, by removing all escape sequences
before testing the string literal, without considering the context.


PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
From: Lasse Reichstein Nielsen on
Thomas 'PointedEars' Lahn <PointedEars(a)web.de> writes:

> But it does so in a bogus way, by removing all escape sequences
> before testing the string literal, without considering the context.

It doesn't remove them. It replaces them with a "@", which isn't part
of any valid token outside of a string literal. So, after replacing
all valid escape sequences by "@", if what remains is valid JSON, so
was the original - and there are no longer escapes in the string
literals, so you don't have to worry about matching the wrong
double quote.

/L
--
Lasse Reichstein Holst Nielsen
'Javascript frameworks is a disruptive technology'

From: Asen Bozhilov on
Garrett Smith wrote:

> A parser would be too much for the FAQ.

If you implemented full parser for JSON that definitely is not
necessary for FAQ. You can use something like this:

if (!this.JSON) {
this.JSON = {
parse : (function () {
var JSON_GRAMMAR = {
STRING : '"([^"\\\\\\x00-\\x1F]|\\\\["\\\\\/bfnrt]|\\\
\u[0-9A-Fa-f]{4})*"',
NUMBER : '-?\\d+(?:\\.\\d+)?(?:[Ee][+-]?\\d+)?',
BOOLEAN : 'true|false',
NULL : 'null'
};
var REG_EXP = {
property : new RegExp(JSON_GRAMMAR.STRING + '\\s*:', "g"),
value : new RegExp(JSON_GRAMMAR.STRING + '|' +
JSON_GRAMMAR.NUMBER + '|' + JSON_GRAMMAR.BOOLEAN + '|' +
JSON_GRAMMAR.NULL, "g"),
invalidTokens : /[^{}\[\],\s]/ ,
};
return function(jsonStr) {
var output;
if
(REG_EXP.invalidTokens.test(jsonStr.replace(REG_EXP.property,
'').replace(REG_EXP.value, ''))) {
throw new SyntaxError('JSON.parse');
}
output = new Function('return Array(' + jsonStr + ');')();
/**
* Here you can check:
* if length property of output is greater that 1
* OR
* output[0] instanceof Array
* throw `JSON.parse` error
*/
return output[0];
};
})()
};
}

For string regular expression I am using Thomas Lahn approach.

From: Asen Bozhilov on
Asen Bozhilov wrote:

>         output = new Function('return Array(' + jsonStr + ');')();

Correction, should be:

output = new Function('return Array(null, ' + jsonStr + ');')();
/**
* Here you can check:
* if length property of output is greater that 2
* OR
* output[1] instanceof Array
* throw `JSON.parse` error
*/
return output[1];



From: Garrett Smith on
On 2010-06-18 03:00 AM, Asen Bozhilov wrote:
> Asen Bozhilov wrote:
>
>> output = new Function('return Array(' + jsonStr + ');')();
>
> Correction, should be:
>
> output = new Function('return Array(null, ' + jsonStr + ');')();

Why `null` as first element?

> /**
> * Here you can check:
> * if length property of output is greater that 2
> * OR
> * output[1] instanceof Array
> * throw `JSON.parse` error
> */
> return output[1];

That strategy you've employed is one I considered; though there are a
couple of issues with some of the patterns.

I've not finished my reply to Thomas; it's been in draft for about three
days.

Garrett