I will be retrieving multiple blocks of json data over time and deserializing them using Json.NET. I have objects created that mirror the structure of each different block of data, but when each block is received I will not be able to detect the type.
I would like to avoid attempting deserialization for each type, catching the failures (when exceptions are thrown) and moving on to the next type. Doing so would obviously be bad for performance.
Is there a fast and efficient way to determine the structure of a json message and deserialize it? Would the only way to use (JObject)JsonConvert.DeserializeObject(...) then drill into the structure to check if certain children exist, then using JsonConvert.DeserializeObject<T>(...)? It seems deserializing the object twice is inefficient.
I had the impression, even before I posted the question, that there wasn’t an obviously more elegant way to handle this scenario. I’ve decided to do the double deserialization. I don’t think it’s ideal, but I guess there isn’t a better/cleaner way out there yet.
Thanks for your answers.