

JSON 3: A modern JSON polyfill compatible with nearly all JavaScript platforms. - kitcambridge
http://bestiejs.github.com/json3/

======
prodigal_erik
If anyone else was confused, a "polyfill" is a pure javascript implementation
of behavior the platform doesn't natively provide, and calling it "JSON 3" is
not trying to claim that the JSON grammar has been revised (that's pretty much
impossible); it's just the unfortunately generic name they chose for this
implementation.

~~~
jdd
@prodigal_erik @benatkin

It is a JS polyfill/shim/fallback for older browsers like IE 6 and 7 but also
fixes/fills in gaps in support/consistency with modern browsers that have
buggy implementations.

The use of the word "modern" in the description is to suggest it is more up-
to-date than Crockford's aging/stale json2.js.

------
arturadib
Speaking of new JSON versions, is it impossible to create a backwards-
compatible JSON spec that supports _comments_? I've been waiting for this for
ages.

~~~
kodablah
Optional quotes and optional EOL commas would be nice too

~~~
akavlie
While more human friendly, that would inhibit serialization.

------
firefoxman1
On issue 28, it says that when you do

    
    
       JSON.stringify(Object('hi'))
    

The issue reporter says it should return '"hi"' but returns
'{"0":"h","1":"i"}'

In my JS console, if I do

    
    
       Object('hi')
    

It returns _{ '0': 'h', '1': 'i' }_

So to me, it looks like JSON2 correctly serializes the result of Object('hi').
Am I missing something?

<https://github.com/douglascrockford/JSON-js/issues/28>

~~~
jdd
Make sure you're not on a page with external scripts that could be mucking it
up.

If you're recent modern browser `JSON.stringify(Object('hi'))` should produce
'"hi"'.

See <http://cl.ly/2J3B0e1z1o3e0T3x2b0l>.

~~~
firefoxman1
Right,

    
    
       JSON.stringify(Object('hi'))
    

does that, but just plain _Object('hi')_ without wrapping in stringify returns

    
    
       { '0': 'h', '1': 'i' }
    

Is it part of the JSON spec that you first call .toString() on array-like
objects?

~~~
jdd
`Object('hi')` produces the equivalent of `new String('hi')`.

The stringify behavior is spec'ed here <http://es5.github.com/#x15.12.3> [Step
4b of the abstract operation Str(key, holder) algorithm].

See <http://cl.ly/2E1l2X3X3U1B2N1d3G1k>.

~~~
firefoxman1
Ahh that makes sense. I appreciate your research finding that part in the
spec.

------
jacobolus
Thanks for the heads up. I’ve always thought JSON2’s approach of adding all
kinds of stuff to built-in objects was a dumb idea for such an important piece
of infrastructure code. I’ve been tempted on occasion to write my own JSON
codec. I’m glad they tested to make sure it works on every browser of the last
6-7 years: it means JSON3 can pretty much be dropped in and forgotten about
for any project.

Unrelated: their readme reveals a bug in github’s markdown implementation. The
bold is not properly applied in the following example:

    
    
      - **`String.prototype` Methods**: `indexOf`, `charAt`, `slice`.

~~~
kitcambridge
Fixed. Thanks for the heads-up!

------
Gigablah
Your git clone link seems to be outdated, it should be
<https://github.com/bestiejs/json3.git> .

~~~
kitcambridge
Fixed. Thank you!

------
spicyj
I'd be slightly worried that overriding the native functions could lead to
problems in certain situations that rely on the (wrong) existing behavior.

~~~
udp
I don't think it's intended as a replacement for the native functions, rather
an implementation to use where they're missing (which, AFAIK, is rare
nowadays).

~~~
spicyj
If you read the description on the page, it says that they fix certain broken
aspects of some native implementations.

~~~
kitcambridge
That's correct. Currently, Firefox is the only browser that implements the
JSON routines exactly as described in the spec (parsing since version 5.0, and
serializing since 6.0)...others deviate in one or more ways. It's not so much
of a concern if you're targeting multiple browsers, as Firefox will correctly
throw a `SyntaxError` when given malformed input, but it can certainly become
problematic if you're relying on non-standard extensions in only one or two
browsers.

------
polemic
This is bike-shed, but `stringify`? What's wrong with `serialize` or `toJSON`
or `toString'?

~~~
jdd
@polemic @shantanubala `stringify` and `parse` are the names specified by the
ECMAScript language spec: <http://es5.github.com/#x15.12.3>

~~~
polemic
Ah, ok. Thanks!

------
steele
unicode support?

damn you u+2028

