Skip to content

Commit

Permalink
improved datacodec.
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Aug 16, 2017
1 parent 9f78d03 commit 3e89092
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 17 deletions.
58 changes: 41 additions & 17 deletions src/main/java/com/alibaba/fastjson/serializer/DateCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
Expand Down Expand Up @@ -164,19 +165,21 @@ protected <T> T cast(DefaultJSONParser parser, Type clazz, Object fieldName, Obj
return null;
}

JSONScanner dateLexer = new JSONScanner(strVal);
try {
if (dateLexer.scanISO8601DateIfMatch(false)) {
Calendar calendar = dateLexer.getCalendar();

if (clazz == Calendar.class) {
return (T) calendar;
{
JSONScanner dateLexer = new JSONScanner(strVal);
try {
if (dateLexer.scanISO8601DateIfMatch(false)) {
Calendar calendar = dateLexer.getCalendar();

if (clazz == Calendar.class) {
return (T) calendar;
}

return (T) calendar.getTime();
}
return (T) calendar.getTime();
} finally {
dateLexer.close();
}
} finally {
dateLexer.close();
}

if (strVal.length() == parser.getDateFomartPattern().length()) {
Expand All @@ -192,18 +195,39 @@ protected <T> T cast(DefaultJSONParser parser, Type clazz, Object fieldName, Obj
String dotnetDateStr = strVal.substring(6, strVal.length() - 2);
strVal = dotnetDateStr;
}

// JSONScanner iso8601Lexer = new JSONScanner(strVal);
// if (iso8601Lexer.scanISO8601DateIfMatch()) {
// val = iso8601Lexer.getCalendar().getTime();
// }
// iso8601Lexer.close();

if ("0000-00-00".equals(strVal)
|| "0000-00-00T00:00:00".equalsIgnoreCase(strVal)
|| "0001-01-01T00:00:00+08:00".equalsIgnoreCase(strVal)) {
return null;
}

int index = strVal.lastIndexOf('|');
if (index > 20) {
String tzStr = strVal.substring(index + 1);
TimeZone timeZone = TimeZone.getTimeZone(tzStr);
if (!"GMT".equals(timeZone.getID())) {
String subStr = strVal.substring(0, index);
JSONScanner dateLexer = new JSONScanner(subStr);
try {
if (dateLexer.scanISO8601DateIfMatch(false)) {
Calendar calendar = dateLexer.getCalendar();

calendar.setTimeZone(timeZone);

if (clazz == Calendar.class) {
return (T) calendar;
}

return (T) calendar.getTime();
}
} finally {
dateLexer.close();
}
}
}

// 2017-08-14 19:05:30.000|America/Los_Angeles
//
long longVal = Long.parseLong(strVal);
return (T) new java.util.Date(longVal);
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/com/alibaba/json/bvt/date/DateFieldTest10.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

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

/**
* Created by wenshao on 07/04/2017.
Expand All @@ -18,6 +19,14 @@ public void test_for_zero() throws Exception {
JSON.parseObject(text, Model.class);
}

public void test_1() throws Exception {
String text = "{\"date\":\"2017-08-14 19:05:30.000|America/Los_Angeles\"}";

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-DD");
Object object = format.parse("0000-00-00");
JSON.parseObject(text, Model.class);
}

public static class Model {
public Date date;
}
Expand Down

0 comments on commit 3e89092

Please sign in to comment.