Closed GoogleCodeExporter closed 8 years ago
We 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
Original comment by olivier....@gmail.com
on 24 Oct 2007 at 4:13
This 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.
Original comment by bdpathfi...@gmail.com
on 24 Oct 2007 at 8:49
Great!! :) 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.
Original comment by olivier....@gmail.com
on 25 Oct 2007 at 7:48
Attachments:
[deleted comment]
Hi 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
Original comment by schoemaker@gmail.com
on 31 Oct 2007 at 2:56
[deleted comment]
I 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();
}
Original comment by olivier....@gmail.com
on 1 Nov 2007 at 6:45
Thanks 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.
Original comment by bdpathfi...@gmail.com
on 1 Nov 2007 at 10:26
Thanks a lot Brian. I am currently on holiday but I'll be testing all this next
tuesday.
Olivier
Original comment by olivier....@gmail.com
on 2 Nov 2007 at 12:20
Original issue reported on code.google.com by
olivier....@gmail.com
on 24 Oct 2007 at 3:16