mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
修复JWT自定义时间格式后的时间戳转换问题
This commit is contained in:
@@ -177,7 +177,7 @@ public interface JSON extends Cloneable, Serializable {
|
||||
* @since 3.0.8
|
||||
*/
|
||||
default <T> T toBean(Type type) {
|
||||
return toBean(type, getConfig().isIgnoreError());
|
||||
return JSONConverter.jsonConvert(type, this, getConfig());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -188,8 +188,10 @@ public interface JSON extends Cloneable, Serializable {
|
||||
* @param ignoreError 是否忽略转换错误
|
||||
* @return 实体类对象
|
||||
* @since 4.3.2
|
||||
* @deprecated 请使用 {@link #toBean(Type)}, ignoreError在JSONConfig中生效
|
||||
*/
|
||||
@Deprecated
|
||||
default <T> T toBean(Type type, boolean ignoreError) {
|
||||
return JSONConverter.jsonConvert(type, this, ignoreError);
|
||||
return JSONConverter.jsonConvert(type, this, JSONConfig.create().setIgnoreError(ignoreError));
|
||||
}
|
||||
}
|
||||
|
@@ -214,7 +214,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
|
||||
|
||||
@Override
|
||||
public <T> T getByPath(String expression, Class<T> resultType) {
|
||||
return JSONConverter.jsonConvert(resultType, getByPath(expression), true);
|
||||
return JSONConverter.jsonConvert(resultType, getByPath(expression), getConfig());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -65,12 +65,12 @@ public class JSONConverter implements Converter<JSON> {
|
||||
* @param <T> 转换后的对象类型
|
||||
* @param targetType 目标类型
|
||||
* @param value 值
|
||||
* @param ignoreError 是否忽略转换错误
|
||||
* @param jsonConfig JSON配置
|
||||
* @return 目标类型的值
|
||||
* @throws ConvertException 转换失败
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
protected static <T> T jsonConvert(Type targetType, Object value, boolean ignoreError) throws ConvertException {
|
||||
protected static <T> T jsonConvert(Type targetType, Object value, JSONConfig jsonConfig) throws ConvertException {
|
||||
if (JSONUtil.isNull(value)) {
|
||||
return null;
|
||||
}
|
||||
@@ -92,7 +92,7 @@ public class JSONConverter implements Converter<JSON> {
|
||||
}
|
||||
}
|
||||
|
||||
return jsonToBean(targetType, value, ignoreError);
|
||||
return jsonToBean(targetType, value, jsonConfig.isIgnoreError());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -2,6 +2,7 @@ package cn.hutool.json;
|
||||
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.convert.ConvertException;
|
||||
import cn.hutool.core.convert.NumberWithFormat;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.date.LocalDateTimeUtil;
|
||||
import cn.hutool.core.getter.OptNullBasicTypeFromObjectGetter;
|
||||
@@ -138,6 +139,8 @@ public interface JSONGetter<K> extends OptNullBasicTypeFromObjectGetter<K> {
|
||||
}
|
||||
if (obj instanceof Date) {
|
||||
return (Date) obj;
|
||||
} else if(obj instanceof NumberWithFormat){
|
||||
return (Date) ((NumberWithFormat) obj).convert(Date.class, obj);
|
||||
}
|
||||
|
||||
final Optional<String> formatOps = Optional.ofNullable(getConfig()).map(JSONConfig::getDateFormat);
|
||||
@@ -232,6 +235,6 @@ public interface JSONGetter<K> extends OptNullBasicTypeFromObjectGetter<K> {
|
||||
if (JSONUtil.isNull(value)) {
|
||||
return null;
|
||||
}
|
||||
return JSONConverter.jsonConvert(type, value, ignoreError);
|
||||
return JSONConverter.jsonConvert(type, value, JSONConfig.create().setIgnoreError(ignoreError));
|
||||
}
|
||||
}
|
||||
|
@@ -317,7 +317,7 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
|
||||
|
||||
@Override
|
||||
public <T> T getByPath(String expression, Class<T> resultType) {
|
||||
return JSONConverter.jsonConvert(resultType, getByPath(expression), true);
|
||||
return JSONConverter.jsonConvert(resultType, getByPath(expression), getConfig());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -1,5 +1,6 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
import cn.hutool.core.convert.NumberWithFormat;
|
||||
import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.io.file.FileReader;
|
||||
import cn.hutool.core.lang.TypeReference;
|
||||
@@ -498,6 +499,7 @@ public class JSONUtil {
|
||||
* @return 实体类对象
|
||||
* @since 4.3.2
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public static <T> T toBean(JSON json, Type beanType, boolean ignoreError) {
|
||||
if (null == json) {
|
||||
return null;
|
||||
@@ -746,7 +748,6 @@ public class JSONUtil {
|
||||
* @param jsonConfig JSON选项
|
||||
* @return 包装后的值,null表示此值需被忽略
|
||||
*/
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
public static Object wrap(Object object, JSONConfig jsonConfig) {
|
||||
if (object == null) {
|
||||
return jsonConfig.isIgnoreNullValue() ? null : JSONNull.NULL;
|
||||
@@ -758,6 +759,9 @@ public class JSONUtil {
|
||||
|| object instanceof Number //
|
||||
|| ObjectUtil.isBasicType(object) //
|
||||
) {
|
||||
if(object instanceof Number && null != jsonConfig.getDateFormat()){
|
||||
return new NumberWithFormat((Number) object, jsonConfig.getDateFormat());
|
||||
}
|
||||
return object;
|
||||
}
|
||||
|
||||
|
@@ -219,14 +219,14 @@ public class JSONObjectTest {
|
||||
|
||||
@Test
|
||||
public void toBeanNullStrTest() {
|
||||
final JSONObject json = JSONUtil.createObj()//
|
||||
final JSONObject json = JSONUtil.createObj(JSONConfig.create().setIgnoreError(true))//
|
||||
.set("strValue", "null")//
|
||||
.set("intValue", 123)//
|
||||
// 子对象对应"null"字符串,如果忽略错误,跳过,否则抛出转换异常
|
||||
.set("beanValue", "null")//
|
||||
.set("list", JSONUtil.createArray().set("a").set("b"));
|
||||
|
||||
final TestBean bean = json.toBean(TestBean.class, true);
|
||||
final TestBean bean = json.toBean(TestBean.class);
|
||||
// 当JSON中为字符串"null"时应被当作字符串处理
|
||||
Assert.assertEquals("null", bean.getStrValue());
|
||||
// 当JSON中为字符串"null"时Bean中的字段类型不匹配应在ignoreError模式下忽略注入
|
||||
|
Reference in New Issue
Block a user