naohirozrx / reallysimplehistory

Automatically exported from code.google.com/p/reallysimplehistory
Other
0 stars 0 forks source link

RSH0.6 beta (json.js) uses Object.prototype and breaks other API (prototype.js) using for..in loops #6

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
> 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" 

Original issue reported on code.google.com by olivier....@gmail.com on 24 Oct 2007 at 3:16

GoogleCodeExporter commented 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

GoogleCodeExporter commented 8 years ago
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

GoogleCodeExporter commented 8 years ago
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:

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
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

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
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

GoogleCodeExporter commented 8 years ago
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

GoogleCodeExporter commented 8 years ago
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