0

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?

BanksySan
  • 27,362
  • 33
  • 117
  • 216
  • Please check the answer with the more votes. This is what you need, the method `DeserializeFromStream`. Then you can cast the result to the type you have mentioned, `List`. – Christos Nov 17 '18 at 14:46
  • @Christos I know I can do that, I wanted to know if there was a method that supported generics. That answer simply returns an `object`. – BanksySan Nov 17 '18 at 15:06
  • According to this post, there isn't _anymore_ such a method. If you want I can reopen the post and wait for an answer. Please just let me know. Thanks – Christos Nov 17 '18 at 15:10

0 Answers0