1

I'm having preformance issues while comparing two JSONObjects. My current method of comparison is as followed:

return o1.toString().equals(o2.toString())

However, this is slow. I can't seem to find any other way to properly compare two JSONObjects though. o1.hashCode() and o2.hashCode() can be different, while the string is the same. Going through every element and comparing those individually seems tediously slow as well.

Is there any way to compare two JSONObjects faster?

This question is specifically about the use of org.json as supplied by http://json.org/. I really want to refrain from using any other libraries like the ones mentioned in Compare two JSON objects in Java, as this library is critical to some parts of the application.

Community
  • 1
  • 1
Daniël van den Berg
  • 2,197
  • 1
  • 20
  • 45
  • Shouldn't `o1.equals(o2)` work? – Thomas Weller Nov 19 '15 at 10:55
  • @Thomas Unfortunately not, it has not been overridden, and therefore the default `Object.equals` gets used. Therefore that'll always return false... – Daniël van den Berg Nov 19 '15 at 10:56
  • I am not sure about: "... tediously slow as well". Do you have code sample for this approach? – sibnick Nov 19 '15 at 10:59
  • Do the objects have some kind of ID present? – vguzzi Nov 19 '15 at 11:01
  • @vguzzi No they do not. – Daniël van den Berg Nov 19 '15 at 11:17
  • @sibnick I do not. I have looked into the possibility, but the problem is that this would have to be done recursively and different for every child. An object can contain objects, or even arrays. And eventually every object would have to be compared by string (unless someone comes up with a better way of doing it?), resulting in still having to compare (and construct) a lot of strings, which seems to be the cause of the slowdown. – Daniël van den Berg Nov 19 '15 at 11:17
  • 1
    Yes it is recursive, but you do not create any objects. You compare already existing objects. As I can see json.org has `JSONObjects.simular` method. You can compare performance between `toString` + `equals` and `JSONObjects.simular` – sibnick Nov 19 '15 at 11:31
  • @sibnick Thank you, I completely missed that one. If you'd like to add that as an answer I'll accept it. – Daniël van den Berg Nov 19 '15 at 12:03

1 Answers1

0

As I can see json.org has JSONObject.similar method. It is recursive, but you do not create any objects. You compare already existing objects. You can compare performance between toString + equals and JSONObject.similar

sibnick
  • 3,995
  • 20
  • 20
  • Do you have a link for the documentation of that method? Can't seem to find it [here](http://www.json.org/javadoc/org/json/JSONObject.html) or anywyere ): – Marcelo Nov 30 '15 at 11:50
  • 1
    I used JSONObject source code: https://github.com/douglascrockford/JSON-java/blob/master/JSONObject.java PS I fix wrong class name in my answer – sibnick Nov 30 '15 at 12:49
  • I know that is a year ago. But there is no `JSONObject.simular` method, instead, `JSONObject.similar` –  Jul 12 '16 at 15:27
  • Note that it is not yet released. Latest [maven] release is 20160810 and it does not have this method. – FabienB Dec 06 '16 at 13:44
  • Just so you know, this method has a (huge?) problem: `{"amount": 2}` compared against `{"amount": 2.0}` will return a "false". That's not how it should be. There are no `int`s in JSON; everything is a float. – John Red Mar 21 '17 at 07:46
  • 1
    there s no org.json.JSONObject.similar method. Maybe it was before but at the moment I write this comment there is none. – Daniel Feb 07 '20 at 13:03