Ignore static fields in JSONObject.fromJson()

This commit is contained in:
Yuki Matsuhashi
2026-03-13 01:23:59 +09:00
parent 6230128f59
commit 94e340002b
3 changed files with 30 additions and 1 deletions
+4 -1
View File
@@ -3349,7 +3349,7 @@ public class JSONObject {
* of the given class. It supports basic data types including {@code int}, {@code double},
* {@code float}, {@code long}, and {@code boolean}, as well as their boxed counterparts.
* The target class must have a no-argument constructor, and its field names must match
* the keys in the JSON string.
* the keys in the JSON string. Static fields are ignored.
*
* <p><strong>Note:</strong> Only classes that are explicitly supported and registered within
* the {@code JSONObject} context can be deserialized. If the provided class is not among those,
@@ -3366,6 +3366,9 @@ public class JSONObject {
try {
T obj = clazz.getDeclaredConstructor().newInstance();
for (Field field : clazz.getDeclaredFields()) {
if (Modifier.isStatic(field.getModifiers())) {
continue;
}
field.setAccessible(true);
String fieldName = field.getName();
if (has(fieldName)) {