views:

76

answers:

1

I want to use json2 as JSON.parse in node.js, (forgive the stupid question) I can't quite figure out how to use it. I have a copy of json2.js, with the first line removed in my current working directory. Then, from the node.js shell i do:

> orig_func = JSON.parse
[Function: parse]
> require('json2')
{ JSON: {} }
> orig_func === JSON.parse
true

I thought from the comments in the code that by requiring the file it would override the current global JSON object.

+1  A: 

json2 checks for the existence of the JSON object before it overrides it. To use json2 you'd need to do something like

var oldJSON = JSON;
JSON = undefined;
require('json2');
JSON.stringify = oldJSON.stringify; // assuming you want builtin stringify

But note that the JSON implementation in json2.js is not 100% correct, is much slower than the builtin impl, and is less secure.

olliej
You know the reason why I was looking into JSON2 was because of some comments that I read here: http://news.ycombinator.com/item?id=1739995 and here http://hns.github.com/2010/09/29/benchmark2.html#comment-82051337 where the dev for the GC on V8 says JSON.parse has some memory issues. I cannot find memory leaks in my code, but I am seeing it consume more and more memory over time.
roder
I would use the builtin JSON impl as I would assume the V8 team will fix these problems
olliej