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

Commit

Permalink
improved timestamp support
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Feb 29, 2020
1 parent dcc90ab commit 6e53cca
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.text.ParseException;
import java.util.Date;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.JSONScanner;
Expand Down Expand Up @@ -110,7 +111,12 @@ protected <T> T castTimestamp(DefaultJSONParser parser, Type clazz, Object field
if (dateLexer.scanISO8601DateIfMatch(false)) {
longVal = dateLexer.getCalendar().getTimeInMillis();
} else {

if (strVal.length() == 29) {
String dateFomartPattern = parser.getDateFomartPattern();
if (dateFomartPattern.length() != 29 && dateFomartPattern == JSON.DEFFAULT_DATE_FORMAT) {
return (T) java.sql.Timestamp.valueOf(strVal);
}
}
DateFormat dateFormat = parser.getDateFormat();
try {
java.util.Date date = (java.util.Date) dateFormat.parse(strVal);
Expand Down
22 changes: 21 additions & 1 deletion src/main/java/com/alibaba/fastjson/serializer/DateCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ public void write(JSONSerializer serializer, Object object, Object fieldName, Ty
return;
}
}

int nanos = 0;
if (clazz == java.sql.Timestamp.class) {
java.sql.Timestamp ts = (java.sql.Timestamp) object;
nanos = ts.getNanos();
}

Date date;
if (object instanceof Date) {
Expand Down Expand Up @@ -147,7 +153,17 @@ public void write(JSONSerializer serializer, Object object, Object fieldName, Ty
int millis = calendar.get(Calendar.MILLISECOND);

char[] buf;
if (millis != 0) {
if (nanos > 0) {
buf = "0000-00-00 00:00:00.000000000".toCharArray();
int nanoSize = IOUtils.stringSize(nanos);
IOUtils.getChars(nanos, 30 - (9 - nanoSize), buf);
IOUtils.getChars(second, 19, buf);
IOUtils.getChars(minute, 16, buf);
IOUtils.getChars(hour, 13, buf);
IOUtils.getChars(day, 10, buf);
IOUtils.getChars(month, 7, buf);
IOUtils.getChars(year, 4, buf);
} else if (millis != 0) {
buf = "0000-00-00T00:00:00.000".toCharArray();
IOUtils.getChars(millis, 23, buf);
IOUtils.getChars(second, 19, buf);
Expand Down Expand Up @@ -175,6 +191,10 @@ public void write(JSONSerializer serializer, Object object, Object fieldName, Ty
}

out.write(buf);
if (nanos > 0) { // java.sql.Timestamp
out.write(quote);
return;
}

float timeZoneF = calendar.getTimeZone().getOffset(calendar.getTimeInMillis()) / (3600.0f * 1000);
int timeZone = (int)timeZoneF;
Expand Down
40 changes: 40 additions & 0 deletions src/test/java/com/alibaba/json/bvt/SqlTimestampTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.alibaba.json.bvt;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import junit.framework.TestCase;
import org.junit.Assert;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.Locale;
import java.util.TimeZone;

public class SqlTimestampTest
extends TestCase {
protected void setUp() throws Exception {
JSON.defaultTimeZone = TimeZone.getTimeZone("Asia/Shanghai");
JSON.defaultLocale = new Locale("zh_CN");
}

public void test_date() throws Exception {
Timestamp ts = new Timestamp(
97,
2,
17,
15,
53,
01,
12345678
);

System.out.println('"' + ts.toString() + '"');

String json = JSON.toJSONString(ts, SerializerFeature.UseISO8601DateFormat);
assertEquals('"' + ts.toString() + '"', '"' + ts.toString() + '"');

Timestamp ts2 = JSON.parseObject(json, Timestamp.class);
String json2 = JSON.toJSONString(ts2, SerializerFeature.UseISO8601DateFormat);
assertEquals(json, json2);
}
}

0 comments on commit 6e53cca

Please sign in to comment.