alibaba / fastjson2

🚄 FASTJSON2 is a Java JSON library with excellent performance.
Apache License 2.0
3.68k stars 478 forks source link

[BUG] JSONArray ClassCastException #2748

Open Cooper-Zhong opened 2 months ago

Cooper-Zhong commented 2 months ago

问题描述

fastjson兼容对于 #742: java.lang.ClassCastException: class com.alibaba.fastjson2.JSONArray cannot be cast to class com.alibaba.fastjson.JSONArray

环境信息

重现步骤

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

zhangbody commented 1 month ago

2.0.52 中 JSONPath.eval 在 return 时,会做 wrapper。这个 issue 可以 close 了。