Skip to content

Commit a0be963

Browse files
rowstopwenshao
authored andcommitted
fix bug where collection being cast to List when readingReferences, for issue #2302
1 parent 23b1b9e commit a0be963

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

core/src/main/java/com/alibaba/fastjson2/JSONReader.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,10 @@ public int startArray() {
522522
public abstract String readReference();
523523

524524
public boolean readReference(List list, int i) {
525+
return readReference((Collection) list, i);
526+
}
527+
528+
public boolean readReference(Collection list, int i) {
525529
if (!isReference()) {
526530
return false;
527531
}

core/src/main/java/com/alibaba/fastjson2/JSONReaderJSONB.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1227,7 +1227,7 @@ public String readReference() {
12271227
throw new JSONException("reference not support input " + error(type));
12281228
}
12291229

1230-
public boolean readReference(List list, int i) {
1230+
public boolean readReference(Collection list, int i) {
12311231
if (bytes[offset] != BC_REFERENCE) {
12321232
return false;
12331233
}

core/src/main/java/com/alibaba/fastjson2/reader/FieldReaderList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public void readFieldValue(JSONReader jsonReader, T object) {
9797
break;
9898
}
9999

100-
if (jsonReader.readReference((List) list, i)) {
100+
if (jsonReader.readReference(list, i)) {
101101
continue;
102102
}
103103

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.alibaba.fastjson2.issues_2300;
2+
3+
import com.alibaba.fastjson2.JSON;
4+
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.util.HashSet;
8+
import java.util.Set;
9+
10+
/**
11+
* @author 张治保
12+
* @since 2024/3/8
13+
*/
14+
public class Issue2302 {
15+
@Test
16+
void test() {
17+
TestA a = new TestA();
18+
a.areaIds.add(1000);
19+
String jsonStr = JSON.toJSONString(a);
20+
TestA newTest = JSON.parseObject(jsonStr, TestA.class);
21+
Assertions.assertEquals(newTest.areaIds, a.areaIds);
22+
}
23+
24+
private static class TestA {
25+
public Set<Integer> areaIds = new HashSet<>();
26+
}
27+
}

0 commit comments

Comments
 (0)