mirror of
https://github.com/stleary/JSON-java.git
synced 2026-03-20 00:00:50 -04:00
Fix: Support Java record accessors in JSONObject
This commit is contained in:
@@ -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,
|
||||
|
||||
31
src/test/java/org/json/junit/data/PersonRecord.java
Normal file
31
src/test/java/org/json/junit/data/PersonRecord.java
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user