From 05edc921752c40fa66e11c823f151b05c49bda52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E9=93=81?= Date: Thu, 8 Aug 2024 09:31:36 +0800 Subject: [PATCH] bug fix for issue #2851 --- .../reader/ObjectReaderImplList.java | 3 +++ .../fastjson2/issues_2800/Issue2851.java | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 core/src/test/java/com/alibaba/fastjson2/issues_2800/Issue2851.java diff --git a/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderImplList.java b/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderImplList.java index 001ddd564e..97d4c46039 100644 --- a/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderImplList.java +++ b/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderImplList.java @@ -174,6 +174,9 @@ public static ObjectReader of(Type type, Class listClass, long features) { instanceClass = TreeSet.class; builder = (Function) Collections::synchronizedNavigableSet; break; + case "java.util.AbstractList$RandomAccessSubList": + instanceClass = ArrayList.class; + break; default: instanceClass = listClass; } diff --git a/core/src/test/java/com/alibaba/fastjson2/issues_2800/Issue2851.java b/core/src/test/java/com/alibaba/fastjson2/issues_2800/Issue2851.java new file mode 100644 index 0000000000..2f57d2794c --- /dev/null +++ b/core/src/test/java/com/alibaba/fastjson2/issues_2800/Issue2851.java @@ -0,0 +1,22 @@ +package com.alibaba.fastjson2.issues_2800; + +import com.alibaba.fastjson2.*; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class Issue2851 { + @Test + public void test() { + List list = Arrays.asList("").subList(0, 1); + byte[] bytes = JSONB.toBytes(list, JSONWriter.Feature.WriteClassName); + JSONReader.AutoTypeBeforeHandler autoTypeFilter = JSONReader.autoTypeFilter("java.util.AbstractList"); + JSONReader.Context context = JSONFactory.createReadContext(); + context.config(autoTypeFilter); + List parsed = (List) JSONB.parse(bytes, context); + assertEquals(list.get(0), parsed.get(0)); + } +}