From cd66e18114a70b06cdc8b3e39eaf6813de61fe61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E9=93=81?= Date: Sun, 11 Aug 2024 02:23:06 +0800 Subject: [PATCH] bug fix deserialize EnumMap, for issue #2859 --- .../reader/ObjectReaderProvider.java | 4 ++++ .../fastjson2/issues_2800/Issue2859.java | 21 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderProvider.java b/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderProvider.java index 27a85236d8..29f44bb181 100644 --- a/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderProvider.java +++ b/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderProvider.java @@ -824,6 +824,10 @@ private ObjectReader getObjectReaderInternal(Type objectType, boolean fieldBased if (typeArguments.length == 1 && ArrayList.class.isAssignableFrom(rawClass)) { return ObjectReaderImplList.of(objectType, rawClass, 0); } + + if (typeArguments.length == 2 && Map.class.isAssignableFrom(rawClass)) { + return ObjectReaderImplMap.of(objectType, (Class) rawType, 0); + } } } diff --git a/core/src/test/java/com/alibaba/fastjson2/issues_2800/Issue2859.java b/core/src/test/java/com/alibaba/fastjson2/issues_2800/Issue2859.java index d6fcdec996..29209c87c9 100644 --- a/core/src/test/java/com/alibaba/fastjson2/issues_2800/Issue2859.java +++ b/core/src/test/java/com/alibaba/fastjson2/issues_2800/Issue2859.java @@ -1,11 +1,17 @@ package com.alibaba.fastjson2.issues_2800; import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.TypeReference; import org.junit.jupiter.api.Test; +import java.lang.reflect.Type; import java.nio.charset.StandardCharsets; import java.time.LocalDate; import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.EnumMap; +import java.util.List; +import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -51,4 +57,19 @@ public void test1() { static class Bean1 { public LocalDate date; } + + @Test + public void testEnumMap() { + Map> map = new EnumMap<>(ApplicationType.class); + map.put(ApplicationType.AI, new ArrayList<>()); + String str = JSON.toJSONString(map); + System.out.println(str); + Type type = new TypeReference>>() {}.getType(); + Map> parsed = JSON.parseObject(str, type); + assertEquals(map.get(ApplicationType.AI), parsed.get(ApplicationType.AI)); + } + + public enum ApplicationType { + AI, Normal + } }