This commit is contained in:
Looly
2023-05-05 00:21:29 +08:00
parent 3b327ec7c3
commit 7cb48f9cd2
9 changed files with 288 additions and 58 deletions

View File

@@ -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;
}

View File

@@ -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转换
*