Skip to content

Commit

Permalink
bug fix deserialize EnumMap, for issue #2859
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Aug 10, 2024
1 parent d3f9b33 commit cd66e18
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -51,4 +57,19 @@ public void test1() {
static class Bean1 {
public LocalDate date;
}

@Test
public void testEnumMap() {
Map<ApplicationType, List<Bean>> map = new EnumMap<>(ApplicationType.class);
map.put(ApplicationType.AI, new ArrayList<>());
String str = JSON.toJSONString(map);
System.out.println(str);
Type type = new TypeReference<EnumMap<ApplicationType, List<Bean>>>() {}.getType();
Map<ApplicationType, List<Bean>> parsed = JSON.parseObject(str, type);
assertEquals(map.get(ApplicationType.AI), parsed.get(ApplicationType.AI));
}

public enum ApplicationType {
AI, Normal
}
}

0 comments on commit cd66e18

Please sign in to comment.