Skip to content

Commit

Permalink
fix ignore cast exception, for issue #2665 (#2668)
Browse files Browse the repository at this point in the history
* fix ignore cast exception, for issue #2665
  • Loading branch information
yanxutao89 authored Jun 6, 2024
1 parent 82f1cfc commit 8ac4553
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public Object createInstance(Collection collection, long features) {
if (itemObjectReader == null) {
itemObjectReader = provider.getObjectReader(itemType);
}
value = itemObjectReader.createInstance((JSONObject) value, features);
value = itemObjectReader.createInstance((Map) value, features);
} else if (valueClass != itemType) {
Function typeConvert = provider.getTypeConvert(valueClass, itemType);
if (typeConvert != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,34 +95,21 @@ public Object createInstance(Map input, long features) {
if (valueObjectReader == null) {
valueObjectReader = provider.getObjectReader(valueType);
}
try {
value = valueObjectReader.createInstance((JSONObject) value, features);
} catch (Exception ignored) {
// ignored
}
value = valueObjectReader.createInstance((Map) value, features);
} else if ((valueClass == JSONArray.class || valueClass == CLASS_JSON_ARRAY_1x)
&& this.valueClass == List.class
) {
if (valueObjectReader == null) {
valueObjectReader = provider.getObjectReader(valueType);
}
try {
value = valueObjectReader.createInstance((JSONArray) value, features);
} catch (Exception ignored) {
// ignored
}
value = valueObjectReader.createInstance((List) value, features);
} else if ((typeConvert = provider.getTypeConvert(valueClass, valueType)) != null) {
value = typeConvert.apply(value);
} else if (value instanceof Map) {
Map map = (Map) value;
if (valueObjectReader == null) {
valueObjectReader = provider.getObjectReader(valueType);
}
try {
value = valueObjectReader.createInstance(map, features);
} catch (Exception ignored) {
// ignored
}
value = valueObjectReader.createInstance((Map) value, features);
} else if (value instanceof Collection && !multiValue) {
if (valueObjectReader == null) {
valueObjectReader = provider.getObjectReader(valueType);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.alibaba.fastjson.issues_compatible;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson2.JSON;
import org.junit.jupiter.api.Test;

import java.util.List;

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

public class Issue2665 {
@Test
public void test() {
String json = "[\r\n"
+ " {\r\n"
+ " \"date\": \"2024-06-10\",\r\n"
+ " \"slots\": [\r\n"
+ " {\r\n"
+ " \"createDate\": \"2024-06-10T11:54:58.240+01:00\",\r\n"
+ " \"expireDate\": \"2024-06-10T11:59:58.240+01:00\",\r\n"
+ " \"id\": 1176592662,\r\n"
+ " \"intervention\": {\r\n"
+ " \"code\": \"FS\",\r\n"
+ " \"name\": \"FA Simple\"\r\n"
+ " },\r\n"
+ " \"timeslot\": {\r\n"
+ " \"code\": \"AM\",\r\n"
+ " \"end\": \"13:00\",\r\n"
+ " \"name\": \"Morning\",\r\n"
+ " \"start\": \"08:00\"\r\n"
+ " }\r\n"
+ " }\r\n"
+ " ]\r\n"
+ " },\r\n"
+ " {\r\n"
+ " \"date\": \"2024-06-10\",\r\n"
+ " \"slots\": [\r\n"
+ " {\r\n"
+ " \"createDate\": \"2024-05-22T11:54:58.240+01:00\",\r\n"
+ " \"expireDate\": \"2024-05-22T11:59:58.240+01:00\",\r\n"
+ " \"id\": 1176592671,\r\n"
+ " \"intervention\": {\r\n"
+ " \"code\": \"FS\",\r\n"
+ " \"name\": \"FA Simple\"\r\n"
+ " },\r\n"
+ " \"timeslot\": {\r\n"
+ " \"code\": \"PM\",\r\n"
+ " \"end\": \"18:00\",\r\n"
+ " \"name\": \"Afternoon\",\r\n"
+ " \"start\": \"13:00\"\r\n"
+ " }\r\n"
+ " }\r\n"
+ " ]\r\n"
+ " }\r\n"
+ "]";

JSONArray jsonArr = JSONArray.parseArray(json);
List list = jsonArr.toJavaObject(List.class);
assertEquals(JSON.toJSONString(jsonArr), JSON.toJSONString(list));
}
}

0 comments on commit 8ac4553

Please sign in to comment.