4

I have an HttpResponseMessage whose Content property I know to be of type Exception, or some subclass thereof. Now I'm trying to deserialize that content like so:

var exception = httpResponseMessage.Content.ReadAsAsync<Exception>().Result;

But when I do this, it throws an AggregateException with the message

System.AggregateException: One or more errors occurred. ---> Newtonsoft.Json.JsonReaderException: Error parsing boolean value. Path '', line 0, position 0.

If I view the content as a string, I can see from the Json that it's of type `DivideByZeroException':

{"ClassName":"System.DivideByZeroException","Message":"Attempted to divide by zero.","Data": ... etc...

I'd be happy to get back an Exception object - but does ReadAsAsync<> not support deserializing an object into its base class type? If not, then what's the best way to detect what type of object I should be creating?

Shaul Behr
  • 36,951
  • 69
  • 249
  • 387
  • Check if this might help you: http://stackoverflow.com/questions/8738031/deserializing-json-using-json-net-with-dynamic-data – Barnstokkr May 19 '15 at 09:01
  • @Barnstokkr yes, I just came to the same conclusion independently, to use dynamic type. Funnily enough, this does not solve the `ReadAsAsync<>` error; I worked around it by using the `JsonConvert.DeserializeObject()` method instead. Feel free to post this as an answer, anyway. Thanks! – Shaul Behr May 19 '15 at 09:12

0 Answers0