mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix bug
This commit is contained in:
parent
1e5f5dc031
commit
afc4b2b213
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
# 5.7.10 (2021-08-19)
|
# 5.7.10 (2021-08-22)
|
||||||
|
|
||||||
### 🐣新特性
|
### 🐣新特性
|
||||||
* 【core 】 增加NamingCase类
|
* 【core 】 增加NamingCase类
|
||||||
@ -18,6 +18,7 @@
|
|||||||
* 【core 】 修复MapUtil.sort比较器不一致返回原map的问题(issue#I46AQJ@Gitee)
|
* 【core 】 修复MapUtil.sort比较器不一致返回原map的问题(issue#I46AQJ@Gitee)
|
||||||
|
|
||||||
### 🐞Bug修复
|
### 🐞Bug修复
|
||||||
|
* 【core 】 修复JSONSupport默认循环引用导致的问题(issue#1779@Github)
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -85,10 +85,30 @@ public class JSONConverter implements Converter<JSON> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(value instanceof JSON) {
|
return jsonToBean(targetType, value, ignoreError);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JSON递归转换<br>
|
||||||
|
* 首先尝试JDK类型转换,如果失败尝试JSON转Bean
|
||||||
|
*
|
||||||
|
* @param <T> 转换后的对象类型
|
||||||
|
* @param targetType 目标类型
|
||||||
|
* @param value 值,JSON格式
|
||||||
|
* @param ignoreError 是否忽略转换错误
|
||||||
|
* @return 目标类型的值
|
||||||
|
* @throws ConvertException 转换失败
|
||||||
|
*/
|
||||||
|
protected static <T> T jsonToBean(Type targetType, Object value, boolean ignoreError) throws ConvertException {
|
||||||
|
if (JSONUtil.isNull(value)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(value instanceof JSON){
|
||||||
final JSONDeserializer<?> deserializer = GlobalSerializeMapping.getDeserializer(targetType);
|
final JSONDeserializer<?> deserializer = GlobalSerializeMapping.getDeserializer(targetType);
|
||||||
if(null != deserializer) {
|
if(null != deserializer) {
|
||||||
return (T) deserializer.deserialize((JSON)value);
|
//noinspection unchecked
|
||||||
|
return (T) deserializer.deserialize((JSON) value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -111,5 +131,4 @@ public class JSONConverter implements Converter<JSON> {
|
|||||||
public JSON convert(Object value, JSON defaultValue) throws IllegalArgumentException {
|
public JSON convert(Object value, JSON defaultValue) throws IllegalArgumentException {
|
||||||
return JSONUtil.parse(value);
|
return JSONUtil.parse(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ public class JSONSupport implements JSONString, JSONBeanParser<JSON> {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void parse(JSON json) {
|
public void parse(JSON json) {
|
||||||
final JSONSupport support = json.toBean(this.getClass());
|
final JSONSupport support = JSONConverter.jsonToBean(getClass(), json, false);
|
||||||
BeanUtil.copyProperties(support, this);
|
BeanUtil.copyProperties(support, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,37 @@
|
|||||||
|
package cn.hutool.json;
|
||||||
|
|
||||||
|
import cn.hutool.core.lang.Console;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class JSONSupportTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void parseTest() {
|
||||||
|
String jsonstr = "{\n" +
|
||||||
|
" \"location\": \"http://www.bejson.com\",\n" +
|
||||||
|
" \"message\": \"这是一条测试消息\",\n" +
|
||||||
|
" \"requestId\": \"123456789\",\n" +
|
||||||
|
" \"traceId\": \"987654321\"\n" +
|
||||||
|
"}";
|
||||||
|
|
||||||
|
|
||||||
|
final TestBean testBean = JSONUtil.toBean(jsonstr, TestBean.class);
|
||||||
|
Console.log(testBean);
|
||||||
|
}
|
||||||
|
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@Data
|
||||||
|
static class TestBean extends JSONSupport{
|
||||||
|
|
||||||
|
private String location;
|
||||||
|
|
||||||
|
private String message;
|
||||||
|
|
||||||
|
private String requestId;
|
||||||
|
|
||||||
|
private String traceId;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user