5

I'm in the process of learning backbone and I keep seeing these libraries mentioned.

Can't one just use two simple functions on the client,

JSON.stringify and JSON.parse

for JSON functionality?

Particularly, this tutorial here.

w5m
  • 2,286
  • 3
  • 34
  • 46
  • 1
    I believe they're for older browsers that don't have a JSON object. If the browser does implement a JSON object, they use it instead. – Corey Ogburn Feb 22 '13 at 15:33
  • 1
    http://caniuse.com/json – Andreas Feb 22 '13 at 15:33
  • This is discussed here too... http://stackoverflow.com/questions/552135/difference-between-json-js-and-json2-js They augment the functionality of the existing JSON parsers. – Donald Ward Feb 22 '13 at 15:35

6 Answers6

6

Some older browsers don't have these JSON functions built in - so the JSON JavaScript library contains code that poly-fills these older browsers to work just like the newer ones.

The notable exceptions are IE 7 and below.

Fenton
  • 241,084
  • 71
  • 387
  • 401
1

They're for parsing json, I use it on my music player site: My music player site

Basically it turns them into accessible variables that you can easily use.

Zachrip
  • 3,242
  • 1
  • 17
  • 32
0

Yes you can, provided your environment supports them. But some older browsers do not include the JSON global.

These libraries fill in the gaps.

See Can I Use for explicit details.

Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103
0

The JSON object is relatively new, and as such isn't supported by older browsers. Those files are intended to implement that functionality for cross-browser support and backwards compatibility.

From http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js:

This file creates a global JSON object containing two methods: stringify and parse.

Anthony Grist
  • 38,173
  • 8
  • 62
  • 76
0

Difference between json.js and json2.js

I guess parseJSON is obsolete, therefore the new version (json2) doesn't even use it anymore. However if your code uses parseJSON a lot you could just add this piece of code somewhere to make it work again

Community
  • 1
  • 1
Sirwan Afifi
  • 10,654
  • 14
  • 63
  • 110
0

It's for legacy browser support

From the GitHub page (emphasis mine):

json2.js: This file creates a JSON property in the global object, if there isn't already one, setting its value to an object containing a stringify method and a parse method. The parse method uses the eval method to do the parsing, guarding it with several regular expressions to defend against accidental code execution hazards. On current browsers, this file does nothing, preferring the built-in JSON object. There is no reason to use this file unless fate compels you to support IE8, which is something that no one should ever have to do again.

Thunderforge
  • 19,637
  • 18
  • 83
  • 130