From 46a5d693a379df194765d327dfbf4bff3436eae7 Mon Sep 17 00:00:00 2001 From: Cooper <12110517@mail.sustech.edu.cn> Date: Sun, 28 Jul 2024 22:13:06 +0800 Subject: [PATCH] fix JSONObject ClassCastException, for issue #2762 --- .../main/java/com/alibaba/fastjson/JSONObject.java | 4 ++++ .../com/alibaba/fastjson/v2issues/Issue2762.java | 14 ++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 fastjson1-compatible/src/test/java/com/alibaba/fastjson/v2issues/Issue2762.java diff --git a/fastjson1-compatible/src/main/java/com/alibaba/fastjson/JSONObject.java b/fastjson1-compatible/src/main/java/com/alibaba/fastjson/JSONObject.java index b0f17e26f7..f749e85a36 100755 --- a/fastjson1-compatible/src/main/java/com/alibaba/fastjson/JSONObject.java +++ b/fastjson1-compatible/src/main/java/com/alibaba/fastjson/JSONObject.java @@ -127,6 +127,10 @@ public Object get(Object key) { val = map.get(key.toString()); } + if (val instanceof com.alibaba.fastjson2.JSONObject) { + val = new com.alibaba.fastjson.JSONObject((com.alibaba.fastjson2.JSONObject) val); + } + return val; } diff --git a/fastjson1-compatible/src/test/java/com/alibaba/fastjson/v2issues/Issue2762.java b/fastjson1-compatible/src/test/java/com/alibaba/fastjson/v2issues/Issue2762.java new file mode 100644 index 0000000000..22881213e6 --- /dev/null +++ b/fastjson1-compatible/src/test/java/com/alibaba/fastjson/v2issues/Issue2762.java @@ -0,0 +1,14 @@ +package com.alibaba.fastjson.v2issues; + +import com.alibaba.fastjson.JSONObject; +import org.json.JSONException; +import org.junit.jupiter.api.Test; + +public class Issue2762 { + @Test + public void test() throws JSONException { + String jsonString = "{\"this0\":{\"$ref\":\"1\"}}"; + JSONObject jsonObject = JSONObject.parseObject(jsonString, JSONObject.class); + JSONObject innerObject1 = (JSONObject) jsonObject.get("this0"); + } +}