Connecting Tech Pros Worldwide Forums | Help | Site Map

JSON modifies Object.prototype?

Kevin Newman
Guest
 
Posts: n/a
#1: May 2 '06
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.

Arnulf Sortland
Guest
 
Posts: n/a
#2: May 2 '06

re: JSON modifies Object.prototype?


> I noticed that the JavaScript library for JSON posted on json.org[color=blue]
> (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?[/color]

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/
Kevin Newman
Guest
 
Posts: n/a
#3: May 2 '06

re: JSON modifies Object.prototype?


Arnulf Sortland wrote:[color=blue][color=green]
>> 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?[/color]
>
> 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/[/color]

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.
Closed Thread