Validating result of JsonConvert.DeserializeObject (think "try parse") using JSON.Net

Posted by Riri on Stack Overflow See other posts from Stack Overflow or by Riri
Published on 2010-04-22T11:07:15Z Indexed on 2010/04/23 3:13 UTC
Read the original article Hit count: 871

Filed under:
|
|

I have incoming messages that I need to try and parse in my own objects structure. SOme of these are well formed JSON obejcts and some are just nonsense.

I use JsonConvert.DeserializeObject<MyObject>(incmoingString); to do this. This however sometimes gives me a exception when the incoming is total garbage. Other times I get a non-complete object structure when the incoming string is kind of OK - and finally it sometimes work.

I've wrapped the conversion in a try/catch and than manually validate that I've gotten the properties I need to the deserialized result.

Is there a better way to do this?

© Stack Overflow or respective owner

Related posts about json.net

Related posts about c#