Repository: alibaba/fastjson2
Pull request #3239 (2025-01-01 03:24:58)

compatible api support non-string key, for issue #3214
### What this PR does / why we need it?



### Summary of your change



#### Please indicate you've done the following:

- [ ] Made sure tests are passing and test coverage is added if needed.
- [ ] Made sure commit message follow the rule of [Conventional Commits specification](https://www.conventionalcommits.org/).
- [ ] Considered the docs impact and opened a new docs issue or PR with docs changes if needed.



Patch:
diff --git a/core/src/main/java/com/alibaba/fastjson2/JSONReader.java b/core/src/main/java/com/alibaba/fastjson2/JSONReader.java
index 900ad14c9..da75aa902 100644
--- a/core/src/main/java/com/alibaba/fastjson2/JSONReader.java
+++ b/core/src/main/java/com/alibaba/fastjson2/JSONReader.java
@@ -2246,6 +2246,7 @@ public abstract class JSONReader
             throw new JSONException("level too large : " + level);
         }
 
+        Map innerMap = null;
         Map object;
         if (context.objectSupplier == null) {
             if ((context.features & Feature.UseNativeObject.mask) != 0) {
@@ -2255,6 +2256,7 @@ public abstract class JSONReader
             }
         } else {
             object = context.objectSupplier.get();
+            innerMap = TypeUtils.getInnerMap(object);
         }
 
         for (int i = 0; ; ++i) {
@@ -2354,7 +2356,12 @@ public abstract class JSONReader
                 continue;
             }
 
-            Object origin = object.put(name, val);
+            Object origin;
+            if (innerMap != null) {
+                origin = innerMap.put(name, val);
+            } else {
+                origin = object.put(name, val);
+            }
             if (origin != null) {
                 if ((context.features & Feature.DuplicateKeyValueAsArray.mask) != 0) {
                     if (origin instanceof Collection) {


Test patch:
diff --git a/core/src/test/java/com/alibaba/fastjson2/issues_3200/Issue3214.java b/core/src/test/java/com/alibaba/fastjson2/issues_3200/Issue3214.java
new file mode 100644
index 000000000..4b1f58dca
--- /dev/null
+++ b/core/src/test/java/com/alibaba/fastjson2/issues_3200/Issue3214.java
@@ -0,0 +1,43 @@
+package com.alibaba.fastjson2.issues_3200;
+
+import com.alibaba.fastjson2.JSON;
+import com.alibaba.fastjson2.JSONFactory;
+import com.alibaba.fastjson2.JSONObject;
+import com.alibaba.fastjson2.JSONReader;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+public class Issue3214 {
+    @Test
+    public void test() {
+        String json = "{\n"
+                + "  \"k1\":{\n"
+                + "    \"k2\":[\n"
+                + "      {\n"
+                + "        {} : {}\n"
+                + "      }\n"
+                + "    ]\n"
+                + "  }\n"
+                + "}";
+        JSONObject jsonObject = JSON.parseObject(json);
+        assertNotNull(jsonObject);
+    }
+
+    @Test
+    public void test1() {
+        JSONReader.Context context = JSONFactory.createReadContext();
+        context.setObjectSupplier(() -> new com.alibaba.fastjson.JSONObject());
+        String json = "{\n"
+                + "  \"k1\":{\n"
+                + "    \"k2\":[\n"
+                + "      {\n"
+                + "        {} : {}\n"
+                + "      }\n"
+                + "    ]\n"
+                + "  }\n"
+                + "}";
+        com.alibaba.fastjson.JSONObject jsonObject = (com.alibaba.fastjson.JSONObject) JSON.parse(json, context);
+        assertNotNull(jsonObject);
+    }
+}
diff --git a/fastjson1-compatible/src/test/java/com/alibaba/fastjson/v2issues/Issue3214.java b/fastjson1-compatible/src/test/java/com/alibaba/fastjson/v2issues/Issue3214.java
new file mode 100644
index 000000000..74b571ca5
--- /dev/null
+++ b/fastjson1-compatible/src/test/java/com/alibaba/fastjson/v2issues/Issue3214.java
@@ -0,0 +1,24 @@
+package com.alibaba.fastjson.v2issues;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+public class Issue3214 {
+    @Test
+    public void test() {
+        String json = "{\n"
+                + "  \"k1\":{\n"
+                + "    \"k2\":[\n"
+                + "      {\n"
+                + "        {} : {}\n"
+                + "      }\n"
+                + "    ]\n"
+                + "  }\n"
+                + "}";
+        JSONObject jsonObject = JSON.parseObject(json);
+        assertNotNull(jsonObject);
+    }
+}