mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix code
This commit is contained in:
parent
d033b1ec0f
commit
054f3fbf61
@ -285,8 +285,8 @@ public class ConverterRegistry implements Serializable {
|
|||||||
final Class<?> clazz = ClassLoaderUtil.loadClass("cn.hutool.json.BeanConverterForJSON");
|
final Class<?> clazz = ClassLoaderUtil.loadClass("cn.hutool.json.BeanConverterForJSON");
|
||||||
return ((Converter<T>)ReflectUtil.newInstance(clazz, type)).convert(value, defaultValue);
|
return ((Converter<T>)ReflectUtil.newInstance(clazz, type)).convert(value, defaultValue);
|
||||||
}catch (final Throwable ignore){
|
}catch (final Throwable ignore){
|
||||||
return new BeanConverter<T>(type).convert(value, defaultValue);
|
|
||||||
}
|
}
|
||||||
|
return new BeanConverter<T>(type).convert(value, defaultValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 无法转换
|
// 无法转换
|
||||||
|
@ -31,7 +31,7 @@ public class BeanConverter<T> extends AbstractConverter<T> {
|
|||||||
|
|
||||||
private final Type beanType;
|
private final Type beanType;
|
||||||
private final Class<T> beanClass;
|
private final Class<T> beanClass;
|
||||||
protected CopyOptions copyOptions;
|
private final CopyOptions copyOptions;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造,默认转换选项,注入失败的字段忽略
|
* 构造,默认转换选项,注入失败的字段忽略
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package cn.hutool.core.convert.impl;
|
package cn.hutool.core.convert.impl;
|
||||||
|
|
||||||
import cn.hutool.core.convert.AbstractConverter;
|
import cn.hutool.core.convert.AbstractConverter;
|
||||||
|
import cn.hutool.core.convert.Convert;
|
||||||
|
import cn.hutool.core.convert.ConvertException;
|
||||||
import cn.hutool.core.date.DateTime;
|
import cn.hutool.core.date.DateTime;
|
||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
@ -23,6 +25,7 @@ import java.time.format.DateTimeFormatter;
|
|||||||
import java.time.temporal.TemporalAccessor;
|
import java.time.temporal.TemporalAccessor;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -109,6 +112,17 @@ public class TemporalAccessorConverter extends AbstractConverter<TemporalAccesso
|
|||||||
} else if (value instanceof Calendar) {
|
} else if (value instanceof Calendar) {
|
||||||
final Calendar calendar = (Calendar) value;
|
final Calendar calendar = (Calendar) value;
|
||||||
return parseFromInstant(calendar.toInstant(), calendar.getTimeZone().toZoneId());
|
return parseFromInstant(calendar.toInstant(), calendar.getTimeZone().toZoneId());
|
||||||
|
} else if (value instanceof Map) {
|
||||||
|
final Map<?, ?> map = (Map<?, ?>) value;
|
||||||
|
if (LocalDate.class.equals(this.targetType)) {
|
||||||
|
return LocalDate.of(Convert.toInt(map.get("year")), Convert.toInt(map.get("month")), Convert.toInt(map.get("day")));
|
||||||
|
} else if (LocalDateTime.class.equals(this.targetType)) {
|
||||||
|
return LocalDateTime.of(Convert.toInt(map.get("year")), Convert.toInt(map.get("month")), Convert.toInt(map.get("day")),
|
||||||
|
Convert.toInt(map.get("hour")), Convert.toInt(map.get("minute")), Convert.toInt(map.get("second")), Convert.toInt(map.get("second")));
|
||||||
|
} else if (LocalTime.class.equals(this.targetType)) {
|
||||||
|
return LocalTime.of(Convert.toInt(map.get("hour")), Convert.toInt(map.get("minute")), Convert.toInt(map.get("second")), Convert.toInt(map.get("nano")));
|
||||||
|
}
|
||||||
|
throw new ConvertException("Unsupported type: [{}] from map: [{}]", this.targetType, map);
|
||||||
} else {
|
} else {
|
||||||
return parseFromCharSequence(convertToStr(value));
|
return parseFromCharSequence(convertToStr(value));
|
||||||
}
|
}
|
||||||
|
@ -1,43 +0,0 @@
|
|||||||
package cn.hutool.json;
|
|
||||||
|
|
||||||
import cn.hutool.core.bean.BeanUtil;
|
|
||||||
import cn.hutool.core.convert.impl.BeanConverter;
|
|
||||||
import cn.hutool.json.serialize.GlobalSerializeMapping;
|
|
||||||
import cn.hutool.json.serialize.JSONDeserializer;
|
|
||||||
|
|
||||||
import java.lang.reflect.Type;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 针对JSON的Bean转换封装。<br>
|
|
||||||
* 此类时针对5.x中设计缺陷设计的类,在ConverterRegistry中通过反射调用
|
|
||||||
*
|
|
||||||
* @param <T> Bean类型
|
|
||||||
* @since 5.8.6
|
|
||||||
*/
|
|
||||||
public class BeanConverterForJSON<T> extends BeanConverter<T> {
|
|
||||||
|
|
||||||
public BeanConverterForJSON(Type beanType) {
|
|
||||||
super(beanType);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected T convertInternal(final Object value) {
|
|
||||||
final Class<T> targetType = getTargetType();
|
|
||||||
if (value instanceof JSON) {
|
|
||||||
final JSONDeserializer<?> deserializer = GlobalSerializeMapping.getDeserializer(targetType);
|
|
||||||
if (null != deserializer) {
|
|
||||||
//noinspection unchecked
|
|
||||||
return (T) deserializer.deserialize((JSON) value);
|
|
||||||
}
|
|
||||||
|
|
||||||
// issue#2212@Github
|
|
||||||
// 在JSONObject转Bean时,读取JSONObject本身的配置文件
|
|
||||||
if (value instanceof JSONGetter && BeanUtil.hasSetter(targetType)) {
|
|
||||||
final JSONConfig config = ((JSONGetter<?>) value).getConfig();
|
|
||||||
this.copyOptions.setIgnoreError(config.isIgnoreError());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return super.convertInternal(value);
|
|
||||||
}
|
|
||||||
}
|
|
@ -9,12 +9,10 @@ import cn.hutool.core.util.ClassUtil;
|
|||||||
import cn.hutool.core.util.HexUtil;
|
import cn.hutool.core.util.HexUtil;
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.core.util.TypeUtil;
|
|
||||||
import cn.hutool.json.serialize.GlobalSerializeMapping;
|
import cn.hutool.json.serialize.GlobalSerializeMapping;
|
||||||
import cn.hutool.json.serialize.JSONArraySerializer;
|
import cn.hutool.json.serialize.JSONArraySerializer;
|
||||||
import cn.hutool.json.serialize.JSONDeserializer;
|
import cn.hutool.json.serialize.JSONDeserializer;
|
||||||
import cn.hutool.json.serialize.JSONObjectSerializer;
|
import cn.hutool.json.serialize.JSONObjectSerializer;
|
||||||
import cn.hutool.json.serialize.JSONSerializer;
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -763,24 +761,6 @@ public class JSONUtil {
|
|||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 自定义序列化
|
|
||||||
final JSONSerializer serializer = GlobalSerializeMapping.getSerializer(object.getClass());
|
|
||||||
if (null != serializer) {
|
|
||||||
final Type jsonType = TypeUtil.getTypeArgument(serializer.getClass());
|
|
||||||
if (null != jsonType) {
|
|
||||||
final JSON json;
|
|
||||||
if (serializer instanceof JSONObjectSerializer) {
|
|
||||||
json = new JSONObject(jsonConfig);
|
|
||||||
} else if (serializer instanceof JSONArraySerializer) {
|
|
||||||
json = new JSONArray(jsonConfig);
|
|
||||||
} else{
|
|
||||||
throw new JSONException("Unsupported JSONSerializer type: " + serializer.getClass());
|
|
||||||
}
|
|
||||||
serializer.serialize(json, object);
|
|
||||||
return json;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// fix issue#1399@Github
|
// fix issue#1399@Github
|
||||||
if(object instanceof SQLException){
|
if(object instanceof SQLException){
|
||||||
|
@ -12,7 +12,7 @@ public class Issue2447Test {
|
|||||||
Time time = new Time();
|
Time time = new Time();
|
||||||
time.setTime(LocalDateTime.of(1970, 1, 2, 10, 0, 1, 0));
|
time.setTime(LocalDateTime.of(1970, 1, 2, 10, 0, 1, 0));
|
||||||
String timeStr = JSONUtil.toJsonStr(time);
|
String timeStr = JSONUtil.toJsonStr(time);
|
||||||
Assert.assertEquals(timeStr, "{\"time\":93601000}");
|
Assert.assertEquals("{\"time\":93601000}", timeStr);
|
||||||
Assert.assertEquals(JSONUtil.toBean(timeStr, Time.class).getTime(), time.getTime());
|
Assert.assertEquals(JSONUtil.toBean(timeStr, Time.class).getTime(), time.getTime());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user