8

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?

1
  • 1
    Probably not, unless the 'converter' allows you attach events like the XmlSerializer for unresolved entities/attributes. Commented Apr 22, 2010 at 11:19

1 Answer 1

5

Json.NET supports JSON Schema. You could create a schema with all the required properties marked and validate incoming JSON against it before deserializing.

Sign up to request clarification or add additional context in comments.

2 Comments

How would you do this?
Is there no way to just validate that the schema is valid JSON?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.