当前位置:网站首页>net.sf.json . jsonobject's format processing of time stamp

net.sf.json . jsonobject's format processing of time stamp

2020-11-08 16:58:47 Have you learned

1. problem

application A Apply B launch HTTP Request for data , Data contains date format attributes Time. application B In returning data to the application A Before the date Time It's formatted , Converted to a timestamp . hypothesis Time Before formatting ‘2020-11-08 00:22:35’, The corresponding timestamp is ‘1468905’. Discover applications A After receiving data Time The value of is really ‘1468905’, However, the application A After formatting the data, we found that Time But not ‘2020-11-08 00:22:35‘, It's the current time of the server !

2. screening

First of all, there is no error in converting date to timestamp , The problem is with the formatter ——net.sf.json.JSONObject.

adopt Debug Find out , application B When formatting the received data , For the properties Time The type received is Long, And the type of expectation is Date. The actual type does not match the expected type , Judge as true :

if (!targetType.isInstance(value)) {
setProperty(bean, key, morphPropertyValue(key, value, type, targetType), jsonConfig);
}

stay morphPropertyValue In the method, it is intended to use reflection from Long Type of Class Find date attribute , Then assign the timestamp to date......

//sourceBean: Namely Long The timestamp of the type 
//name: Try to find out date attribute
PropertyDescriptor sourcePd = PropertyUtils.getPropertyDescriptor(sourceBean, name);

The result is predictable , It can't be found . Finally, it returns the current time of the server ......

3. solve

Don't copy the code before shit ;

use FastJSON.

版权声明
本文为[Have you learned]所创,转载请带上原文链接,感谢