Hi, D. Stuart Freeman schrieb: > OK, in that case it sounds like comparing the results of toString() > should suffice for my purposes.
No, this won't work - technically - since the order of properties in an object is *explicitly* defined as not being defined in the spec. In our implementation we cheated a bit and make the order defined as "insertion order" using a LinkedHashMap. If you really would want to compare JSONObjects, we would have to implement the equals method - probably in terms of the equals() method of the underlying Map used internally. Comparing toString is unreliable. > > On Fri, Jun 05, 2009 at 09:18:05PM +0200, Alexander Klimetschek wrote: >> I think they are not equivalent in the strict Javascript sense, as the >> order of the properties in an object is defined (you can iterate over >> it). It is not to be seen as a pure hash map. This is wrong. The spec explicitly states that there is no defined order amongst the properties of an object. Now, in reality, the known browsers implement objects in a way, that the properties have a factual insertion order. But, this is an implementation specific thing. Regards Felix >> >> Regards, >> Alex >> >> -- >> Alexander Klimetschek @iPhone >> >> >> Am 05.06.2009 um 21:00 schrieb "D. Stuart Freeman" >> <stuart.free...@et.gatech.edu>: >> >>> In the example I do both a toString() comparison and an Object >>> comparison >>> to show that it doesn't work. The problem is that in the current >>> setup >>> is as follows: >>> >>> JSONObject j = new JSONObject("{a:a, b:b}"); >>> JSONObject j = new JSONObject("{b:b, a:a}"); >>> >>> There's no easy way to find out these are functionally equivalent. >>> The >>> toString() methods of those objects will return them with the >>> properties >>> in that order and as such will not be equal. Calling .equals on the >>> JSONObjects won't work either because it calls Object.equals which is >>> false. >>> >>> On Fri, Jun 05, 2009 at 12:16:13PM -0500, John Crawford wrote: >>>> Well in you're example you used toString() before comparing. >>>> >>>> Sent from my iPhone >>>> >>>> On Jun 5, 2009, at 12:14 PM, "D. Stuart Freeman" >>>> <stuart.free...@et.gatech.edu> wrote: >>>> >>>>> I just realized this is because JSONObject doesn't override equals. >>>>> Maybe it should? >>>>> >>>>> On Fri, Jun 05, 2009 at 01:10:30PM -0400, D. Stuart Freeman wrote: >>>>>> Should it hold that if we have two JSONObjects, j and k, that if >>>>>> j.toString().equals(k.toString()) then j.equals(k)? I ask because >>>>>> I've >>>>>> found that it's possible to create JSONObjects where that isn't the >>>>>> case. >>>>>> Here's a quick, minimal example I worked up: >>>>>> http://pastebin.com/f12de25b9 >>>>>> >>>>>> -- >>>>>> D. Stuart Freeman >>>>>> Georgia Institute of Technology >>>>> >>>>> >>>>> -- >>>>> D. Stuart Freeman >>>>> Georgia Institute of Technology >>> -- >>> D. Stuart Freeman >>> Georgia Institute of Technology >