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