fastjson2
fastjson2 copied to clipboard
[BUG] JSONArray ClassCastException
问题描述
fastjson兼容对于 #742: java.lang.ClassCastException: class com.alibaba.fastjson2.JSONArray cannot be cast to class com.alibaba.fastjson.JSONArray
环境信息
- OS信息: [MacOS 12.7.4 M1 Pro 16 GB]
- JDK信息: [Openjdk 17.0.6]
- 版本信息:[Fastjson 2.0.51]
重现步骤
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONPath;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class MutatedIssue742_1021 {
@Test
public void mutatedTest() {
User user = JSON.parseObject(str, User.class);
JSONArray result = (JSONArray) JSONPath.eval(user, "$.phoneNumbers[?(@.address.city == 'Nara1')]");
assertEquals(1, result.size());
}
public static class User {
public String firstName;
public String lastName;
public int age;
public List<PhoneNumber> phoneNumbers;
}
public static class Address {
public String streetAddress;
public String city;
public String postalCode;
}
public static class PhoneNumber {
public String type;
public String number;
public Address address;
}
static String str = "{ \"firstName\": \"John\", \"lastName\" : \"doe\", \"age\" : 26, \"address\" : { \"streetAddress\": \"naist street\", \"city\" : \"Nara\", \"postalCode\" : \"630-0192\" }, \"phoneNumbers\": [ { \"type\" : \"iPhone\", \"number\": \"0123-4567-8888\", \"address\" : { \"streetAddress\": \"naist street1\", \"city\" : \"Nara1\", \"postalCode\" : \"630-0192\" } }, { \"type\" : \"home\", \"number\": \"0123-4567-8910\", \"address\" : { \"streetAddress\": \"naist street2\", \"city\" : \"Nara2\", \"postalCode\" : \"630-0192\" } } ] }";
}
相关日志输出
java.lang.ClassCastException: class com.alibaba.fastjson2.JSONArray cannot be cast to class com.alibaba.fastjson.JSONArray