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 issue 414. #414
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Apr 15, 2016
1 parent 34452dc commit da11393
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.alibaba.fastjson.parser.deserializer;

import java.lang.reflect.Type;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
Expand Down Expand Up @@ -97,15 +96,28 @@ protected <T> T castTimestamp(DefaultJSONParser parser, Type clazz, Object field
return null;
}

DateFormat dateFormat = parser.getDateFormat();
long longVal;
JSONScanner dateLexer = new JSONScanner(strVal);
try {
Date date = (Date) dateFormat.parse(strVal);
return (T) new Timestamp(date.getTime());
} catch (ParseException e) {
// skip
if (dateLexer.scanISO8601DateIfMatch()) {
longVal = dateLexer.getCalendar().getTimeInMillis();
} else {

DateFormat dateFormat = parser.getDateFormat();
try {
java.util.Date date = (java.util.Date) dateFormat.parse(strVal);
java.sql.Timestamp sqlDate = new java.sql.Timestamp(date.getTime());
return (T) sqlDate;
} catch (ParseException e) {
// skip
}

longVal = Long.parseLong(strVal);
}
} finally {
dateLexer.close();
}

long longVal = Long.parseLong(strVal);
return (T) new java.sql.Timestamp(longVal);
}

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

import java.sql.Timestamp;

import com.alibaba.fastjson.JSON;

import junit.framework.TestCase;

public class Bug_for_issue_414 extends TestCase {

public void test_for_issue() throws Exception {
String jsonStr = "{publishedDate:\"2015-09-07\"}";
TestEntity news = JSON.parseObject(jsonStr, TestEntity.class);
System.out.println(news.getPublishedDate());
}

public static class TestEntity {
private Timestamp publishedDate;

public Timestamp getPublishedDate() {
return publishedDate;
}
public void setPublishedDate(Timestamp publishedDate) {
this.publishedDate = publishedDate;
}
}
}

0 comments on commit da11393

Please sign in to comment.