mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix bug
This commit is contained in:
@@ -40,6 +40,7 @@ import java.util.List;
|
||||
public class JSONUtil {
|
||||
|
||||
// -------------------------------------------------------------------- Pause start
|
||||
|
||||
/**
|
||||
* 创建JSONObject
|
||||
*
|
||||
@@ -169,7 +170,7 @@ public class JSONUtil {
|
||||
* @return JSON(JSONObject or JSONArray)
|
||||
*/
|
||||
public static Object parse(final Object obj, final JSONConfig config) {
|
||||
if(null == config){
|
||||
if (null == config) {
|
||||
return JSONConverter.INSTANCE.toJSON(obj);
|
||||
}
|
||||
return JSONConverter.of(config).toJSON(obj);
|
||||
@@ -226,6 +227,7 @@ public class JSONUtil {
|
||||
// -------------------------------------------------------------------- Read end
|
||||
|
||||
// -------------------------------------------------------------------- toString start
|
||||
|
||||
/**
|
||||
* 转换为格式化后的JSON字符串
|
||||
*
|
||||
@@ -270,7 +272,7 @@ public class JSONUtil {
|
||||
return stringWriter.toString();
|
||||
}
|
||||
|
||||
if(null == obj){
|
||||
if (null == obj) {
|
||||
return null;
|
||||
}
|
||||
return parse(obj, jsonConfig).toString();
|
||||
@@ -323,48 +325,18 @@ public class JSONUtil {
|
||||
// -------------------------------------------------------------------- toString end
|
||||
|
||||
// -------------------------------------------------------------------- toBean start
|
||||
|
||||
/**
|
||||
* JSON字符串转为实体类对象,转换异常将被抛出
|
||||
* 转为实体类对象
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @param jsonString JSON字符串
|
||||
* @param beanClass 实体类对象
|
||||
* @param <T> Bean类型
|
||||
* @param json JSONObject
|
||||
* @param clazz 实体类
|
||||
* @return 实体类对象
|
||||
* @since 3.1.2
|
||||
* @since 4.6.2
|
||||
*/
|
||||
public static <T> T toBean(final String jsonString, final Class<T> beanClass) {
|
||||
return toBean(parse(jsonString), beanClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON字符串转为实体类对象,转换异常将被抛出<br>
|
||||
* 通过{@link JSONConfig}可选是否忽略大小写、忽略null等配置
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @param jsonString JSON字符串
|
||||
* @param config JSON配置
|
||||
* @param beanClass 实体类对象
|
||||
* @return 实体类对象
|
||||
* @since 5.8.0
|
||||
*/
|
||||
public static <T> T toBean(final String jsonString, final JSONConfig config, final Class<T> beanClass) {
|
||||
return toBean(jsonString, config, (Type) beanClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON字符串转为实体类对象,转换异常将被抛出<br>
|
||||
* 通过{@link JSONConfig}可选是否忽略大小写、忽略null等配置
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @param jsonString JSON字符串
|
||||
* @param config JSON配置
|
||||
* @param type Bean类型
|
||||
* @return 实体类对象
|
||||
* @throws JSONException 提供的JSON字符串不支持转Bean或字符串错误
|
||||
*/
|
||||
public static <T> T toBean(final String jsonString, final JSONConfig config, final Type type) throws JSONException {
|
||||
return toBean(parse(jsonString, config), type);
|
||||
public static <T> T toBean(final Object json, final Class<T> clazz) {
|
||||
Assert.notNull(clazz);
|
||||
return toBean(json, (Type)clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -391,12 +363,31 @@ public class JSONUtil {
|
||||
* @since 4.3.2
|
||||
*/
|
||||
public static <T> T toBean(final Object json, final Type type) {
|
||||
return toBean(json, null, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转为实体类对象
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @param json JSONObject
|
||||
* @param config JSON配置
|
||||
* @param type 实体类对象类型
|
||||
* @return 实体类对象
|
||||
* @since 4.3.2
|
||||
*/
|
||||
public static <T> T toBean(Object json, final JSONConfig config, Type type) {
|
||||
if (null == json) {
|
||||
return null;
|
||||
}
|
||||
json = parse(json, config);
|
||||
if (json instanceof JSON) {
|
||||
if (type instanceof TypeReference) {
|
||||
type = ((TypeReference<?>) type).getType();
|
||||
}
|
||||
return ((JSON) json).toBean(type);
|
||||
}
|
||||
|
||||
throw new JSONException("Unsupported json string to bean : {}", json);
|
||||
}
|
||||
// -------------------------------------------------------------------- toBean end
|
||||
|
@@ -12,6 +12,7 @@
|
||||
|
||||
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.copier.BeanCopier;
|
||||
import org.dromara.hutool.core.convert.Convert;
|
||||
@@ -24,10 +25,8 @@ import org.dromara.hutool.core.reflect.ConstructorUtil;
|
||||
import org.dromara.hutool.core.reflect.TypeReference;
|
||||
import org.dromara.hutool.core.reflect.TypeUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.core.array.ArrayUtil;
|
||||
import org.dromara.hutool.core.util.ObjUtil;
|
||||
import org.dromara.hutool.json.*;
|
||||
import org.dromara.hutool.json.*;
|
||||
import org.dromara.hutool.json.serialize.JSONDeserializer;
|
||||
import org.dromara.hutool.json.serialize.JSONStringer;
|
||||
|
||||
@@ -233,6 +232,7 @@ public class JSONConverter implements Converter {
|
||||
* <pre>
|
||||
* Collection
|
||||
* Map
|
||||
* Map.Entry
|
||||
* 强转(无需转换)
|
||||
* 数组
|
||||
* </pre>
|
||||
@@ -258,6 +258,11 @@ public class JSONConverter implements Converter {
|
||||
return (T) MapConverter.INSTANCE.convert(type, value);
|
||||
}
|
||||
|
||||
// issue#I6SZYB Entry类(含有泛型参数,不可以默认强转)
|
||||
if(Map.Entry.class.isAssignableFrom(rowType)){
|
||||
return (T) EntryConverter.INSTANCE.convert(type, value);
|
||||
}
|
||||
|
||||
// 默认强转
|
||||
if (rowType.isInstance(value)) {
|
||||
return (T) value;
|
||||
|
@@ -0,0 +1,51 @@
|
||||
package org.dromara.hutool.json;
|
||||
|
||||
import org.dromara.hutool.core.lang.mutable.MutableEntry;
|
||||
import org.dromara.hutool.core.reflect.TypeReference;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.AbstractMap;
|
||||
|
||||
public class IssueI6SZYBTest {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
void mutableEntryTest() {
|
||||
final MutableEntry<String, String> entry = MutableEntry.of("a", "b");
|
||||
final String jsonStr = JSONUtil.toJsonStr(entry);
|
||||
final MutableEntry<String, String> entry2 = JSONUtil.toBean(jsonStr, MutableEntry.class);
|
||||
|
||||
Assertions.assertEquals(entry, entry2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void mutableEntryTest2() {
|
||||
final MutableEntry<Integer, Integer> entry = MutableEntry.of(1, 2);
|
||||
final String jsonStr = JSONUtil.toJsonStr(entry);
|
||||
final MutableEntry<Integer, Integer> entry2 = JSONUtil.toBean(jsonStr,
|
||||
new TypeReference<MutableEntry<Integer, Integer>>() {});
|
||||
|
||||
Assertions.assertEquals(entry, entry2);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
void simpleEntryTest() {
|
||||
final AbstractMap.SimpleEntry<String, String> entry = new AbstractMap.SimpleEntry<>("a", "b");
|
||||
final String jsonStr = JSONUtil.toJsonStr(entry);
|
||||
final AbstractMap.SimpleEntry<String, String> entry2 = JSONUtil.toBean(jsonStr, AbstractMap.SimpleEntry.class);
|
||||
|
||||
Assertions.assertEquals(entry, entry2);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
void simpleEntryTest2() {
|
||||
final AbstractMap.SimpleEntry<String, String> entry = new AbstractMap.SimpleEntry<>("a", "b");
|
||||
final String jsonStr = JSONUtil.toJsonStr(entry);
|
||||
final MutableEntry<String, String> entry2 = JSONUtil.toBean(jsonStr, MutableEntry.class);
|
||||
|
||||
Assertions.assertEquals(entry, entry2);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user