I have a JSON response from a web service, I want to convert it to a List<Foo>
. I know I can do this several ways, but the only way that supports generics that I can see in the JSON.NET documentation is from a string
.
The response is potentially large, so I don't really want to read it all into memory, then convert it to my object.
I've tried:
using (var response = request.GetResponse())
{
using (var responseStream = response.GetResponseStream())
{
if (responseStream == null) throw new Exception("ResponseStream is null!!!");
using (var reader = new StreamReader(responseStream))
{
using (var jsonReader = new JsonTextReader(reader))
{
// var foos = JsonConvert.DeserializeObject<List<Foo>>() -- Requires a string
// var foos = new JsonConverter<List<Foo>>().ReadJson()... -- Non generic
}
}
}
}
Can I read it directly from the stream into a List<Foo>
without having to do this?