Skip to content

JSONObject does not work with records without field annotations #1015

Closed

Description

@MarcoMartins86

new JSONObject(object) or JSONObject.wrap(object) does not work if the object is a record without @JSONPropertyName on its fields.

The flow eventually reaches out to JSONObject.getKeyNameFromMethod(Method method), and looking into the version 20250517 code, we can see that:

JSONPropertyName annotation = getAnnotation(method, JSONPropertyName.class);
if (annotation != null && annotation.value() != null && !annotation.value().isEmpty()) {
    return annotation.value();
}
String key;
final String name = method.getName();
if (name.startsWith("get") && name.length() > 3) {
    key = name.substring(3);
} else if (name.startsWith("is") && name.length() > 2) {
    key = name.substring(2);
} else {
    return null;
}

If the field is not annotated with @JSONPropertyName and if it doesn't start with get or is, then it will return null.
Since records don't use prefixes on getters, it will always be represented as {} an empty object.

Activity

  1. stleary commented on Oct 17, 2025

    @stleary
    Owner

    @MarcoMartins86 Thanks for bringing up this issue, looks like something that should be fixed.

  2. Madjid-Ghazi commented on Oct 21, 2025

    @Madjid-Ghazi

    We have just hit the same problem, so its def an issue.
    Thanks for the OP for raising the ticket and also for the work to fix it.

  3. Abhineshhh commented on Oct 25, 2025

    @Abhineshhh
    Contributor

    can i work on it ? @stleary

  4. stleary commented on Oct 25, 2025

    @stleary
    Owner
  5. stleary commented on Nov 12, 2025

    @stleary
    Owner

    Closing this issue; a fix was added in #1020. If there are any questions or issues with the feature, please open a new ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions