This commit is contained in:
Looly
2022-09-30 22:36:20 +08:00
parent 5f61405464
commit 00fad42926
10 changed files with 64 additions and 23 deletions

View File

@@ -8,6 +8,7 @@ import cn.hutool.core.convert.impl.TemporalAccessorConverter;
import cn.hutool.core.reflect.TypeUtil;
import cn.hutool.core.text.StrUtil;
import cn.hutool.json.convert.JSONConverter;
import cn.hutool.json.serialize.JSONString;
import java.io.Serializable;
import java.time.temporal.TemporalAccessor;
@@ -56,9 +57,17 @@ public class JSONConfig implements Serializable {
*/
private boolean checkDuplicate;
/**
* 自定义的类型转换器,用于在序列化、反序列化操作中实现对象类型转换
* 自定义的类型转换器,用于在getXXX操作中自动转换类型
*/
private Converter converter = (type, value)->{
if(null == value){
return null;
}
if(value instanceof JSONString){
// 被JSONString包装的对象获取其原始类型
value = ((JSONString) value).getRaw();
}
final Class<?> rawType = TypeUtil.getClass(type);
if(null == rawType){
return value;
@@ -67,12 +76,6 @@ public class JSONConfig implements Serializable {
return JSONConverter.INSTANCE.toJSON(value);
}
if(Date.class.isAssignableFrom(rawType) || TemporalAccessor.class.isAssignableFrom(rawType)){
// 用户指定了日期格式,获取日期属性时使用对应格式
final String valueStr = Convert.convertWithCheck(String.class, value, null, isIgnoreError());
if (null == valueStr) {
return null;
}
// 日期转换,支持自定义日期格式
final String format = getDateFormat();
if (StrUtil.isNotBlank(format)) {

View File

@@ -130,6 +130,6 @@ public interface JSONGetter<K> extends TypeGetter<K> {
return defaultValue;
}
return (T) getConfig().getConverter().convert(type, value, defaultValue);
return get(key, type, getConfig().getConverter(), defaultValue);
}
}

View File

@@ -41,6 +41,9 @@ import java.util.Map;
* @since 6.0.0
*/
public class JSONConverter implements Converter {
/**
* 单例
*/
public static final JSONConverter INSTANCE = new JSONConverter(null);
static {

View File

@@ -1,8 +1,6 @@
package cn.hutool.json.serialize;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.convert.ConvertException;
import cn.hutool.core.convert.Converter;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TemporalAccessorUtil;
import cn.hutool.core.date.format.GlobalCustomFormat;
@@ -10,7 +8,6 @@ import cn.hutool.core.text.StrUtil;
import cn.hutool.json.InternalJSONUtil;
import cn.hutool.json.JSONConfig;
import java.lang.reflect.Type;
import java.time.MonthDay;
import java.time.temporal.TemporalAccessor;
import java.util.Calendar;
@@ -23,7 +20,7 @@ import java.util.Date;
* @author looly
* @since 6.0.0
*/
public class DateJSONString implements JSONString, Converter {
public class DateJSONString implements JSONString {
final Object dateObj;
final JSONConfig jsonConfig;
@@ -44,7 +41,8 @@ public class DateJSONString implements JSONString, Converter {
*
* @return 日期对象
*/
public Object getDateObj() {
@Override
public Object getRaw() {
return this.dateObj;
}
@@ -58,11 +56,6 @@ public class DateJSONString implements JSONString, Converter {
return formatDate(this.dateObj, this.jsonConfig.getDateFormat());
}
@Override
public Object convert(Type targetType, Object value) throws ConvertException {
return Convert.convert(targetType, this.dateObj);
}
/**
* 按照给定格式格式化日期,格式为空时返回时间戳字符串<br>
* 如果给定的格式是时间戳,直接返回时间戳字符串,如果是给定字符串格式,返回带双引号包装的字符串

View File

@@ -7,6 +7,7 @@ package cn.hutool.json.serialize;
* @author Looly
*
*/
@FunctionalInterface
public interface JSONString {
/**
@@ -15,4 +16,13 @@ public interface JSONString {
* @return JSON字符串
*/
String toJSONString();
/**
* 获取原始的对象默认为this
*
* @return 原始对象
*/
default Object getRaw() {
return this;
}
}

View File

@@ -10,7 +10,12 @@ import java.time.LocalTime;
import java.time.temporal.TemporalAccessor;
/**
* {@link TemporalAccessor}的JSON自定义序列化实现
* {@link TemporalAccessor}的JSON自定义序列化实现,支持包括:<br>
* <ul>
* <li>LocalDate</li>
* <li>LocalDateTime</li>
* <li>LocalTime</li>
* </ul>
*
* @author looly
* @since 5.7.22
@@ -27,6 +32,11 @@ public class TemporalAccessorSerializer implements JSONSerializer<JSONObject, Te
private final Class<? extends TemporalAccessor> temporalAccessorClass;
/**
* 构造
*
* @param temporalAccessorClass TemporalAccessor实现类型
*/
public TemporalAccessorSerializer(final Class<? extends TemporalAccessor> temporalAccessorClass) {
this.temporalAccessorClass = temporalAccessorClass;
}