forked from alibaba/fastjson2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug fix for java.sql.Timestamp codec, fix alibaba#164
- Loading branch information
Showing
2 changed files
with
117 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
core/src/test/java/com/alibaba/fastjson2/date/SqlTimestampTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.alibaba.fastjson2.date; | ||
|
||
import com.alibaba.fastjson2.JSON; | ||
import com.alibaba.fastjson2.JSONWriter; | ||
import com.alibaba.fastjson2.annotation.JSONField; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.ZonedDateTime; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class SqlTimestampTest { | ||
@Test | ||
public void test() { | ||
String str = "{\"birthday\":\"2022-05-03 15:26:05\"}"; | ||
Student student = JSON.parseObject(str, Student.class); | ||
assertEquals(str, JSON.toJSONString(student)); | ||
} | ||
|
||
public static class Student { | ||
@JSONField(format = "yyyy-MM-dd HH:mm:ss") | ||
public java.sql.Timestamp birthday; | ||
} | ||
|
||
@Test | ||
public void test1() { | ||
String str = "{\"birthday\":\"2022-05-03T15:26:05\"}"; | ||
Student1 student = JSON.parseObject(str, Student1.class); | ||
String str2 = JSON.toJSONString(student); | ||
Student1 student1 = JSON.parseObject(str2, Student1.class); | ||
assertEquals(student.birthday.getYear(), student1.birthday.getYear()); | ||
assertEquals(student.birthday.toInstant().toEpochMilli(), student1.birthday.toInstant().toEpochMilli()); | ||
} | ||
|
||
@Test | ||
public void test1_null() { | ||
String str = "{\"birthday\":null}"; | ||
Student1 student = JSON.parseObject(str, Student1.class); | ||
assertEquals(str, JSON.toJSONString(student, JSONWriter.Feature.WriteNulls)); | ||
} | ||
|
||
public static class Student1 { | ||
@JSONField(format = "iso8601") | ||
public java.sql.Timestamp birthday; | ||
} | ||
|
||
@Test | ||
public void test2() { | ||
String str = "{\"birthday\":\"2022-05-03 15:26:05\"}"; | ||
Student2 student = JSON.parseObject(str, Student2.class); | ||
assertEquals(str, JSON.toJSONString(student, "yyyy-MM-dd HH:mm:ss")); | ||
} | ||
|
||
public static class Student2 { | ||
public java.sql.Timestamp birthday; | ||
} | ||
|
||
|
||
@Test | ||
public void test3() { | ||
long millis = System.currentTimeMillis(); | ||
String str = "{\"birthday\":" + millis + "}"; | ||
Student3 student = JSON.parseObject(str, Student3.class); | ||
String str2 = JSON.toJSONString(student); | ||
Student3 student1 = JSON.parseObject(str2, Student3.class); | ||
assertEquals(student.birthday.toInstant().getEpochSecond(), student1.birthday.toInstant().getEpochSecond()); | ||
} | ||
|
||
public static class Student3 { | ||
@JSONField(format = "millis") | ||
public java.sql.Timestamp birthday; | ||
} | ||
|
||
@Test | ||
public void test4() { | ||
long seconds = System.currentTimeMillis() / 1000; | ||
String str = "{\"birthday\":" + seconds + "}"; | ||
Student4 student = JSON.parseObject(str, Student4.class); | ||
String str2 = JSON.toJSONString(student); | ||
Student4 student1 = JSON.parseObject(str2, Student4.class); | ||
assertEquals(student.birthday.toInstant().getEpochSecond() | ||
, student1.birthday.toInstant().getEpochSecond() | ||
); | ||
} | ||
|
||
public static class Student4 { | ||
@JSONField(format = "unixtime") | ||
public java.sql.Timestamp birthday; | ||
} | ||
} |