
reallysimplehistory - issue #6
RSH0.6 beta (json.js) uses Object.prototype and breaks other API (prototype.js) using for..in loops
> What steps will reproduce the problem? See test case here : http://support.jalios.com/rshPrototype/index.html
> What is the expected output? What do you see instead? I think RSH should not interfere with any other library (as much as it possible), and thus should not extend javascript objects using Object.prototype
> What version of the product are you using? On what operating system? RSH 0.6 beta
> Please provide any additional information below. I am going to post the same issue on the prototype mailinglist in order to see if they plan to fix the "for..in" loop issue using "hasOwnProperty" to prevent a clash.. But it would probably be nice if you could not use "Object.prototype"
Comment #1
Posted on Oct 24, 2007 by Massive ElephantWe have an anwser from developpers of prototype.js : http://groups.google.com/group/prototype-core/t/5bf0ecabe5487f3c
In short, Object prototype is verboten. Prototype library will break if you augment Object.prototype, and this is not likely to change.
[...] We could say, json.js is verboten :)
So ... if you can come up with a solution that does not involve modifying Object.prototype this would be great!
Thanks for listening. Olivier
Comment #2
Posted on Oct 24, 2007 by Happy LionThis is an industry-standard JSON parser. The methods it implements are going to become part of the JavaScript language at some point. The solution here for Prototypers is to substitute Prototype's own JSON methods for the ones in the Crockford parser using my bridge methods. I'm going to improve the API for doing so but need to play with it for a bit. This will definitely get done before the public release.
Comment #3
Posted on Oct 25, 2007 by Massive ElephantGreat!! :) Thanks for this clarification and for your reactivity. I'll be using RSH a lot, so don't hesitate to ask if you need to get some return on the next beta. In the meantime I'll hard code the use of Prototype's JSON methods.
PS : I will remove the test case from our server, so if you ever need to have a look it, I have attached it to this comment.
- rshPrototype.zip 34.18KB
Comment #4
Posted on Oct 28, 2007 by Happy LionComment deleted
Comment #5
Posted on Oct 31, 2007 by Happy RhinoHi all,
I couldn't find the changed methods for the JSON problem by Olivier, but I did it myself like this:
/*private: A bridge for our toJSONString implementation. */
toJSONString: function(s) {
if(typeof s == 'object' && !s.toJSON) {
return $H(s).toJSON(); // the past object seems to have no method, but make it
into a Hash and then apply its toJSON method } else if( typeof s == 'boolean') { return String(s); // boolean also seem to have no JSON-method with Prototype, so I did it like this... } else { return s.toJSON(); } },
/*private: A bridge for our parseJSON implementation. */
parseJSON: function(s) {
return s.evalJSON();
}
Greets,
Alwin
Comment #6
Posted on Oct 31, 2007 by Happy LionComment deleted
Comment #7
Posted on Nov 1, 2007 by Massive ElephantI fixed the problem this way, and it worked for boolean as far as I could see :
toJSONString: function(obj) {
return Object.toJSON(obj);
},
parseJSON: function(s) {
return s.evalJSON();
}
Comment #8
Posted on Nov 1, 2007 by Happy LionThanks all for your suggestions on how to handle this. I ended up using Oliver's solution (calling .toJSON on the Object object and passing it an argument) because it was cleanest.
The lastest version in SVN now contains the following for the JSON methods.
/*private: A bridge for our toJSONString implementation. */
toJSONString: function(o) {
if (typeof JSON != 'undefined' && JSON.stringify) {
return JSON.stringify(o);/*2005 JSON lib*/
}
else if (o.toJSONString) {
return o.toJSONString();/*2007 JSON lib*/
}
else if (Object.toJSON) {
return Object.toJSON(o);/*Prototype*/
}
else {
var e = "No JSON stringify method defined."
throw e;
}
},
/*private: A bridge for our parseJSON implementation. */
parseJSON: function(s) {
if (typeof JSON != 'undefined' && JSON.parse) {
return JSON.parse(s);/*2005 JSON lib*/
}
else if (s.parseJSON) {
return s.parseJSON();/*2007 JSON lib*/
}
else if (s.evalJSON) {
return s.evalJSON();/*Prototype*/
}
else {
var e = "No JSON parse method defined."
throw e;
}
}
I may end up moving this branching logic out of the library and into the initialization code to simplify things for most users. I've also renamed json.js to json2007.js and added json2005.js (the original JSON library Brad used in 0.4) as an alternative. This way, users can:
--use the current industry-standard JSON parser if they want;
--substitute an older version of the parser that doesn't modify Object.prototype;
--or, leave out all JSON libraries and use Prototype's built-in JSON methods.
Comment #9
Posted on Nov 2, 2007 by Massive ElephantThanks a lot Brian. I am currently on holiday but I'll be testing all this next tuesday. Olivier
Status: Fixed
Labels:
Type-Defect
Priority-Medium