mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add exception for json
This commit is contained in:
24
hutool-json/src/test/java/cn/hutool/json/Issue2746Test.java
Executable file
24
hutool-json/src/test/java/cn/hutool/json/Issue2746Test.java
Executable file
@@ -0,0 +1,24 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class Issue2746Test {
|
||||
|
||||
@Test
|
||||
public void parseObjTest() {
|
||||
final String str = StrUtil.repeat("{", 1500) + StrUtil.repeat("}", 1500);
|
||||
try{
|
||||
JSONUtil.parseObj(str);
|
||||
} catch (final JSONException e){
|
||||
Assert.assertTrue(e.getMessage().startsWith("A JSONObject can not directly nest another JSONObject or JSONArray"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = JSONException.class)
|
||||
public void parseTest() {
|
||||
final String str = StrUtil.repeat("[", 1500) + StrUtil.repeat("]", 1500);
|
||||
JSONUtil.parseArray(str);
|
||||
}
|
||||
}
|
37
hutool-json/src/test/java/cn/hutool/json/Issue2749Test.java
Executable file
37
hutool-json/src/test/java/cn/hutool/json/Issue2749Test.java
Executable file
@@ -0,0 +1,37 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* https://github.com/dromara/hutool/issues/2749
|
||||
* <p>
|
||||
* 由于使用了递归方式解析和写出,导致JSON太长的话容易栈溢出。
|
||||
*/
|
||||
public class Issue2749Test {
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void jsonObjectTest() {
|
||||
final Map<String, Object> map = new HashMap<>(1, 1f);
|
||||
Map<String, Object> node = map;
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
//noinspection unchecked
|
||||
node = (Map<String, Object>) node.computeIfAbsent("a", k -> new HashMap<String, Object>(1, 1f));
|
||||
}
|
||||
node.put("a", 1);
|
||||
final String jsonStr = JSONUtil.toJsonStr(map);
|
||||
|
||||
@SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
|
||||
final JSONObject jsonObject = new JSONObject(jsonStr);
|
||||
Assert.assertNotNull(jsonObject);
|
||||
|
||||
// 栈溢出
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
jsonObject.toString();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user