-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Fix: Support Java record accessors in JSONObject #1020
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good work by the way :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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