On Aug 16, 1:42 pm, Light and Shadow <
lightandshado...@mac.com> wrote:
> Kangax,
>
> Unfortunately,I don't have control over which version of Prototype is
> in use. My class will be used with many pre-existing sites which may
> or may not be using Prototype (or any Javascript library). However,
> I've upgraded to 1.6.x with the same results.
>
> Without the reference JSON implementation, Prototype's JSON operations
> work fine, but I can't count on it being present.
>
>
http://www.ahtenindustries.com/json/prototype-only.html
The json string in your example actualy is invalid:
"{quickLinkData: {links: [{url:
http://www.ahtenindustries.com, name:
Ahten Industries : Home}]}}"
should be:
'{"quickLinkData": {"links": [{"url": "http://
www.ahtenindustries.com", "name": "Ahten Industries"}]}}'
>
> Nor can I use Prototype instead of the reference implementation as
> Prototype conflicts with other sites using which are currently using
> jQuery.
>
> A temporary work around is to add a check for Object.toJSON to the if
> statement around the reference JSON implementation and conditionally
> use Prototype's methods when present.
>
> if ((!this.JSON) && !(Object.toJSON)) {
>
> / / / / / / / / / / / / / / / / / / / / / / / / / / /
>
> encodedObjectGraph = '';
>
> if(Object.toJSON){
> encodedObjectGraph = Object.toJSON(this.jarRoot);} else {
>
> encodedObjectGraph = JSON.stringify(this.jarRoot);
>
> }
That would work, though it might be safer to check if `Object.toJSON`
actually works as expected:
var toJSON,
testee = {a: true},
exemplar = '{"a": true}';
if (Object.toJSON(testee) === exemplar) {
toJSON = Object.toJSON;
}
else if(JSON.stringify(testee) === exemplar) {
toJSON = JSON.stringify;
}
else {
toJSON = function(s) {
return eval('(' + s + ')');
}
}
--
kangax