-
Notifications
You must be signed in to change notification settings - Fork 495
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 LocalDate JSONField format not work #380
- Loading branch information
Showing
4 changed files
with
131 additions
and
16 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
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
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
61 changes: 61 additions & 0 deletions
61
core/src/test/java/com/alibaba/fastjson2/issues/Issue380.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,61 @@ | ||
package com.alibaba.fastjson2.issues; | ||
|
||
import com.alibaba.fastjson2.JSON; | ||
import com.alibaba.fastjson2.annotation.JSONField; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.ZoneId; | ||
import java.time.ZonedDateTime; | ||
import java.util.Date; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class Issue380 { | ||
@Test | ||
public void test() { | ||
Bean bean = new Bean(); | ||
bean.date = LocalDate.of(2017, 6, 28); | ||
|
||
assertEquals("{\"date\":\"2017-06-28 00:00:00\"}", JSON.toJSONString(bean)); | ||
assertEquals("{\"date\":\"2017-06-28 00:00:00\"}", JSON.toJSONString(bean, "yyyy-MM-dd")); | ||
} | ||
|
||
public static class Bean { | ||
@JSONField(format = "yyyy-MM-dd HH:mm:ss") | ||
public LocalDate date; | ||
} | ||
|
||
@Test | ||
public void test1() { | ||
Bean1 bean = new Bean1(); | ||
bean.date = LocalDateTime.of(2017, 6, 28, 11, 12, 13); | ||
|
||
assertEquals("{\"date\":\"2017-06-28\"}", JSON.toJSONString(bean)); | ||
assertEquals("{\"date\":\"2017-06-28\"}", JSON.toJSONString(bean, "yyyy-MM-dd")); | ||
} | ||
|
||
public static class Bean1 { | ||
@JSONField(format = "yyyy-MM-dd") | ||
public LocalDateTime date; | ||
} | ||
|
||
@Test | ||
public void test2() { | ||
ZonedDateTime zdt = LocalDateTime.of(2017, 6, 28, 11, 12, 13) | ||
.atZone( | ||
ZoneId.of("Asia/Shanghai") | ||
); | ||
Bean2 bean = new Bean2(); | ||
bean.date = new Date(zdt.toInstant().toEpochMilli()); | ||
|
||
assertEquals("{\"date\":\"2017-06-28\"}", JSON.toJSONString(bean)); | ||
assertEquals("{\"date\":\"2017-06-28\"}", JSON.toJSONString(bean, "yyyy-MM-dd HH:mm:ss")); | ||
} | ||
|
||
public static class Bean2 { | ||
@JSONField(format = "yyyy-MM-dd") | ||
public java.util.Date date; | ||
} | ||
} |