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:
parent
99c1daf803
commit
dbd92814db
@ -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)
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
22
hutool-json/src/test/java/cn/hutool/json/Issue3713Test.java
Normal file
22
hutool-json/src/test/java/cn/hutool/json/Issue3713Test.java
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user