mgkarakaya / json-simple

Automatically exported from code.google.com/p/json-simple
Apache License 2.0
0 stars 0 forks source link

toJSONString crahses for large number of objects in JSONArray #59

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Build jsonObject containing JSONArray with around 2000 json objects [in 
short large number of objects in JSONArray] 
2. Convert it into string using toJSONString of main jsonObject
3. it crashes on Android 

What is the expected output? What do you see instead?
It should not crash

What version of the product are you using? On what operating system?

Please provide any additional information below.
11-07 12:20:48.180: ERROR/dalvikvm-heap(21506): Out of memory on a 2235124-byte 
allocation.
11-07 12:21:11.420: ERROR/AndroidRuntime(21506): FATAL EXCEPTION: Thread-44
11-07 12:21:11.420: ERROR/AndroidRuntime(21506): java.lang.OutOfMemoryError
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at 
java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:96)
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at 
java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:147)
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at 
java.lang.StringBuffer.append(StringBuffer.java:219)
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at 
java.io.StringWriter.write(StringWriter.java:148)
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at 
org.json.simple.JSONObject.writeJSONString(JSONObject.java:75)
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at 
org.json.simple.JSONObject.writeJSONString(JSONObject.java:86)
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at 
org.json.simple.JSONValue.writeJSONString(JSONValue.java:153)
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at 
org.json.simple.JSONObject.writeJSONString(JSONObject.java:78)
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at 
org.json.simple.JSONObject.writeJSONString(JSONObject.java:86)
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at 
org.json.simple.JSONValue.writeJSONString(JSONValue.java:153)
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at 
org.json.simple.JSONArray.writeJSONString(JSONArray.java:62)
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at 
org.json.simple.JSONArray.writeJSONString(JSONArray.java:70)
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at 
org.json.simple.JSONValue.writeJSONString(JSONValue.java:153)
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at 
org.json.simple.JSONObject.writeJSONString(JSONObject.java:78)
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at 
org.json.simple.JSONObject.writeJSONString(JSONObject.java:86)
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at 
org.json.simple.JSONValue.writeJSONString(JSONValue.java:153)
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at 
org.json.simple.JSONObject.writeJSONString(JSONObject.java:78)
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at 
org.json.simple.JSONObject.writeJSONString(JSONObject.java:86)
11-07 12:21:11.420: ERROR/AndroidRuntime(21506):     at 
com.myapp.jsonToString(abc.java:61)

Original issue reported on code.google.com by vishalghadge on 7 Nov 2011 at 7:22

GoogleCodeExporter commented 8 years ago
tried following code.. which was more efficient but it also crashes for more 
objects.

final Writer writer = new StringWriter();

        try
        {
            json.writeJSONString(writer);
        }
        catch ( final IOException e )
        {
            if ( QOCCompileConstants.LOGGING )
            {
                LOG.log(Level.ERROR, "IO error" + e.getMessage());
            }
        }
        return writer.toString();

Original comment by vishalghadge on 7 Nov 2011 at 7:25

GoogleCodeExporter commented 8 years ago
Please make sure you that you allocate sufficient memory for JVM, or use the 
streaming approach (what you mentioned in the comment):
http://code.google.com/p/json-simple/wiki/EncodingExamples#Example_2-4_-_Encode_
a_JSON_array_-_Using_List_and_streaming

Please note that a StringWriter still stores JSON output in the memory. Please 
use something like FileWriter or your own writer implementation.

Original comment by fangyid...@gmail.com on 29 Nov 2011 at 2:50

GoogleCodeExporter commented 8 years ago
With respect, I don't think this is really a bug with json.simple; trying to 
create a String that's too big to fit in available memory by any means will 
cause the same problem.

Original comment by jon.cham...@gmail.com on 10 Aug 2013 at 3:49