Conversation
|
have a review @stleary ! |
| } | ||
|
|
||
| // Exclude common bean/Object method names | ||
| if ("get".equals(methodName) || "is".equals(methodName) || "set".equals(methodName) |
There was a problem hiding this comment.
A static hard-coded Set with these values and then a call to contains would be way more performant, right? Since it would behave like a lookup table, and there are a lot of conditions.
There was a problem hiding this comment.
Great suggestion ! I have refactored the code to use a Set with contains() instead of multiple equals() calls
- Added EXCLUDED_RECORD_METHOD_NAMES Set containing all the excluded method names
- Made it unmodifiable using Collections.unmodifiableSet() for thread safety
- Updated isRecordStyleAccessor() to use Set.contains() instead of chained equals() calls
|
@Abhineshhh Please resync your branch to include the latest changes from #1006 |
a7eb236 to
f2acf8a
Compare
|
done @stleary |
|
@Abhineshhh This is a needed change, and the code looks fine in general, but there are some concerns:
|
|
|
Thank you for the feedback @stleary ! I've addressed both concerns: |
|
What problem does this code solve? Does the code still compile with Java6? Risks Changes to the API? Will this require a new release? Should the documentation be updated? Does it break the unit tests? Was any code refactored in this commit? Review status Starting 3-day comment window |



Fixes : #1015
Problem
new JSONObject(recordInstance)orJSONObject.wrap(recordInstance)produces an empty object{}when the object is a Java record (or any class using record-style accessors without@JSONPropertyNameannotations).Root Cause
Java records use accessor methods without the traditional JavaBean
get/isprefixes:getName()→ worksname()→ fails (returns{})The
getKeyNameFromMethod()logic only checked for methods starting withgetoris, causing it to ignore record accessors.Solution
Modified
getKeyNameFromMethod()inJSONObject.javato recognize record-style accessor methods:name(),age(),active())Object,Enum,Number, and alljava.*/javax.*classeshashCode,toString,equals, etc.)Changes
src/main/java/org/json/JSONObject.java- Added record accessor supportsrc/test/java/org/json/junit/data/PersonRecord.java- Test class mimicking record behaviorjsonObjectByRecord()inJSONObjectTest.javaTesting
All 735 existing tests pass
New test for record-style classes passes
No breaking changes - fully backward compatible
Example