By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,796 Members | 1,205 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,796 IT Pros & Developers. It's quick & easy.

JSON modifies Object.prototype?

P: n/a
Hello,

I noticed that the JavaScript library for JSON posted on json.org
(http://www.json.org/json.js) is modifying Object.prototype (adding a
method - toJSONString). I thought this was considered bad practice
because it can disrupt the use of for in loops on Objects.

Am I incorrect?

Thanks,

Kevin N.
May 2 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
> I noticed that the JavaScript library for JSON posted on json.org
(http://www.json.org/json.js) is modifying Object.prototype (adding a
method - toJSONString). I thought this was considered bad practice
because it can disrupt the use of for in loops on Objects.

Am I incorrect?


it did so for me too, so I switched to:
http://www.litfuel.net/mybic/

I currently only use JSON.parse & JSON.stringify, I
would use mybic server side if it supported iso-8859-1

arnulf @ http://rlb.no/my/
May 2 '06 #2

P: n/a
Arnulf Sortland wrote:
I noticed that the JavaScript library for JSON posted on json.org
(http://www.json.org/json.js) is modifying Object.prototype (adding a
method - toJSONString). I thought this was considered bad practice
because it can disrupt the use of for in loops on Objects.

Am I incorrect?


it did so for me too, so I switched to:
http://www.litfuel.net/mybic/

I currently only use JSON.parse & JSON.stringify, I
would use mybic server side if it supported iso-8859-1

arnulf @ http://rlb.no/my/


I modified the one from json.org to use the same interface as the one
you have described (and as the Actionscript version):

http://www.unfocus.com/projects/sour...script/json.js
/*
json.js (modified 2006-05-02)

USAGE:
var jsObj = JSON.parse(jsonStr);
var jsonStr = JSON.stringify(jsObj);
*/
var json = (function () {
var m = {
'\b': '\\b',
'\t': '\\t',
'\n': '\\n',
'\f': '\\f',
'\r': '\\r',
'"' : '\\"',
'\\': '\\\\'
},
s = {
array: function (x) {
var a = ['['], b, f, i, l = x.length, v;
for (i = 0; i < l; i += 1) {
v = x[i];
f = s[typeof v];
if (f) {
v = f(v);
if (typeof v == 'string') {
if (b) {
a[a.length] = ',';
}
a[a.length] = v;
b = true;
}
}
}
a[a.length] = ']';
return a.join('');
},
'boolean': function (x) {
return String(x);
},
'null': function (x) {
return "null";
},
number: function (x) {
return isFinite(x) ? String(x) : 'null';
},
object: function (x) {
if (x) {
if (x instanceof Array) {
return s.array(x);
}
var a = ['{'], b, f, i, v;
for (i in x) {
v = x[i];
f = s[typeof v];
if (f) {
v = f(v);
if (typeof v == 'string') {
if (b) {
a[a.length] = ',';
}
a.push(s.string(i), ':', v);
b = true;
}
}
}
a[a.length] = '}';
return a.join('');
}
return 'null';
},
string: function (x) {
if (/["\\\x00-\x1f]/.test(x)) {
x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
var c = m[b];
if (c) {
return c;
}
c = b.charCodeAt();
return '\\u00' +
Math.floor(c / 16).toString(16) +
(c % 16).toString(16);
});
}
return '"' + x + '"';
}
};

/*Object.prototype.toJSONString = function () {
return s.object(this);
};

Array.prototype.toJSONString = function () {
return s.array(this);
};*/

return {
parse: function(s) {
try {
return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
s.replace(/"(\\.|[^"\\])*"/g, ''))) &&
eval('(' + s + ')');
} catch (e) {
return false;
}
},
stringify: s.object
};
})();

/*String.prototype.parseJSON = function () {
try {
return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
this.replace(/"(\\.|[^"\\])*"/g, ''))) &&
eval('(' + this + ')');
} catch (e) {
return false;
}
};*/
Maybe it'll be useful for someone,

Kevin N.
May 2 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.