From e07d013d685e1bdc7a7544730f3f4238d11b1a37 Mon Sep 17 00:00:00 2001 From: wenshao Date: Sat, 2 Nov 2024 11:08:57 +0800 Subject: [PATCH] fix parse private class error, for issue #3134 --- .../reader/ObjectReaderCreatorASM.java | 4 +- .../fastjson2/issues_3100/Issue3134.java | 37 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 core/src/test/java/com/alibaba/fastjson2/issues_3100/Issue3134.java diff --git a/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderCreatorASM.java b/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderCreatorASM.java index ee988a06c7..8d93714d39 100644 --- a/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderCreatorASM.java +++ b/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderCreatorASM.java @@ -3032,7 +3032,9 @@ private int genReadFieldValue( if (list) { Class itemClass = TypeUtils.getMapping(itemType); - if (itemClass != null && Collection.class.isAssignableFrom(itemClass)) { + if (itemClass != null + && (Collection.class.isAssignableFrom(itemClass) || !Modifier.isPublic(itemClass.getModifiers())) + ) { list = false; } } diff --git a/core/src/test/java/com/alibaba/fastjson2/issues_3100/Issue3134.java b/core/src/test/java/com/alibaba/fastjson2/issues_3100/Issue3134.java new file mode 100644 index 0000000000..dc44ce5078 --- /dev/null +++ b/core/src/test/java/com/alibaba/fastjson2/issues_3100/Issue3134.java @@ -0,0 +1,37 @@ +package com.alibaba.fastjson2.issues_3100; + +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONArray; +import com.alibaba.fastjson2.JSONObject; +import lombok.Data; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class Issue3134 { + @Test + public void test() { + String json = "{\"name\":\"希望小学\",\"students\":{\"age\":\"12\",\"id\":\"1\",\"name\":\"张宇\"}}"; + School schoolOne = JSON.parseObject(json, School.class); + JSONObject schoolTwo = JSON.parseObject(json); + School schoolThree = schoolTwo.toJavaObject(School.class); + JSONObject studentOne = schoolTwo.getJSONObject("students"); + JSONArray studentTwo = schoolTwo.getJSONArray("students"); + assertEquals("[{\"age\":\"12\",\"id\":\"1\",\"name\":\"张宇\"}]", studentTwo.toString()); + } + + @Data + private static class School{ + private String name; + private List students; + } + + @Data + private static class Student{ + private String name; + private String id; + private String age; + } +}