mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-04 12:47:59 +08:00
fix bug
This commit is contained in:
parent
afc4b2b213
commit
d58cda4ff7
@ -56,7 +56,8 @@ public class JSONConverter implements Converter<JSON> {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* JSON递归转换<br>
|
* JSON递归转换<br>
|
||||||
* 首先尝试JDK类型转换,如果失败尝试JSON转Bean
|
* 首先尝试JDK类型转换,如果失败尝试JSON转Bean<br>
|
||||||
|
* 如果遇到{@link JSONBeanParser},则调用其{@link JSONBeanParser#parse(Object)}方法转换。
|
||||||
*
|
*
|
||||||
* @param <T> 转换后的对象类型
|
* @param <T> 转换后的对象类型
|
||||||
* @param targetType 目标类型
|
* @param targetType 目标类型
|
||||||
@ -98,6 +99,7 @@ public class JSONConverter implements Converter<JSON> {
|
|||||||
* @param ignoreError 是否忽略转换错误
|
* @param ignoreError 是否忽略转换错误
|
||||||
* @return 目标类型的值
|
* @return 目标类型的值
|
||||||
* @throws ConvertException 转换失败
|
* @throws ConvertException 转换失败
|
||||||
|
* @since 5.7.10
|
||||||
*/
|
*/
|
||||||
protected static <T> T jsonToBean(Type targetType, Object value, boolean ignoreError) throws ConvertException {
|
protected static <T> T jsonToBean(Type targetType, Object value, boolean ignoreError) throws ConvertException {
|
||||||
if (JSONUtil.isNull(value)) {
|
if (JSONUtil.isNull(value)) {
|
||||||
|
@ -1,16 +1,21 @@
|
|||||||
package cn.hutool.json;
|
package cn.hutool.json;
|
||||||
|
|
||||||
import cn.hutool.core.lang.Console;
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class JSONSupportTest {
|
public class JSONSupportTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* https://github.com/dromara/hutool/issues/1779
|
||||||
|
* 在JSONSupport的JSONBeanParse中,如果使用json.toBean,会导致JSONBeanParse.parse方法反复递归调用,最终栈溢出<br>
|
||||||
|
* 因此parse方法默认实现必须避开JSONBeanParse.parse调用。
|
||||||
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void parseTest() {
|
public void parseTest() {
|
||||||
String jsonstr = "{\n" +
|
String jsonstr = "{\n" +
|
||||||
" \"location\": \"http://www.bejson.com\",\n" +
|
" \"location\": \"https://hutool.cn\",\n" +
|
||||||
" \"message\": \"这是一条测试消息\",\n" +
|
" \"message\": \"这是一条测试消息\",\n" +
|
||||||
" \"requestId\": \"123456789\",\n" +
|
" \"requestId\": \"123456789\",\n" +
|
||||||
" \"traceId\": \"987654321\"\n" +
|
" \"traceId\": \"987654321\"\n" +
|
||||||
@ -18,7 +23,10 @@ public class JSONSupportTest {
|
|||||||
|
|
||||||
|
|
||||||
final TestBean testBean = JSONUtil.toBean(jsonstr, TestBean.class);
|
final TestBean testBean = JSONUtil.toBean(jsonstr, TestBean.class);
|
||||||
Console.log(testBean);
|
Assert.assertEquals("https://hutool.cn", testBean.getLocation());
|
||||||
|
Assert.assertEquals("这是一条测试消息", testBean.getMessage());
|
||||||
|
Assert.assertEquals("123456789", testBean.getRequestId());
|
||||||
|
Assert.assertEquals("987654321", testBean.getTraceId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@EqualsAndHashCode(callSuper = true)
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
Loading…
Reference in New Issue
Block a user