Hi, Is there a JSON serializer for prototype or is anyone working on one? I am looking for something equivalent to MochiKit''s JSON support. The serializer from json.org mucks with the prototype of object, array, etc so trips up anyone who writes code using the native iteration support, e.g for (var foo in bar). Carl --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs -~----------~----~----~----~------~----~------~--~---
Carl Shimer wrote:> Hi, > > Is there a JSON serializer for prototype or is anyone working on one? I am > looking for something equivalent to MochiKit''s JSON support. The serializer > from json.org mucks with the prototype of object, array, etc so trips up > anyone who writes code using the native iteration support, e.g for (var foo > in bar).You could ignore any property called "toJSONString", or more generically any property whose value is a function. json.js messes with all basic javascript objects - Array, Boolean, Date, Number, Object and String. Prototype 1.4 messes with Object and Array, Prototype 1.5 messes with Array, so your problems don''t end with fixing json.js. -- Fred --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs -~----------~----~----~----~------~----~------~--~---
If MochiKit has your desired behavior, why not pull theirs in to your project, or port theirs to the prototype model? json.js is quite good, and not at all very large. If you don''t like it''s mucking with the prototypes as it does, why not just rewrite its functions as static functions, where the first argument would be the instance you want to serialize? That would take but 20 minutes or so of work tops. On 10/31/06, Fred <ozfred-AFFH1GffN5hPR4JQBCEnsQ@public.gmane.org> wrote:> > > > Carl Shimer wrote: > > Hi, > > > > Is there a JSON serializer for prototype or is anyone working on one? I > am > > looking for something equivalent to MochiKit''s JSON support. The > serializer > > from json.org mucks with the prototype of object, array, etc so trips up > > anyone who writes code using the native iteration support, e.g for (var > foo > > in bar). > > You could ignore any property called "toJSONString", or more > generically any property whose value is a function. json.js messes > with all basic javascript objects - Array, Boolean, Date, Number, > Object and String. > > Prototype 1.4 messes with Object and Array, Prototype 1.5 messes with > Array, so your problems don''t end with fixing json.js. > > > -- > Fred > > > > >-- Ryan Gahl Application Development Consultant Athena Group, Inc. Inquire: 1-920-955-1457 Blog: http://www.someElement.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs -~----------~----~----~----~------~----~------~--~---
Rewriting this stuff isn''t hard but is more of a maintenance issue. anyway it looks like it easier to simply rewrite the code that breaks because of json.js with the addition of writing nastygrams to tell other devs how to do proper JS iteration :) On 10/31/06, Ryan Gahl <ryan.gahl-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > If MochiKit has your desired behavior, why not pull theirs in to your > project, or port theirs to the prototype model? > > json.js is quite good, and not at all very large. If you don''t like it''s > mucking with the prototypes as it does, why not just rewrite its functions > as static functions, where the first argument would be the instance you want > to serialize? That would take but 20 minutes or so of work tops. > > On 10/31/06, Fred <ozfred-AFFH1GffN5hPR4JQBCEnsQ@public.gmane.org> wrote: > > > > > > > > Carl Shimer wrote: > > > Hi, > > > > > > Is there a JSON serializer for prototype or is anyone working on > > one? I am > > > looking for something equivalent to MochiKit''s JSON support. The > > serializer > > > from json.org mucks with the prototype of object, array, etc so trips > > up > > > anyone who writes code using the native iteration support, e.g for > > (var foo > > > in bar). > > > > You could ignore any property called "toJSONString", or more > > generically any property whose value is a function. json.js messes > > with all basic javascript objects - Array, Boolean, Date, Number, > > Object and String. > > > > Prototype 1.4 messes with Object and Array, Prototype 1.5 messes with > > Array, so your problems don''t end with fixing json.js. > > > > > > -- > > Fred > > > > > > > > > > > > > -- > Ryan Gahl > Application Development Consultant > Athena Group, Inc. > Inquire: 1-920-955-1457 > Blog: http://www.someElement.com > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs -~----------~----~----~----~------~----~------~--~---
> > with the addition of writing nastygrams to tell other devs how to do > proper JS iteration :)Yes, great idea! :-) I''m on the fence actually. I don''t see too much use in treating an associative array (object) as a collection. If you need a robust dictionary or hash like collection, there are better ways IMHO. So I''ve never had issues with any libs "breaking" the (for var p in obj) method, as I''ve never thought of that as a great way to iterate (mind you, I do use it in a pinch, but am sure to filter things out if I have to). Arrays are much better suited, and it just gets my goat every time I see someone do something like declare an array and then try to iterate its properties as an associative array, rather than using indexes and the Array object''s special collection operation methods (like .push() for example). Anyway... </rant> --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs -~----------~----~----~----~------~----~------~--~---
Actually, unless I''m misunderstanding you, there is no maintenance issue. JSON has no version; it simply is. There will not be a JSON 2. With that constraint, you should be confident that any porting activity you do should be stable for a long time to come. On 31-Oct-06, at 8:14 PM, Carl Shimer wrote:> Rewriting this stuff isn''t hard but is more of a maintenance > issue. anyway it looks like it easier to simply rewrite the code > that breaks because of json.js with the addition of writing > nastygrams to tell other devs how to do proper JS iteration :) >Pete Forde: Partner at Unspace Interactive contact | pete-oWFMc0dWE16w5LPnMra/2Q@public.gmane.org - 416.548.8444 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs -~----------~----~----~----~------~----~------~--~---
Pete Forde ha scritto:> Actually, unless I''m misunderstanding you, there is no maintenance > issue. JSON has no version; it simply is. There will not be a JSON 2. > > With that constraint, you should be confident that any porting > activity you do should be stable for a long time to come. > > On 31-Oct-06, at 8:14 PM, Carl Shimer wrote: > >> Rewriting this stuff isn''t hard but is more of a maintenance issue. >> anyway it looks like it easier to simply rewrite the code that breaks >> because of json.js with the addition of writing nastygrams to tell >> other devs how to do proper JS iteration :) >>The current version of the json.js library published on json.org emulates a to be standardized syntax that once upon a time in the future will be eventually implemented by browser vendors. I still use a previous version what was implemented without native objects pollution but disappeared from json.org. Source code follows. --htrex; /* Copyright (c) 2005 JSON.org Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The Software shall be used for Good, not Evil. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* The global object JSON contains two methods. JSON.stringify(value) takes a JavaScript value and produces a JSON text. The value must not be cyclical. JSON.parse(text) takes a JSON text and produces a JavaScript value. It will throw a ''JSONError'' exception if there is an error. */ var JSON = { copyright: ''(c)2005 JSON.org'', license: ''http://www.crockford.com/JSON/license.html'', /* Stringify a JavaScript value, producing a JSON text. */ stringify: function (v) { var a = []; /* Emit a string. */ function e(s) { a[a.length] = s; } /* Convert a value. */ function g(x) { var c, i, l, v; switch (typeof x) { case ''object'': if (x) { if (x instanceof Array) { e(''[''); l = a.length; for (i = 0; i < x.length; i += 1) { v = x[i]; if (typeof v != ''undefined'' && typeof v != ''function'') { if (l < a.length) { e('',''); } g(v); } } e('']''); return; } else if (typeof x.valueOf == ''function'') { e(''{''); l = a.length; for (i in x) { v = x[i]; if (typeof v != ''undefined'' && typeof v != ''function'' && (!v || typeof v != ''object'' || typeof v.valueOf == ''function'')) { if (l < a.length) { e('',''); } g(i); e('':''); g(v); } } return e(''}''); } } e(''null''); return; case ''number'': e(isFinite(x) ? +x : ''null''); return; case ''string'': l = x.length; e(''"''); for (i = 0; i < l; i += 1) { c = x.charAt(i); if (c >= '' '') { if (c == ''\\'' || c == ''"'') { e(''\\''); } e(c); } else { switch (c) { case ''\b'': e(''\\b''); break; case ''\f'': e(''\\f''); break; case ''\n'': e(''\\n''); break; case ''\r'': e(''\\r''); break; case ''\t'': e(''\\t''); break; default: c = c.charCodeAt(); e(''\\u00'' + Math.floor(c / 16).toString(16) + (c % 16).toString(16)); } } } e(''"''); return; case ''boolean'': e(String(x)); return; default: e(''null''); return; } } g(v); return a.join(''''); }, /* Parse a JSON text, producing a JavaScript value. */ parse: function (text) { return (/^(\s+|[,:{}\[\]]|"(\\["\\\/bfnrtu]|[^\x00-\x1f"\\]+)*"|-?\d+(\.\d*)?([eE][+-]?\d+)?|true|false|null)+$/.test(text)) && eval(''('' + text + '')''); } }; --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---