mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
改进TemporalAccessorSerializer支持dayOfMonth和month枚举名
This commit is contained in:
parent
01cb9bdd26
commit
6d33c3f413
@ -5,6 +5,7 @@
|
|||||||
# 5.8.23(2023-09-21)
|
# 5.8.23(2023-09-21)
|
||||||
|
|
||||||
### 🐣新特性
|
### 🐣新特性
|
||||||
|
* 【json 】 改进TemporalAccessorSerializer支持dayOfMonth和month枚举名(issue#I82AM8@Gitee)
|
||||||
|
|
||||||
### 🐞Bug修复
|
### 🐞Bug修复
|
||||||
* 【cron 】 修复Cron表达式range解析错误问题(issue#I82CSH@Gitee)
|
* 【cron 】 修复Cron表达式range解析错误问题(issue#I82CSH@Gitee)
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package cn.hutool.json.serialize;
|
package cn.hutool.json.serialize;
|
||||||
|
|
||||||
|
import cn.hutool.core.lang.Assert;
|
||||||
import cn.hutool.json.JSON;
|
import cn.hutool.json.JSON;
|
||||||
import cn.hutool.json.JSONException;
|
import cn.hutool.json.JSONException;
|
||||||
import cn.hutool.json.JSONObject;
|
import cn.hutool.json.JSONObject;
|
||||||
@ -7,6 +8,7 @@ import cn.hutool.json.JSONObject;
|
|||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.LocalTime;
|
import java.time.LocalTime;
|
||||||
|
import java.time.Month;
|
||||||
import java.time.temporal.TemporalAccessor;
|
import java.time.temporal.TemporalAccessor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -61,11 +63,33 @@ public class TemporalAccessorSerializer implements JSONObjectSerializer<Temporal
|
|||||||
@Override
|
@Override
|
||||||
public TemporalAccessor deserialize(JSON json) {
|
public TemporalAccessor deserialize(JSON json) {
|
||||||
final JSONObject jsonObject = (JSONObject) json;
|
final JSONObject jsonObject = (JSONObject) json;
|
||||||
if (LocalDate.class.equals(this.temporalAccessorClass)) {
|
if (LocalDate.class.equals(this.temporalAccessorClass) || LocalDateTime.class.equals(this.temporalAccessorClass)) {
|
||||||
return LocalDate.of(jsonObject.getInt(YEAR_KEY), jsonObject.getInt(MONTH_KEY), jsonObject.getInt(DAY_KEY));
|
final Integer year = jsonObject.getInt(YEAR_KEY);
|
||||||
} else if (LocalDateTime.class.equals(this.temporalAccessorClass)) {
|
Assert.notNull(year, "Field 'year' must be not null");
|
||||||
return LocalDateTime.of(jsonObject.getInt(YEAR_KEY), jsonObject.getInt(MONTH_KEY), jsonObject.getInt(DAY_KEY),
|
Integer month = jsonObject.getInt(MONTH_KEY);
|
||||||
jsonObject.getInt(HOUR_KEY), jsonObject.getInt(MINUTE_KEY), jsonObject.getInt(SECOND_KEY), jsonObject.getInt(NANO_KEY));
|
if (null == month) {
|
||||||
|
final Month monthEnum = Month.valueOf(jsonObject.getStr(MONTH_KEY));
|
||||||
|
Assert.notNull(monthEnum, "Field 'month' must be not null");
|
||||||
|
month = monthEnum.getValue();
|
||||||
|
}
|
||||||
|
Integer day = jsonObject.getInt(DAY_KEY);
|
||||||
|
if (null == day) {
|
||||||
|
day = jsonObject.getInt("dayOfMonth");
|
||||||
|
Assert.notNull(day, "Field 'day' or 'dayOfMonth' must be not null");
|
||||||
|
}
|
||||||
|
|
||||||
|
final LocalDate localDate = LocalDate.of(year, month, day);
|
||||||
|
if (LocalDate.class.equals(this.temporalAccessorClass)) {
|
||||||
|
return localDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
final LocalTime localTime = LocalTime.of(
|
||||||
|
jsonObject.getInt(HOUR_KEY, 0),
|
||||||
|
jsonObject.getInt(MINUTE_KEY, 0),
|
||||||
|
jsonObject.getInt(SECOND_KEY, 0),
|
||||||
|
jsonObject.getInt(NANO_KEY, 0));
|
||||||
|
|
||||||
|
return LocalDateTime.of(localDate, localTime);
|
||||||
} else if (LocalTime.class.equals(this.temporalAccessorClass)) {
|
} else if (LocalTime.class.equals(this.temporalAccessorClass)) {
|
||||||
return LocalTime.of(jsonObject.getInt(HOUR_KEY), jsonObject.getInt(MINUTE_KEY), jsonObject.getInt(SECOND_KEY), jsonObject.getInt(NANO_KEY));
|
return LocalTime.of(jsonObject.getInt(HOUR_KEY), jsonObject.getInt(MINUTE_KEY), jsonObject.getInt(SECOND_KEY), jsonObject.getInt(NANO_KEY));
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,43 @@
|
|||||||
|
package cn.hutool.json;
|
||||||
|
|
||||||
|
import cn.hutool.core.io.resource.ResourceUtil;
|
||||||
|
import cn.hutool.core.lang.TypeReference;
|
||||||
|
import lombok.Data;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class IssueI82AM8Test {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void toBeanTest() {
|
||||||
|
final String json = ResourceUtil.readUtf8Str("issueI82AM8.json");
|
||||||
|
|
||||||
|
Map<String, MedicalCenter.MedicalCenterLocalized> bean1 =
|
||||||
|
JSONUtil.toBean(json, new TypeReference<Map<String, MedicalCenter.MedicalCenterLocalized>>() {
|
||||||
|
}, false);
|
||||||
|
|
||||||
|
bean1.forEach((k, v) -> Assert.assertNotNull(v.getTestimonials()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 对象
|
||||||
|
@Data
|
||||||
|
public static class MedicalCenter {
|
||||||
|
|
||||||
|
private Map<String, MedicalCenterLocalized> medicalCenterLocalized;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class MedicalCenterLocalized {
|
||||||
|
|
||||||
|
private List<Testimonial> testimonials;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class Testimonial {
|
||||||
|
private LocalDateTime createTime;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
46
hutool-json/src/test/resources/issueI82AM8.json
Normal file
46
hutool-json/src/test/resources/issueI82AM8.json
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
{
|
||||||
|
"en": {
|
||||||
|
"testimonials": [
|
||||||
|
{
|
||||||
|
"createTime": {
|
||||||
|
"dayOfYear": 261,
|
||||||
|
"dayOfWeek": "MONDAY",
|
||||||
|
"year": 2023,
|
||||||
|
"month": "SEPTEMBER",
|
||||||
|
"nano": 0,
|
||||||
|
"monthValue": 9,
|
||||||
|
"dayOfMonth": 18,
|
||||||
|
"hour": 15,
|
||||||
|
"minute": 18,
|
||||||
|
"second": 0,
|
||||||
|
"chronology": {
|
||||||
|
"id": "ISO",
|
||||||
|
"calendarType": "iso8601"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"zh": {
|
||||||
|
"testimonials": [
|
||||||
|
{
|
||||||
|
"createTime": {
|
||||||
|
"dayOfYear": 261,
|
||||||
|
"dayOfWeek": "MONDAY",
|
||||||
|
"year": 2023,
|
||||||
|
"month": "SEPTEMBER",
|
||||||
|
"nano": 0,
|
||||||
|
"monthValue": 9,
|
||||||
|
"dayOfMonth": 18,
|
||||||
|
"hour": 15,
|
||||||
|
"minute": 18,
|
||||||
|
"second": 0,
|
||||||
|
"chronology": {
|
||||||
|
"id": "ISO",
|
||||||
|
"calendarType": "iso8601"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user