Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/main/java/org/json/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -3349,7 +3349,7 @@ public static <T> T fromJson(String jsonString, Class<T> clazz) {
* 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,
Expand All @@ -3366,6 +3366,9 @@ public <T> T fromJson(Class<T> clazz) {
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)) {
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/org/json/junit/JSONObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import org.json.junit.data.CustomClassG;
import org.json.junit.data.CustomClassH;
import org.json.junit.data.CustomClassI;
import org.json.junit.data.CustomClassJ;
import org.json.JSONObject;
import org.junit.After;
import org.junit.Ignore;
Expand Down Expand Up @@ -4232,4 +4233,21 @@ public void jsonObjectParseFromJson_8() {
CustomClassI compareClassI = new CustomClassI(dataList);
assertEquals(customClassI.integerMap.toString(), compareClassI.integerMap.toString());
}

@Test
public void jsonObjectParseFromJson_9() {
JSONObject object = new JSONObject();
object.put("number", 12);
object.put("classState", "mutated");

String initialClassState = CustomClassJ.classState;
CustomClassJ.classState = "original";
try {
CustomClassJ customClassJ = object.fromJson(CustomClassJ.class);
assertEquals(12, customClassJ.number);
assertEquals("original", CustomClassJ.classState);
} finally {
CustomClassJ.classState = initialClassState;
}
}
}
10 changes: 10 additions & 0 deletions src/test/java/org/json/junit/data/CustomClassJ.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.json.junit.data;

public class CustomClassJ {
public static String classState = "original";
public int number;

public CustomClassJ() {
// Required for JSONObject#fromJson(Class<T>) tests.
}
}
Loading