Fastjson's questions about date types

problem description

encountered the conversion problem of fastjson in the project, when converting Java Bean to JSONObject. Fastjson converts java.util.Date to a timestamp

when converted to a json object,
uses an exception
long birthday= (long) jsonObject.get ("birthday") to get the date object in it;
all tested dates with date-time stamps between the minimum and maximum values of int will report an error:

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long

so I have a question here: after fastJson is converted into an object, what type of object is obtained here using json.get ("date"), and why there is this bug

related versions

jdk version: 1.7
fastjson version: 1.2.32

related codes

// 
public class Student{
    private Date birthday;
    
    // getter,setter...
}

// 
public static void main(String[] args) {
    Student student = new TestJson().new Student();
    // 
    student.setBirthday(new Date());
    // 1970-01-01 llong
    student.setBirthday(new Date(-28800000));
    // 1970-01-02 llong
    student.setBirthday(new Date(57600000));
    // 2018-07-10 int,l
    student.setBirthday(new Date(1531152000000l));
    // 1960-07-10 intl
    student.setBirthday(new Date(-299145600000l));
    
    JSONObject fastJson = JSONObject.parseObject(JSONObject.toJSONString(student));
    // int
    long birthday = (long) fastJson.get("birthday");
    
    // ,,
    //long birthday = (long) fastJson.getInteger("birthday");
    //long birthday = (long) fastJson.getIntValue("birthday");
    //long birthday = fastJson.getLongValue("birthday");
    //long birthday = fastJson.getLong("birthday");
    
    System.out.println(birthday);        
}
< H2 > final summary < / H2 >

at present, due to the existence of birthday timestamps in the system, the birthday timestamp is within the range of the minimum and maximum values of int. So the above exception occurs. Long birthday = fastJson.getLongValue ("birthday") has been used; instead of the original method

but I still don"t know why there is an exception, so please let me know if you know. Or point out the key implementation location of the source code of fastjson

Mar.25,2021

 // 1970-01-01 llong
    student.setBirthday(new Date(-28800000));
    // 1970-01-02 llong
    student.setBirthday(new Date(57600000));

the reason for these two errors is that the type of value in HashMap's static inner class Node stores Integer type, forcefully long reports class conversion error, and the other three stores long type, so no error will be reported.
long birthday = fastJson.getLongValue ("birthday"); the reason for not reporting an error is that Integer has been converted to long.


timestamp has always been long,. If Int is already dead, but long is not enough from the current point of view, I should be able to wait until that day in my lifetime

.
Menu