Closed carlos-mg89 closed 6 years ago
I have tried using the GSON package to do the same that I was doing with Minimal JSON, and even though I was forced to perform a careful parsing of the JSON file, it allowed me to read the JSON file, without having the OutOfMemory issues, since I only wanted to read the file to do some processing, which could be done without having to store all the JSON values. Actually, what I wanted is to parse it to store it later in a SQLite database, and thanks to a real buffered reading like GSON offers it was possible to do so.
Example:
val inputStream: InputStream? = assets.open(fileName)
val inputStreamReader: InputStreamReader? = InputStreamReader(inputStream)
val jsonReader = JsonReader(inputStreamReader)
jsonReader.beginArray()
while (jsonReader.hasNext()) {
// Do the manual parsing here, however it will be with a real buffered reader, so you won't incur on OutOfMemory exceptions
}
jsonReader.endArray()
jsonReader.close()
Hi and thanks for sharing your work,
I'm writing this issue since I'm a bit confused when using the minimal json package due to an OutOfMemory error.
I'm using it in an Android project to read a sort of big (10MB) JSON file. And to do that, I thought using a Stream would be the appropriate way, in order to avoid memory issues, but it doesn't seem to be the case. Or at least, I don't know how to make it work like that.
This is my code for reading the JSON file and storing the result in a variable (jsonValue):
This code is working on my phone and on an Android Virtual Device, however in some devices is not working due to OutOfMemory errors.
I had understood that to avoid them when reading the JSON file, a Stream reader had to be used, and I'm using an InputStreamReader. Perhaps I'm getting the error since I'm trying to return the parsed JSON and the allocation is the reason of the OutOfMemory error? If that's the case, then I imagine I should be processing the results as I read the JSON file. And store it, for example, in an SQLite database (that is stored in disk and not in memory).
This is the error log:
Is that true? Or am I doing something wrong related on the way that I'm parsing the JSON with the MinimalJSON library?
Thanks in advance