From f9b4c79c30108f2150cd883a0f41f62ed6c1b035 Mon Sep 17 00:00:00 2001 From: Colm O hEigeartaigh Date: Wed, 16 Nov 2022 15:36:53 +0000 Subject: [PATCH] Fixing StackOverflow error --- .../java/org/codehaus/jettison/json/JSONObject.java | 2 +- .../org/codehaus/jettison/json/JSONObjectTest.java | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/codehaus/jettison/json/JSONObject.java b/src/main/java/org/codehaus/jettison/json/JSONObject.java index 0c15d74..541cf72 100644 --- a/src/main/java/org/codehaus/jettison/json/JSONObject.java +++ b/src/main/java/org/codehaus/jettison/json/JSONObject.java @@ -268,7 +268,7 @@ public JSONObject(Map map) { if (v instanceof Collection) { myHashMap.put(entry.getKey(), new JSONArray((Collection) v)); } - if (v instanceof Map) { + if (v instanceof Map && v != map) { myHashMap.put(entry.getKey(), new JSONObject((Map) v)); } } diff --git a/src/test/java/org/codehaus/jettison/json/JSONObjectTest.java b/src/test/java/org/codehaus/jettison/json/JSONObjectTest.java index acd8246..1026cea 100644 --- a/src/test/java/org/codehaus/jettison/json/JSONObjectTest.java +++ b/src/test/java/org/codehaus/jettison/json/JSONObjectTest.java @@ -2,6 +2,11 @@ import junit.framework.TestCase; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + public class JSONObjectTest extends TestCase { public void testEquals() throws Exception { JSONObject aJsonObj = new JSONObject("{\"x\":\"y\"}"); @@ -148,4 +153,10 @@ public void testMalformedArray() throws Exception { } } + // https://github.com/jettison-json/jettison/issues/52 + public void testIssue52() throws Exception { + Map map = new HashMap<>(); + map.put("t",map); + new JSONObject(map); + } }