I get user input which I need to store in a database field of the JSON-type. This can be anything and may be anything as long as it is valid JSON. In JSON.net there is a way to validate JSON against a JSON scheme but that is too specific.
Now I have to deserialize the JSON string and use a try-catch for this and I rather do a simple:
JsonTextReader rJSonReader = new JsonTextReader(new StringReader(@sCellValue));
if (rJSonReader.isValid()) {#more code here;}
This would be much cleaner I think. Or am I wrong?
Here is a library that encapsulates the complexity of parsing JSON objects, while allowing you the flexibility to validate its properties by simply reading from a
NameValueCollection
.Subclass the
Deserialiser<T>
class and loop through the JSON properties in theNameValueCollection
returned as a result of callingDeserialise()
in your implementation. You can effectively introduce your own custom validation by determining whether or not each expected property is present in the collection:Leveraging this, you don't need to worry about parsing the JSON object, and can focus on validating the result instead. I'm happy to walk through this in more detail.