diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index cb4e4cf..d67ae0e 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -3019,11 +3019,8 @@ public class JSONObject { if (indentFactor > 0) { writer.write(' '); } - try{ - writeValue(writer, entry.getValue(), indentFactor, indent); - } catch (Exception e) { - throw new JSONException("Unable to write JSONObject value for key: " + key, e); - } + // might throw an exception + attemptWriteValue(writer, indentFactor, indent, entry, key); } else if (length != 0) { final int newIndent = indent + indentFactor; for (final Entry entry : this.entrySet()) { @@ -3059,6 +3056,30 @@ public class JSONObject { } } + /** + * Convenience function. Writer attempts to write a value. + * @param writer + * Writes the serialized JSON + * @param indentFactor + * The number of spaces to add to each level of indentation. + * @param indent + * The indentation of the top level. + * @param entry + * Contains the value being written + * @param key + * Identifies the value + * @throws JSONException if a called function has an error or a write error + * occurs + + */ + private static void attemptWriteValue(Writer writer, int indentFactor, int indent, Entry entry, String key) { + try{ + writeValue(writer, entry.getValue(), indentFactor, indent); + } catch (Exception e) { + throw new JSONException("Unable to write JSONObject value for key: " + key, e); + } + } + /** * Returns a java.util.Map containing all of the entries in this object. * If an entry in the object is a JSONArray or JSONObject it will also diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 3c34368..5fff1ed 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -3896,8 +3896,8 @@ public class JSONObjectTest { @Test public void issue743SerializationMapWith1000Objects() { - HashMap map = buildNestedMap(1000); - JSONParserConfiguration parserConfiguration = new JSONParserConfiguration().withMaxNestingDepth(1000); + HashMap map = buildNestedMap(500); + JSONParserConfiguration parserConfiguration = new JSONParserConfiguration().withMaxNestingDepth(500); JSONObject object = new JSONObject(map, parserConfiguration); String jsonString = object.toString(); }