I would like to efficiently deserialize JSON object which contains a nested array of JSON objects.
For example, a JSON directory listing could contain a root JSON file system object with a recursive array of file system objects.
{
"bytes": 0,
"path": "/Public",
"is_dir": true,
"contents": [
{
"bytes": 0,
"modified": "Mon, 18 Jul 2011 20:13:43 +0000",
"path": "/Public/1.txt",
"is_dir": false
},
{
"bytes": 0,
"modified": "Mon, 18 Jul 2011 20:13:43 +0000",
"path": "/Public/2.txt",
"is_dir": false
}
]
}
The corresponding C# class would look something like this
class JsonFileInfo
{
public string Path;
public long Bytes;
public string Modified;
public bool IsDir;
public List<JsonFileInfo> Contents;
}
I've tried the .NET DataContractJsonSerializer e.g.
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(RainstormFileInfo));
JsonFileInfo jfi = (JsonFileInfo)serializer.ReadObject(responseStream);
and I've also tried the JSON.NET JsonSerializer + JsonTextReader e.g.
JsonReader reader = new JsonTextReader(new StreamReader(responseStream))
JsonSerializer serializer = new JsonSerializer();
JsonFileInfo jfi = serializer.Deserialize<JsonFileInfo>(reader);
but both frameworks read the entire contents
JSON sub-array into memory in one fell swoop of deserialization.
How can I read this JSON result from a stream one JsonFileInfo object at a time?