I'm migrating data from an old properietary object database format using JSON as the intermediate format. The objects are output into a JSON array of objects, each of which has an initial field giving the type of the original object followed by field called Instance which has the nested original object.
I need to stream these in as there are potentially hundreds of thousands of them - I can't just read the whole JSON array into memory and then process it.
So the JSON looks like this:
[
{
"Type": "Foo",
"Instance": {
// instance of Foo type
}
},
{
"Type": "Bar",
"Instance": {
// instance of Bar type
}
},
// tens or hundreds of thousands more objects...
]
Using Json.NET, what's the best way to stream in one array element at a time, access the "Type" property and then deserialize the "Instance" to a .Net object of the appropriate type?
Edit: although there is a similar question regarding reading a large JSON array, the specifics of accessing the instance are not answered in that question.