473,604 Members | 2,483 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

JSON modifies Object.prototyp e?

Hello,

I noticed that the JavaScript library for JSON posted on json.org
(http://www.json.org/json.js) is modifying Object.prototyp e (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
2 3741
> I noticed that the JavaScript library for JSON posted on json.org
(http://www.json.org/json.js) is modifying Object.prototyp e (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
Arnulf Sortland wrote:
I noticed that the JavaScript library for JSON posted on json.org
(http://www.json.org/json.js) is modifying Object.prototyp e (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(json Str);
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.prototy pe.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.prototy pe.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
1754
by: Raphael Jolivet | last post by:
Hello there, I have a bunch of objects that I store in a text file (to store preferences), after having serialized them with JSON. Let's say I have my object : obj1 = { a : 1
8
10402
by: Douglas Crockford | last post by:
There is a new version of JSON.parse in JavaScript. It is vastly faster and smaller than the previous version. It uses a single call to eval to do the conversion, guarded by a single regexp test to assure that it is safe. JSON.parse = function (text) { return (/^(\s|]|"(\\|)*"|-?\d+(\.\d*)?(?\d+)?|true|false|null)+$/.test(text)) && eval('(' + text + ')'); };
9
1685
by: eggie5 | last post by:
eval is not working on this JSON string: {"top":} {"car":} {"man":} data = eval('(' + json + ')'); What's wrong with the json string, is it 'cause it has no root?
2
3313
by: ChrisO | last post by:
I've been pretty infatuated with JSON for some time now since "discovering" it a while back. (It's been there all along in JavaScript, but it was just never "noticed" or used by most until recently -- or maybe I should just speak for myself.) The fact that JSON is more elegant goes without saying, yet I can't seem to find a way to use JSON the way I *really* want to use it: to create objects that can be instantated into multiple...
6
2167
by: dd | last post by:
I'm writing something in JS using the latest OO and JSON and I'm looking for a bit of guidance. I'm going to have this large object which has many top-level properties and some top-level functions. That part I have no problem with, I'll define it in JSON style (my prototype is working just fine). What I'll also have though, is some arrays at the top level. These arrays will contain n number of sub-objects. Each of these sub-objects will...
2
2508
by: holtmann | last post by:
Hi, I got a question regarding JSON as datasource- I understand that eval on a JSON String creates the appropriate objects in JS. But I would like to use JSON to supply data to already defined prototpye objects. I try to give an example. I e.g. got: address.prototype = { firstname: null;
23
3193
by: dhtmlkitchen | last post by:
JSON We all know what it is. In ECMAScript 4, there's a JSON proposal: Object.prototype.toJSONString String.prototype.parseJSON The current proposal, String.prototype.parseJSON, returns an object.
6
2813
by: Lasse Reichstein Nielsen | last post by:
Max <adsl@tiscali.itwrites: Not really. It shows that a particularly naïve implementation of a conversion from XML to JSON doesn't work well. What if the conversion of <e> some
0
7929
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8419
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8409
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8065
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8280
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5882
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5441
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3955
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2434
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.