This commit is contained in:
Looly 2021-08-22 16:06:00 +08:00
parent 1e5f5dc031
commit afc4b2b213
4 changed files with 62 additions and 5 deletions

View File

@ -3,7 +3,7 @@
-------------------------------------------------------------------------------------------------------------
# 5.7.10 (2021-08-19)
# 5.7.10 (2021-08-22)
### 🐣新特性
* 【core 】 增加NamingCase类
@ -18,6 +18,7 @@
* 【core 】 修复MapUtil.sort比较器不一致返回原map的问题issue#I46AQJ@Gitee
### 🐞Bug修复
* 【core 】 修复JSONSupport默认循环引用导致的问题issue#1779@Github
-------------------------------------------------------------------------------------------------------------

View File

@ -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);
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 {
return JSONUtil.parse(value);
}
}

View File

@ -26,7 +26,7 @@ public class JSONSupport implements JSONString, JSONBeanParser<JSON> {
*/
@Override
public void parse(JSON json) {
final JSONSupport support = json.toBean(this.getClass());
final JSONSupport support = JSONConverter.jsonToBean(getClass(), json, false);
BeanUtil.copyProperties(support, this);
}

View File

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