This commit is contained in:
Looly 2021-02-03 13:33:31 +08:00
parent 1a7812a281
commit 0056560a93
5 changed files with 42 additions and 21 deletions

View File

@ -10,6 +10,7 @@
### Bug修复 ### Bug修复
* 【json 】 JSONUtil.isJson方法改变trim策略解决特殊空白符导致判断失败问题 * 【json 】 JSONUtil.isJson方法改变trim策略解决特殊空白符导致判断失败问题
* 【json 】 修复SQLEXception导致的栈溢出issue#1401@Github
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------

View File

@ -605,8 +605,14 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
} else { } else {
throw new JSONException("JSONArray initial value should be a string or collection or array."); throw new JSONException("JSONArray initial value should be a string or collection or array.");
} }
Object next;
while (iter.hasNext()) { while (iter.hasNext()) {
this.add(iter.next()); next = iter.next();
// 检查循环引用
if(next != source){
this.add(next);
}
} }
} }
} }

View File

@ -24,7 +24,6 @@ import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
import java.util.Collection; import java.util.Collection;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.ResourceBundle; import java.util.ResourceBundle;
@ -653,7 +652,7 @@ public class JSONObject implements JSON, JSONGetter<String>, Map<String, Object>
return; return;
} }
if(ArrayUtil.isArray(source) || source instanceof Iterable || source instanceof Iterator){ if(ArrayUtil.isArray(source)){
// 不支持集合类型转换为JSONObject // 不支持集合类型转换为JSONObject
throw new JSONException("Unsupported type [{}] to JSONObject!", source.getClass()); throw new JSONException("Unsupported type [{}] to JSONObject!", source.getClass());
} }

View File

@ -21,6 +21,7 @@ import java.io.StringWriter;
import java.io.Writer; import java.io.Writer;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.sql.SQLException;
import java.time.temporal.TemporalAccessor; import java.time.temporal.TemporalAccessor;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
@ -735,6 +736,11 @@ public final class JSONUtil {
} }
try { try {
// fix issue#1401@Github
if(object instanceof SQLException){
return object.toString();
}
// JSONArray // JSONArray
if (object instanceof Iterable || ArrayUtil.isArray(object)) { if (object instanceof Iterable || ArrayUtil.isArray(object)) {
return new JSONArray(object, jsonConfig); return new JSONArray(object, jsonConfig);

View File

@ -11,6 +11,7 @@ import cn.hutool.json.test.bean.UserC;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -179,4 +180,12 @@ public class JSONUtilTest {
" \"test\": \"\\\\地库地库\",\n" + " \"test\": \"\\\\地库地库\",\n" +
"}"); "}");
} }
@Test
public void sqlExceptionTest(){
//https://github.com/looly/hutool/issues/1401
// SQLException实现了Iterable接口默认是遍历之会栈溢出修正后只返回string
final JSONObject set = JSONUtil.createObj().set("test", new SQLException("test"));
Assert.assertEquals("{\"test\":\"java.sql.SQLException: test\"}", set.toString());
}
} }