mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
deprecate OptionalBean
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
import cn.hutool.core.bean.OptionalBean;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.convert.ConvertException;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
@@ -10,6 +9,7 @@ import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Date;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 用于JSON的Getter类,提供各种类型的Getter方法
|
||||
@@ -114,24 +114,27 @@ public interface JSONGetter<K> extends OptNullBasicTypeFromObjectGetter<K> {
|
||||
}
|
||||
|
||||
@Override
|
||||
default Date getDate(K key, Date defaultValue){
|
||||
default Date getDate(K key, Date defaultValue) {
|
||||
// 默认转换
|
||||
final Object obj = getObj(key);
|
||||
if (null == obj) {
|
||||
return defaultValue;
|
||||
}
|
||||
if(obj instanceof Date){
|
||||
if (obj instanceof Date) {
|
||||
return (Date) obj;
|
||||
}
|
||||
|
||||
String format = OptionalBean.ofNullable(getConfig()).getBean(JSONConfig::getDateFormat).get();
|
||||
if(StrUtil.isNotBlank(format)){
|
||||
// 用户指定了日期格式,获取日期属性时使用对应格式
|
||||
final String str = Convert.toStr(obj);
|
||||
if(null == str){
|
||||
return defaultValue;
|
||||
final Optional<String> formatOps = Optional.ofNullable(getConfig()).map(JSONConfig::getDateFormat);
|
||||
if (formatOps.isPresent()) {
|
||||
final String format = formatOps.get();
|
||||
if (StrUtil.isNotBlank(format)) {
|
||||
// 用户指定了日期格式,获取日期属性时使用对应格式
|
||||
final String str = Convert.toStr(obj);
|
||||
if (null == str) {
|
||||
return defaultValue;
|
||||
}
|
||||
return DateUtil.parse(str, format);
|
||||
}
|
||||
return DateUtil.parse(str, format);
|
||||
}
|
||||
|
||||
return Convert.toDate(obj, defaultValue);
|
||||
@@ -140,29 +143,32 @@ public interface JSONGetter<K> extends OptNullBasicTypeFromObjectGetter<K> {
|
||||
/**
|
||||
* 获取{@link LocalDateTime}类型值
|
||||
*
|
||||
* @param key 键
|
||||
* @param key 键
|
||||
* @param defaultValue 默认值
|
||||
* @return {@link LocalDateTime}
|
||||
* @since 5.7.7
|
||||
*/
|
||||
default LocalDateTime getLocalDateTime(K key, LocalDateTime defaultValue){
|
||||
default LocalDateTime getLocalDateTime(K key, LocalDateTime defaultValue) {
|
||||
// 默认转换
|
||||
final Object obj = getObj(key);
|
||||
if (null == obj) {
|
||||
return defaultValue;
|
||||
}
|
||||
if(obj instanceof LocalDateTime){
|
||||
if (obj instanceof LocalDateTime) {
|
||||
return (LocalDateTime) obj;
|
||||
}
|
||||
|
||||
String format = OptionalBean.ofNullable(getConfig()).getBean(JSONConfig::getDateFormat).get();
|
||||
if(StrUtil.isNotBlank(format)){
|
||||
// 用户指定了日期格式,获取日期属性时使用对应格式
|
||||
final String str = Convert.toStr(obj);
|
||||
if(null == str){
|
||||
return defaultValue;
|
||||
final Optional<String> formatOps = Optional.ofNullable(getConfig()).map(JSONConfig::getDateFormat);
|
||||
if (formatOps.isPresent()) {
|
||||
final String format = formatOps.get();
|
||||
if (StrUtil.isNotBlank(format)) {
|
||||
// 用户指定了日期格式,获取日期属性时使用对应格式
|
||||
final String str = Convert.toStr(obj);
|
||||
if (null == str) {
|
||||
return defaultValue;
|
||||
}
|
||||
return LocalDateTimeUtil.parse(str, format);
|
||||
}
|
||||
return LocalDateTimeUtil.parse(str, format);
|
||||
}
|
||||
|
||||
return Convert.toLocalDateTime(obj, defaultValue);
|
||||
|
Reference in New Issue
Block a user