mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
修复DayOfWeek转json异常问题
This commit is contained in:
@@ -22,6 +22,9 @@ import cn.hutool.json.JSONUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.time.DayOfWeek;
|
||||
import java.time.MonthDay;
|
||||
import java.time.chrono.Era;
|
||||
import java.time.temporal.TemporalAccessor;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
@@ -262,6 +265,14 @@ public class JSONWriter extends Writer {
|
||||
} else if (value instanceof Number) {
|
||||
writeNumberValue((Number) value);
|
||||
} else if (value instanceof Date || value instanceof Calendar || value instanceof TemporalAccessor) {
|
||||
// issue#2572@Github
|
||||
if(value instanceof TemporalAccessor){
|
||||
if(value instanceof DayOfWeek || value instanceof java.time.Month || value instanceof Era || value instanceof MonthDay){
|
||||
writeStrValue(value.toString());
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
final String format = (null == config) ? null : config.getDateFormat();
|
||||
writeRaw(formatDate(value, format));
|
||||
} else if (value instanceof Boolean) {
|
||||
|
54
hutool-json/src/test/java/cn/hutool/json/Issue2572Test.java
Executable file
54
hutool-json/src/test/java/cn/hutool/json/Issue2572Test.java
Executable file
@@ -0,0 +1,54 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
import cn.hutool.core.lang.TypeReference;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.DayOfWeek;
|
||||
import java.time.Month;
|
||||
import java.time.MonthDay;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class Issue2572Test {
|
||||
|
||||
@Test
|
||||
public void putDayOfWeekTest(){
|
||||
final Set<DayOfWeek> weeks = new HashSet<>();
|
||||
weeks.add(DayOfWeek.MONDAY);
|
||||
final JSONObject obj = new JSONObject();
|
||||
obj.set("weeks", weeks);
|
||||
Assert.assertEquals("{\"weeks\":[\"MONDAY\"]}", obj.toString());
|
||||
|
||||
final Map<String, Set<DayOfWeek>> monthDays1 = obj.toBean(new TypeReference<Map<String, Set<DayOfWeek>>>() {
|
||||
});
|
||||
Assert.assertEquals("{weeks=[MONDAY]}", monthDays1.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void putMonthTest(){
|
||||
final Set<Month> months = new HashSet<>();
|
||||
months.add(Month.DECEMBER);
|
||||
final JSONObject obj = new JSONObject();
|
||||
obj.set("months", months);
|
||||
Assert.assertEquals("{\"months\":[\"DECEMBER\"]}", obj.toString());
|
||||
|
||||
final Map<String, Set<Month>> monthDays1 = obj.toBean(new TypeReference<Map<String, Set<Month>>>() {
|
||||
});
|
||||
Assert.assertEquals("{months=[DECEMBER]}", monthDays1.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void putMonthDayTest(){
|
||||
final Set<MonthDay> monthDays = new HashSet<>();
|
||||
monthDays.add(MonthDay.of(Month.DECEMBER, 1));
|
||||
final JSONObject obj = new JSONObject();
|
||||
obj.set("monthDays", monthDays);
|
||||
Assert.assertEquals("{\"monthDays\":[\"--12-01\"]}", obj.toString());
|
||||
|
||||
final Map<String, Set<MonthDay>> monthDays1 = obj.toBean(new TypeReference<Map<String, Set<MonthDay>>>() {
|
||||
});
|
||||
Assert.assertEquals("{monthDays=[--12-01]}", monthDays1.toString());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user