改进TemporalAccessorSerializer支持dayOfMonth和month枚举名

This commit is contained in:
Looly 2023-09-21 13:06:23 +08:00
parent b06885e2cd
commit 86ba812a93
3 changed files with 131 additions and 6 deletions

View File

@ -12,6 +12,8 @@
package org.dromara.hutool.json.serialize;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.math.NumberUtil;
import org.dromara.hutool.json.JSON;
import org.dromara.hutool.json.JSONException;
import org.dromara.hutool.json.JSONObject;
@ -19,6 +21,7 @@ import org.dromara.hutool.json.JSONObject;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.temporal.TemporalAccessor;
/**
@ -83,13 +86,46 @@ public class TemporalAccessorSerializer implements JSONSerializer<JSONObject, Te
@Override
public TemporalAccessor deserialize(final JSON json) {
final JSONObject jsonObject = (JSONObject) json;
if (LocalDate.class.equals(this.temporalAccessorClass) || LocalDateTime.class.equals(this.temporalAccessorClass)) {
//
final Integer year = jsonObject.getInt(YEAR_KEY);
Assert.notNull(year, "Field 'year' must be not null");
//
final String monthStr = jsonObject.getStr(MONTH_KEY);
Assert.notNull(monthStr, "Field 'month' must be not null");
Integer month = NumberUtil.parseInt(monthStr, null);
if (null == month) {
final Month monthEnum = Month.valueOf(monthStr);
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.of(jsonObject.getInt(YEAR_KEY), jsonObject.getInt(MONTH_KEY), jsonObject.getInt(DAY_KEY));
} else if (LocalDateTime.class.equals(this.temporalAccessorClass)) {
return LocalDateTime.of(jsonObject.getInt(YEAR_KEY), jsonObject.getInt(MONTH_KEY), jsonObject.getInt(DAY_KEY),
jsonObject.getInt(HOUR_KEY), jsonObject.getInt(MINUTE_KEY), jsonObject.getInt(SECOND_KEY), jsonObject.getInt(NANO_KEY));
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)) {
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));
}
throw new JSONException("Unsupported type from JSON: {}", this.temporalAccessorClass);

View File

@ -0,0 +1,43 @@
package org.dromara.hutool.json;
import lombok.Data;
import org.dromara.hutool.core.io.resource.ResourceUtil;
import org.dromara.hutool.core.reflect.TypeReference;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
public class IssueI82AM8Test {
@Test
void toBeanTest() {
final String json = ResourceUtil.readUtf8Str("issueI82AM8.json");
final Map<String, MedicalCenter.MedicalCenterLocalized> bean1 =
JSONUtil.toBean(json, new TypeReference<Map<String, MedicalCenter.MedicalCenterLocalized>>() {});
//Console.log(bean1);
bean1.forEach((k, v) -> Assertions.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;
}
}
}
}

View 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"
}
}
}
]
}
}