Skip to content

Commit

Permalink
fix create error for empty EnumSet and EnumMap for issue #2423
Browse files Browse the repository at this point in the history
  • Loading branch information
yanxutao89 authored and wenshao committed Apr 12, 2024
1 parent deedf37 commit 5612e15
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,15 @@ public static ObjectReader of(Type type, Class listClass, long features) {
instanceClass = HashSet.class;
} else if (listClass == EnumSet.class) {
instanceClass = HashSet.class;
builder = (o) -> EnumSet.copyOf((Collection) o);
Type finalItemType = itemType;
builder = (o) -> {
Collection collection = (Collection) o;
if (collection.isEmpty() && finalItemType instanceof Class) {
return EnumSet.noneOf((Class) finalItemType);
} else {
return EnumSet.copyOf(collection);
}
};
} else if (listClass == NavigableSet.class || listClass == SortedSet.class) {
instanceClass = TreeSet.class;
} else if (listClass == CLASS_SINGLETON) {
Expand Down Expand Up @@ -473,7 +481,14 @@ public Object readJSONBObject(JSONReader jsonReader, Type fieldType, Object fiel
} else if (listType != null && EnumSet.class.isAssignableFrom(listType)) {
// maybe listType is java.util.RegularEnumSet or java.util.JumboEnumSet
list = new HashSet();
builder = (o) -> EnumSet.copyOf((Collection) o);
builder = (o) -> {
Collection collection = (Collection) o;
if (collection.isEmpty() && itemType instanceof Class) {
return EnumSet.noneOf((Class) itemType);
} else {
return EnumSet.copyOf(collection);
}
};
} else if (listType != null && listType != this.listType) {
try {
list = (Collection) listType.newInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ public Object readObject(JSONReader jsonReader, Type fieldType, Object fieldName
} else {
object = new HashMap<>();
}
} else if (instanceType == EnumMap.class && keyType instanceof Class) {
object = new EnumMap((Class) keyType);
} else {
object = (Map) createInstance(contextFeatures);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.alibaba.fastjson2.issues_2400;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import org.junit.jupiter.api.Test;

import java.util.EnumMap;
import java.util.EnumSet;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class Issue2423 {
@Test
void test() {
Bean bean = new Bean();

String jsonStr = com.alibaba.fastjson.JSON.toJSONString(bean);
Bean object = JSON.parseObject(jsonStr, Bean.class);
assertTrue(object.getSet().isEmpty());
assertTrue(object.getMap().isEmpty());
assertEquals(bean, object);

jsonStr = com.alibaba.fastjson2.JSON.toJSONString(bean);
object = com.alibaba.fastjson2.JSON.parseObject(jsonStr, Bean.class);
assertTrue(object.getSet().isEmpty());
assertTrue(object.getMap().isEmpty());
assertEquals(bean, object);
}

@Data
static class Bean {
private EnumSet<TimeUnit> set = EnumSet.noneOf(TimeUnit.class);
private EnumMap<TimeUnit, Integer> map = new EnumMap<>(TimeUnit.class);
}
}

0 comments on commit 5612e15

Please sign in to comment.