/    Sign up×
Community /Pin to ProfileBookmark

JavaScript JSON Libraries

Hi exploring on the JavaScript JSON libraries and as usual I am using Douglas Crockford json2.js

I discover his implementation is very [B]STRICT[/B] based on the JSON grammar syntax he has specified. I am actually looking for a equivalent that is “looser” in terms of implementation.

First,

[code]
This fail!
myData = JSON.parse(‘{
“a”:100,
“b”:200
}’);
[/code]

For normal developer, new-line should not be considered as part of the grammar isn’t it ? Above fail the JSON parsing. This is too strict!!! I know the JSON grammar did not mention about new-line but then shouldn’t the implementation be a little lax a bit ?

[code]
This works!
myData = JSON.parse(‘{“a”:100,”b”:200}’);
[/code]

Second,

[code]
This fail!
myData = JSON.parse(‘{a:100,b:200}’);
[/code]

[code]
This works!
myData = JSON.parse(‘{“a”:100,”b”:200}’);
[/code]

For normal developer, the properties in double-quote or not should not be considered as part of the grammar isn’t it ? Above fail the JSON parsing. This is too strict!!! I know the JSON grammar did mention about double-quote but then shouldn’t the implementation be a little lax a bit ?

Conclusion: Anyone has a more “lax abit” JSON grammar implementation besides the one done by Douglas Crockford ? ?

Edit:
json.org mention “Whitespace can be inserted between any pair of tokens. Excepting a few encoding details, that completely describes the language.”

So obviously Douglas interpret whitespace as spaces and tabs but NOT new-lines ?!?!

[code]
This works again!
myData = JSON.parse(‘{ “a”:100, “b”:200 }’);
[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@KorNov 26.2010 — 
<i>
</i>This fail!
myData = JSON.parse('{[COLOR="Red"] // oops! this is the end of the line[/COLOR]
"a":100, [COLOR="Red"]// oops! this is also the end of the line[/COLOR]
"b":200[COLOR="Red"] // oops! this is also the end of the line[/COLOR]
}');

[/QUOTE]


Simple: The argument of the parse() method is a [I]string[/I]. In JavaScript you can not fragment a string with line breaks - they are interpreted as an end of the code's line. That has nothing to do with JSON. It's just the JavaScript syntax particularity. Should you have used a professional editor (as Zend or DW CS5) it would mark those end lines as syntax errors.
Copy linkTweet thisAlerts:
@sohguanhauthorNov 26.2010 — Simple: The argument of the parse() method is a [I]string[/I]. In JavaScript you can not fragment a string with line breaks - they are interpreted as an end of the code's line. That has nothing to do with JSON. It's just the JavaScript syntax particularity[/QUOTE]

Hi thanks for your feedback. Although I have Crockford json2.js source code and I can change but looking at his JavaScript code is quite daunting.

Hmmm.... I prefer to let others do the "enhancement". Any takers? ?

Edit: I thot of a simple way. Do a reg exp to search for all n in the AJAX response.text and replace into space before calling JSON.parse. Problem solved! This should be easy to do I guess.
×

Success!

Help @sohguanh spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...