Fix: Support Java record accessors in JSONObject

This commit is contained in:
AbhineshJha
2025-10-25 14:16:55 +05:30
parent 25f355a953
commit 20f5200000
3 changed files with 79 additions and 1 deletions

View File

@@ -51,6 +51,7 @@ import org.json.junit.data.MyJsonString;
import org.json.junit.data.MyNumber;
import org.json.junit.data.MyNumberContainer;
import org.json.junit.data.MyPublicClass;
import org.json.junit.data.PersonRecord;
import org.json.junit.data.RecursiveBean;
import org.json.junit.data.RecursiveBeanEquals;
import org.json.junit.data.Singleton;
@@ -796,6 +797,25 @@ public class JSONObjectTest {
Util.checkJSONObjectMaps(jsonObject);
}
/**
* JSONObject built from a Java record.
* Records use accessor methods without get/is prefixes (e.g., name() instead of getName()).
* This test verifies that JSONObject correctly handles record types.
*/
@Test
public void jsonObjectByRecord() {
PersonRecord person = new PersonRecord("John Doe", 30, true);
JSONObject jsonObject = new JSONObject(person);
// validate JSON
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
assertTrue("expected 3 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 3);
assertTrue("expected name field", "John Doe".equals(jsonObject.query("/name")));
assertTrue("expected age field", Integer.valueOf(30).equals(jsonObject.query("/age")));
assertTrue("expected active field", Boolean.TRUE.equals(jsonObject.query("/active")));
Util.checkJSONObjectMaps(jsonObject);
}
/**
* A bean is also an object. But in order to test the JSONObject
* ctor that takes an object and a list of names,

View File

@@ -0,0 +1,31 @@
package org.json.junit.data;
/**
* A test class that mimics Java record accessor patterns.
* Records use accessor methods without get/is prefixes (e.g., name() instead of getName()).
* This class simulates that behavior to test JSONObject's handling of such methods.
*/
public class PersonRecord {
private final String name;
private final int age;
private final boolean active;
public PersonRecord(String name, int age, boolean active) {
this.name = name;
this.age = age;
this.active = active;
}
// Record-style accessors (no "get" or "is" prefix)
public String name() {
return name;
}
public int age() {
return age;
}
public boolean active() {
return active;
}
}