From 72f7a829651722a94ecfbeb856327b9088a9e82a Mon Sep 17 00:00:00 2001 From: yanxutao89 <910135896@qq.com> Date: Sun, 17 Mar 2024 17:14:49 +0800 Subject: [PATCH] fix add message tip when OutOfMemory occurred for issue #2323 --- .../alibaba/fastjson2/JSONWriterJSONB.java | 2 +- .../alibaba/fastjson2/JSONWriterUTF16.java | 2 +- .../com/alibaba/fastjson2/JSONWriterUTF8.java | 2 +- .../fastjson2/issues_2300/Issue2323.java | 51 +++++++++++++++++++ 4 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 core/src/test/java/com/alibaba/fastjson2/issues_2300/Issue2323.java diff --git a/core/src/main/java/com/alibaba/fastjson2/JSONWriterJSONB.java b/core/src/main/java/com/alibaba/fastjson2/JSONWriterJSONB.java index 0d8ebcf3ff..491b0df210 100644 --- a/core/src/main/java/com/alibaba/fastjson2/JSONWriterJSONB.java +++ b/core/src/main/java/com/alibaba/fastjson2/JSONWriterJSONB.java @@ -853,7 +853,7 @@ void ensureCapacity(int minCapacity) { newCapacity = minCapacity; } if (newCapacity > maxArraySize) { - throw new OutOfMemoryError(); + throw new OutOfMemoryError("try enabling LargeObject feature instead"); } // minCapacity is usually close to size, so this is a win: diff --git a/core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF16.java b/core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF16.java index 3a46e6dead..3a04821c85 100644 --- a/core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF16.java +++ b/core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF16.java @@ -1970,7 +1970,7 @@ final void ensureCapacity(int minCapacity) { newCapacity = minCapacity; } if (newCapacity - maxArraySize > 0) { - throw new OutOfMemoryError(); + throw new OutOfMemoryError("try enabling LargeObject feature instead"); } // minCapacity is usually close to size, so this is a win: diff --git a/core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF8.java b/core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF8.java index 915945a19c..682e5e95f1 100644 --- a/core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF8.java +++ b/core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF8.java @@ -1996,7 +1996,7 @@ final void ensureCapacity(int minCapacity) { newCapacity = minCapacity; } if (newCapacity - maxArraySize > 0) { - throw new OutOfMemoryError(); + throw new OutOfMemoryError("try enabling LargeObject feature instead"); } // minCapacity is usually close to size, so this is a win: diff --git a/core/src/test/java/com/alibaba/fastjson2/issues_2300/Issue2323.java b/core/src/test/java/com/alibaba/fastjson2/issues_2300/Issue2323.java new file mode 100644 index 0000000000..2f04edf20c --- /dev/null +++ b/core/src/test/java/com/alibaba/fastjson2/issues_2300/Issue2323.java @@ -0,0 +1,51 @@ +package com.alibaba.fastjson2.issues_2300; + +import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONWriter; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.Map; + +public class Issue2323 { + String errMsg = "try enabling LargeObject feature instead"; + + @Test + public void test() throws Exception { + int size = 50; + + List dataList2 = Lists.newArrayList(); + for (int rowIndex = 1; rowIndex <= 50000; rowIndex++) { + JSONObject data = new JSONObject(); + for (int i = 0; i < size; i++) { + data.put("test" + i, "testdata"); + } + dataList2.add(data); + } + + Map params = Maps.newHashMap(); + params.put("dataListStr", JSON.toJSONString(dataList2)); + + try { + JSONWriter.ofUTF16().write(params); + } catch (OutOfMemoryError error) { + Assertions.assertEquals(errMsg, error.getMessage()); + } + + try { + JSONWriter.ofUTF8().write(params); + } catch (OutOfMemoryError error) { + Assertions.assertEquals(errMsg, error.getMessage()); + } + + try { + JSONWriter.ofJSONB().write(params); + } catch (OutOfMemoryError error) { + Assertions.assertEquals(errMsg, error.getMessage()); + } + } +}