mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix code
This commit is contained in:
@@ -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.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.core.util.TypeUtil;
|
||||
import cn.hutool.json.serialize.GlobalSerializeMapping;
|
||||
import cn.hutool.json.serialize.JSONArraySerializer;
|
||||
import cn.hutool.json.serialize.JSONDeserializer;
|
||||
import cn.hutool.json.serialize.JSONObjectSerializer;
|
||||
import cn.hutool.json.serialize.JSONSerializer;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@@ -763,24 +761,6 @@ public class JSONUtil {
|
||||
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 {
|
||||
// fix issue#1399@Github
|
||||
if(object instanceof SQLException){
|
||||
|
@@ -12,7 +12,7 @@ public class Issue2447Test {
|
||||
Time time = new Time();
|
||||
time.setTime(LocalDateTime.of(1970, 1, 2, 10, 0, 1, 0));
|
||||
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());
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user