Possible Duplicate:
Incremental JSON Parsing in C#
The following questions are related but don't address (at least directly) the problem I'm trying to solve:
- Load JSON data stream from text file into objects C#
- deserializing JSON to .net object using NewtonSoft (or linq to json maybe?)
I am trying to deserialize potentially a very large JSON data using Json.NET. Instead of loading the entire file into memory and parse the JSON using JObject.Parse(JsonFullString)
, I wish to read from the stream token by token and construct the object graph. I would appreciate any suggestion on how to implement deserialization from stream.
Note: My intent is to replace the following code with better implementation
string jsonData = string.Empty;
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
jsonData = ASCIIEncoding.ASCII.GetString(ms.ToArray());
}
JObject jObject = JObject.Parse(jsonData);
var entities = from e in jObject.Root
select e;