mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix code
This commit is contained in:
@@ -14,13 +14,13 @@ package org.dromara.hutool.json.convert;
|
||||
|
||||
import org.dromara.hutool.core.array.ArrayUtil;
|
||||
import org.dromara.hutool.core.bean.BeanUtil;
|
||||
import org.dromara.hutool.core.bean.RecordUtil;
|
||||
import org.dromara.hutool.core.bean.copier.BeanCopier;
|
||||
import org.dromara.hutool.core.convert.Convert;
|
||||
import org.dromara.hutool.core.convert.ConvertException;
|
||||
import org.dromara.hutool.core.convert.Converter;
|
||||
import org.dromara.hutool.core.convert.RegisterConverter;
|
||||
import org.dromara.hutool.core.convert.impl.*;
|
||||
import org.dromara.hutool.core.lang.Console;
|
||||
import org.dromara.hutool.core.map.MapWrapper;
|
||||
import org.dromara.hutool.core.reflect.ConstructorUtil;
|
||||
import org.dromara.hutool.core.reflect.TypeReference;
|
||||
@@ -228,7 +228,7 @@ public class JSONConverter implements Converter {
|
||||
}
|
||||
|
||||
// 无法转换
|
||||
throw new JSONException("Can not convert from {}: [{}] to [{}]",
|
||||
throw new JSONException("Can not convert from '{}': {} to '{}'",
|
||||
json.getClass().getName(), json, targetType.getTypeName());
|
||||
}
|
||||
|
||||
@@ -280,6 +280,11 @@ public class JSONConverter implements Converter {
|
||||
return (T) ArrayConverter.INSTANCE.convert(type, value);
|
||||
}
|
||||
|
||||
// Record
|
||||
if(RecordUtil.isRecord(rowType)){
|
||||
return (T) RecordConverter.INSTANCE.convert(type, value);
|
||||
}
|
||||
|
||||
// 表示非需要特殊转换的对象
|
||||
return null;
|
||||
}
|
||||
|
@@ -13,10 +13,12 @@
|
||||
package org.dromara.hutool.json.mapper;
|
||||
|
||||
import org.dromara.hutool.core.bean.BeanUtil;
|
||||
import org.dromara.hutool.core.bean.RecordUtil;
|
||||
import org.dromara.hutool.core.bean.copier.CopyOptions;
|
||||
import org.dromara.hutool.core.convert.Convert;
|
||||
import org.dromara.hutool.core.io.IoUtil;
|
||||
import org.dromara.hutool.core.lang.mutable.MutableEntry;
|
||||
import org.dromara.hutool.core.reflect.MethodUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.json.InternalJSONUtil;
|
||||
import org.dromara.hutool.json.JSONArray;
|
||||
@@ -30,6 +32,7 @@ import org.dromara.hutool.json.serialize.JSONSerializer;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Map;
|
||||
import java.util.ResourceBundle;
|
||||
@@ -105,7 +108,7 @@ public class JSONObjectMapper {
|
||||
if (source instanceof JSONTokener) {
|
||||
// JSONTokener
|
||||
mapFromTokener((JSONTokener) source, jsonObject);
|
||||
}else if (source instanceof Map) {
|
||||
} else if (source instanceof Map) {
|
||||
// Map
|
||||
for (final Map.Entry<?, ?> e : ((Map<?, ?>) source).entrySet()) {
|
||||
jsonObject.set(Convert.toStr(e.getKey()), e.getValue(), predicate, false);
|
||||
@@ -125,11 +128,14 @@ public class JSONObjectMapper {
|
||||
} else if (source instanceof ResourceBundle) {
|
||||
// ResourceBundle
|
||||
mapFromResourceBundle((ResourceBundle) source, jsonObject);
|
||||
} else if (RecordUtil.isRecord(source.getClass())) {
|
||||
// since 6.0.0
|
||||
mapFromRecord(source, jsonObject);
|
||||
} else if (BeanUtil.isReadableBean(source.getClass())) {
|
||||
// 普通Bean
|
||||
mapFromBean(source, jsonObject);
|
||||
} else {
|
||||
if(!jsonObject.config().isIgnoreError()){
|
||||
if (!jsonObject.config().isIgnoreError()) {
|
||||
// 不支持对象类型转换为JSONObject
|
||||
throw new JSONException("Unsupported type [{}] to JSONObject!", source.getClass());
|
||||
}
|
||||
@@ -180,6 +186,22 @@ public class JSONObjectMapper {
|
||||
JSONParser.of(x).parseTo(jsonObject, this.predicate);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从Record转换
|
||||
*
|
||||
* @param record Record对象
|
||||
* @param jsonObject {@link JSONObject}
|
||||
*/
|
||||
private void mapFromRecord(final Object record, final JSONObject jsonObject) {
|
||||
final Map.Entry<String, Type>[] components = RecordUtil.getRecordComponents(record.getClass());
|
||||
|
||||
String key;
|
||||
for (final Map.Entry<String, Type> entry : components) {
|
||||
key = entry.getKey();
|
||||
jsonObject.set(key, MethodUtil.invoke(record, key));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从Bean转换
|
||||
*
|
||||
|
Reference in New Issue
Block a user