Skip to content

Commit

Permalink
deserialize support EnumMap, for issue alibaba#1679
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jul 24, 2023
1 parent 1ddd9ba commit 2a67a01
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ public static ObjectReader of(Type fieldType, Class mapType, long features) {
Map.Entry entry = (Map.Entry) map.entrySet().iterator().next();
return Collections.singletonMap(entry.getKey(), entry.getValue());
};
} else if (mapType == EnumMap.class) {
instanceType = LinkedHashMap.class;
builder = e -> new EnumMap((Map) e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.alibaba.fastjson2.issues_1600;

import com.alibaba.fastjson2.JSONB;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
import org.junit.jupiter.api.Test;

import java.util.EnumMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class Issue1679 {
@Test
public void test() {
Map<TimeUnit, String> map = new LinkedHashMap<>();
map.put(TimeUnit.DAYS, "day");
EnumMap<TimeUnit, String> enumMap = new EnumMap(map);

byte[] jsonbBytes = JSONB.toBytes(enumMap, JSONWriter.Feature.WriteClassName);
EnumMap enumMap1 = JSONB.parseObject(jsonbBytes, EnumMap.class, JSONReader.Feature.SupportAutoType);
assertEquals(enumMap.get(TimeUnit.DAYS), enumMap1.get(TimeUnit.DAYS));
}
}

0 comments on commit 2a67a01

Please sign in to comment.