Good Day to all, Does anyone know how to compare two json objects?? i tried the "===" operator but it still returns false.. thanks in advance.. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hey there, yagi a écrit :> Does anyone know how to compare two json objects?? i tried the "===" > operator but it still returns false..=== (or in this case, == for that matter) would return true only if you had the exact same object reference as both arguments. What you''re presumably alluding to here is a "deep equal", something that checks that both operands have the same property set (same names, same values for a given name). It''s not a trivial thing. Are you sure you need to do that? If so, here''s a shot at it (tested enough that I feel confident in posting it): Object.extend(Object, { deepEquals: function(o1, o2) { var k1 = Object.keys(o1).sort(); var k2 = Object.keys(o2).sort(); if (k1.length != k2.length) return false; return k1.zip(k2, function(keyPair) { return o1[keyPair[0]] == o2[keyPair[1]]; }).all(); } }); Usage: if (Object.deepEquals(anObj, anotherObj)) ... -- Christophe Porteneuve aka TDD tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Sorry to once again chime in without writing code. I know your implementation was off the cuff Christophe, but just thought I''d throw in that as written, it doesn''t support a truly deep comparison. You are assuming a JSON structure that is only 1 level deep. It needs a recursion step to handle both arrays and nested objects. No time for me to attempt to whip it up right now, but I''m sure you see what I mean... On 2/23/07, Christophe Porteneuve <tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org> wrote:> > > Hey there, > > yagi a écrit : > > Does anyone know how to compare two json objects?? i tried the "===" > > operator but it still returns false.. > > === (or in this case, == for that matter) would return true only if you > had the exact same object reference as both arguments. > > What you''re presumably alluding to here is a "deep equal", something > that checks that both operands have the same property set (same names, > same values for a given name). > > It''s not a trivial thing. Are you sure you need to do that? If so, > here''s a shot at it (tested enough that I feel confident in posting it): > > Object.extend(Object, { > deepEquals: function(o1, o2) { > var k1 = Object.keys(o1).sort(); > var k2 = Object.keys(o2).sort(); > if (k1.length != k2.length) return false; > return k1.zip(k2, function(keyPair) { > return o1[keyPair[0]] == o2[keyPair[1]]; > }).all(); > } > }); > > Usage: > > if (Object.deepEquals(anObj, anotherObj)) > ... > > -- > Christophe Porteneuve aka TDD > tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org > > > >-- 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?hl=en -~----------~----~----~----~------~----~------~--~---
Hey Ryan, Ryan Gahl a écrit :> assuming a JSON structure that is only 1 level deep. It needs a > recursion step to handle both arrays and nested objects. No time for me > to attempt to whip it up right now, but I''m sure you see what I mean...Indeed. The callback passed to zip would need to decide whether to == or to recurse based on the type/constructor. Something along these lines: Object.extend(Object, { _supportsEqual: function(o) { return null == o || ''number string boolean undefined''.include(typeof o); }, deepEquals: function(o1, o2) { var k1 = Object.keys(o1).sort(); var k2 = Object.keys(o2).sort(); if (k1.length != k2.length) return false; var isOk = Object_supportsEqual; return k1.zip(k2, function(keyPair) { return isOk(o1[keyPair[0]) || isOk(o2[keyPair[1]) ? o1[keyPair[0]] == o2[keyPair[1]] : Object.deepEquals(o1[keyPair[0]], o2[keyPair[1]]); }).all(); } }); I''m not too sure this handles cases where one prop supports == and the other does not, but I think, intuitively, that it does: in such a case, they would probably not be deemed equal anyway, and == would then fail, which seems good. Hence the || instead of the &&. -- Christophe Porteneuve aka TDD tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Feb 23, 6:46 pm, "yagi" <kennyag...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Good Day to all, > > Does anyone know how to compare two json objects?? i tried the "===" > operator but it still returns false..You could try just comparing their toString value, but that is likely very flakey. Christophe''s code seems very obfuscated to me (though I don''t doubt its effectiveness), try plain javascript - you might find it much simpler to understand. Below is a simple recursive funciton, it effectively tests ===, so null != undefined and 0 != ''0''. function testEquivalent(a, b) { var result = true; function typeTest(a, b) {return (typeof a == typeof b)} function test(a, b) { if (!typeTest(a, b)) return false; if (typeof a == ''function'' || typeof a == ''object'') { for (var p in a) { result = test(a[p], b[p]); if (!result) return false; } return result; } return (a == b); } return test(a, b); } // Some tests... var objA = { a:0, b:5, c:[1,2,3], d:''x''} var objB = { a:0, b:5, c:[1,2,3], d:''x''} var objC = { a:0, b:5, c:[1,2,null], d:''0''} var objD = { a:0, b:5, c:[1,2,undefined], d:0} alert( testEquivalent(objA, objB) ); // true alert( testEquivalent(objA, objC) ); // false alert( testEquivalent(objA, objD) ); // false alert( testEquivalent(objC, objD) ); // false -- Rob --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---