Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
bug fixed for support < 1970 date deserialize, for issue #1772
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Aug 4, 2018
1 parent b032cf5 commit cf5ba3a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/main/java/com/alibaba/fastjson/util/TypeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,12 @@ public static Date castToDate(Object value, String format){
} finally{
dateLexer.close();
}
if(strVal.startsWith("/Date(") && strVal.endsWith(")/")){

if (strVal.startsWith("/Date(") && strVal.endsWith(")/")) {
strVal = strVal.substring(6, strVal.length() - 2);
}
if(strVal.indexOf('-') != -1){

if (strVal.indexOf('-') > 0) {
if (format == null) {
if (strVal.length() == JSON.DEFFAULT_DATE_FORMAT.length()
|| (strVal.length() == 22 && JSON.DEFFAULT_DATE_FORMAT.equals("yyyyMMddHHmmssSSSZ"))) {
Expand Down Expand Up @@ -335,7 +337,8 @@ public static Date castToDate(Object value, String format){
}
longValue = Long.parseLong(strVal);
}
if(longValue < 0){

if (longValue == -1) {
Class<?> clazz = value.getClass();
if("oracle.sql.TIMESTAMP".equals(clazz.getName())){
if(oracleTimestampMethod == null && !oracleTimestampMethodInited){
Expand Down Expand Up @@ -373,8 +376,10 @@ public static Date castToDate(Object value, String format){
}
return (Date) result;
}

throw new JSONException("can not cast to Date, value : " + value);
}

return new Date(longValue);
}

Expand Down
27 changes: 27 additions & 0 deletions src/test/java/com/alibaba/json/bvt/issue_1700/Issue1772.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.alibaba.json.bvt.issue_1700;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import junit.framework.TestCase;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Issue1772 extends TestCase {
public void test_0() throws Exception {
Date date = JSON.parseObject("\"-14189155200000\"", Date.class);
assertEquals(-14189155200000L, date.getTime());
}

public void test_1() throws Exception {
JSONObject jsonObject = new JSONObject();
jsonObject.put("time", "-14189155200000");

Model m = jsonObject.toJavaObject(Model.class);
assertEquals(-14189155200000L, m.time.getTime());
}

public static class Model {
public Date time;
}
}

0 comments on commit cf5ba3a

Please sign in to comment.