1
0
mirror of https://gitee.com/dromara/hutool.git synced 2025-04-05 17:37:59 +08:00

修复JSONConfig.setDateFormat设置后toBean无效问题

This commit is contained in:
Looly 2024-09-02 13:44:52 +08:00
parent 99c1daf803
commit dbd92814db
5 changed files with 62 additions and 2 deletions
CHANGELOG.md
hutool-core/src/main/java/cn/hutool/core
hutool-json/src
main/java/cn/hutool/json
test/java/cn/hutool/json

View File

@ -2,10 +2,11 @@
# 🚀Changelog
-------------------------------------------------------------------------------------------------------------
# 5.8.33(2024-08-30)
# 5.8.33(2024-09-02)
### 🐣新特性
### 🐞Bug修复
* 【json 】 修复JSONConfig.setDateFormat设置后toBean无效问题issue#3713@Gitee
-------------------------------------------------------------------------------------------------------------
**# 5.8.32(2024-08-30)

View File

@ -4,6 +4,7 @@ import cn.hutool.core.bean.PropDesc;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.convert.TypeConverter;
import cn.hutool.core.convert.impl.DateConverter;
import cn.hutool.core.lang.Editor;
import cn.hutool.core.lang.func.Func1;
import cn.hutool.core.lang.func.LambdaUtil;
@ -14,6 +15,7 @@ import cn.hutool.core.util.StrUtil;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;
@ -96,6 +98,11 @@ public class CopyOptions implements Serializable {
return Convert.convertWithCheck(type, value, null, ignoreError);
};
/**
* 在Bean转换时如果源是String目标对象是Date或LocalDateTime则可自定义转换格式
*/
private String formatIfDate;
//region create
/**
@ -359,6 +366,24 @@ public class CopyOptions implements Serializable {
return this;
}
/**
* 获取日期格式用于日期转字符串默认为{@code null}
* @return 日期格式
*/
public String getFormatIfDate() {
return formatIfDate;
}
/**
* 设置日期格式用于日期转字符串默认为{@code null}
* @param formatIfDate 日期格式
* @return this
*/
public CopyOptions setFormatIfDate(String formatIfDate) {
this.formatIfDate = formatIfDate;
return this;
}
/**
* 使用自定义转换器转换字段值<br>
* 如果自定义转换器为{@code null}则返回原值
@ -368,7 +393,12 @@ public class CopyOptions implements Serializable {
* @return 编辑后的字段值
* @since 5.8.0
*/
@SuppressWarnings({"unchecked", "rawtypes"})
protected Object convertField(Type targetType, Object fieldValue) {
if((targetType instanceof Class && Date.class.isAssignableFrom((Class<?>) targetType)) && null != this.formatIfDate){
return new DateConverter((Class) targetType, this.formatIfDate).convert(fieldValue, null);
}
return (null != this.converter) ?
this.converter.convert(targetType, fieldValue) : fieldValue;
}

View File

@ -1090,6 +1090,13 @@ public class DateTime extends Date {
String pattern;
if (dateFormat instanceof SimpleDateFormat) {
pattern = ((SimpleDateFormat) dateFormat).toPattern();
// issue#3713 尝试使用US Locale解析
try {
DateUtil.newSimpleFormat(pattern, Locale.US, null).parse(dateStr.toString());
} catch (Exception ignore) {
// ignore
}
} else {
pattern = dateFormat.toString();
}

View File

@ -130,7 +130,7 @@ public class JSONConverter implements Converter<JSON> {
final JSONConfig config = ((JSONGetter<?>) value).getConfig();
final Converter<T> converter = new BeanConverter<>(targetType,
InternalJSONUtil.toCopyOptions(config).setIgnoreError(ignoreError));
InternalJSONUtil.toCopyOptions(config).setIgnoreError(ignoreError).setFormatIfDate(config.getDateFormat()));
return converter.convertWithCheck(value, null, ignoreError);
}
}

View File

@ -0,0 +1,22 @@
package cn.hutool.json;
import lombok.Data;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.text.ParseException;
import java.util.Date;
public class Issue3713Test {
@Test
void toBeanTest() throws ParseException {
String jsonStr = "{\"operDate\":\"Aug 22, 2024, 4:21:21 PM\"}";
final CustomerCreateLog bean = JSONUtil.toBean(jsonStr, JSONConfig.create().setDateFormat("MMM dd, yyyy, h:mm:ss a"), CustomerCreateLog.class);
Assertions.assertEquals("Thu Aug 22 16:21:21 CST 2024", bean.getOperDate().toString());
}
@Data
private static class CustomerCreateLog{
private Date operDate;
}
}