I am doing some testing with NewtonSoft Json.Net and am running into some issues with large Json datasets.
I have a dataset which is 400MB in size and it seems that no matter how I parse it, I keep getting out of memory exceptions. I tried the standard Parse method and tried the streamreader method where I am parsing the tokens. Either way, at some point, I end up with a memory exception.
The stack trace indicates the problem is centered around the use of Int32 to index the document (see below). I thought using JsonTextReader would solve that problem, but it doesn't appear to be the case.
Here is my stream request from the server:
public Task<Stream> GetAsync(string command)
{
Task<Stream> promise;
using (var handler = new HttpClientHandler { Credentials = new NetworkCredential(username, password) })
{
var client = new HttpClient(handler);
promise = client.GetStreamAsync(command);
}
return promise;
}
Then, I start the work in this way:
using (Stream s = task.GetAwaiter().GetResult())
using (StreamReader sr = new StreamReader(s))
using (JsonReader reader = new JsonTextReader(sr))
{
while (reader.Read())
{
if( reader.TokenType == JsonToken.PropertyName && reader.Value.ToString() == "Values" ) {
reader.Read();
if( reader.TokenType != JsonToken.StartArray )
break;
while( reader.Read() ) {
// Do more parsing;
}
}
}
}
Is there some configuration in Json.net I need to set to avoid this problem? Or do I need to break down the response from the server to smaller chunks? If so how do I deal with partial json?
Here is the stack trace:
at System.String.CtorCharArrayStartLength(Char[] value, Int32 startIndex, Int32 length)
at Newtonsoft.Json.Utilities.StringReference.ToString()
at Newtonsoft.Json.JsonTextReader.ParseString(Char quote)
at Newtonsoft.Json.JsonTextReader.ParseValue()
at Newtonsoft.Json.JsonTextReader.ReadInternal()
at Newtonsoft.Json.JsonTextReader.Read()