stleary / JSON-java

A reference implementation of a JSON package in Java.
http://stleary.github.io/JSON-java/index.html
Other
4.54k stars 2.56k forks source link

Performance improvement for POJO conversion into JSONObject #844

Closed seppl831 closed 11 months ago

seppl831 commented 11 months ago

Hi,

when converting a POJO into a JSONObject we are experiencing performance degradations due to the annotation search in superclasses of the POJO.

The method getAnnotationDepth in JSONObject should not search methods in superclasses if the superclass is java.lang.Object. At the moment the search is stopped only if the superclass is null, i.e. the Object-class is searched also. This leads to recurring NoSuchMethodExceptions, which are catched but unnecessary.

A short performance test for a big HashMap (only POJOs) being converted to a JSONObject showed 900ms duration with the actual code. When avoiding the search in java.lang.Object the conversion of the same map takes only less than 70ms on the same system/environment.

stleary commented 11 months ago

Closing due to issue is addressed in an approved pull request.