Schema validation before Deserialize JSON object

Suppose we have below class:

public class TestResultInBound {
[JsonProperty ("supplier_code")]
public string SupplierCode { get; set; }
[JsonProperty ("supplier_name")]
public string SupplierName { get; set; }
[JsonProperty ("commodity_name")]
public string CommodityName { get; set; }
[JsonProperty ("site")]
public string Site { get; set; }
[JsonProperty("unit")]
public string Unit { get; set; }
[JsonProperty ("test_value")]
public string TestValue { get; set; }
[JsonProperty ("test_result")]
public string TestResult { get; set; }
}

We would like to write some API that accept the JSON string from vendor and transfer into our database and so on.

Image that we have below case:

// Case1
var targetJson = "{\"supplier_code\":\"123456\",\"supplier_name\":\"LL\",\"commodity_name\":null,\"site\":\"MARS\",\"unit\":null,\"test_value\":null,\"test_result\":null}";
var toObj1 = JsonConvert.DeserializeObject<TestResultInBound>(targetJson);// Case2
var correctJson = "{\"A\":1}";
var toObj2 = JsonConvert.DeserializeObject<TestResultInBound>(correctJson);
// Case3
var incorrectJson = "{A=1}";
var toObj3 = JsonConvert.DeserializeObject<TestResultInBound>(incorrectJson);

What should we expected the result after we execute DeserializeObject through Json.NET?

For case 1 was easiest one that we got expected Object what we want.

Result of Case 1

So what do you guess for the result of Case2 & Case 3? All failed? The answers would be Case2 return “TestResultInBound” but with all null property without error and Case3 throw JsonReaderException.

Result of Case 2
Result of Case 3

So far For the Case2&3, we would like to validate the input string in the early stage and response to the vendors in a clever way. This will also avoiding causing further exceptions while acting with database or performing business logic…

The common way we should validate our input according JSON schema.

Json.NET Schema do have ability to achieve the validation functionality for us. But it not for free. So that we could considerate open source project: NJsonSchema for alternative.

var invaliodtJson = "{\"supplier_\":\"123456\",\"supplier_name\":\"LL\",\"commodity_name\":null,\"site\":\"MARS\",\"test_value\":null,\"test_result\":null}";// create schema according class
var schema = NJsonSchema.JsonSchema.FromType<TestResultInBound>();
// do validation
var errors = schema.Validate(invaliodtJson);
// if pass errors list remain count = 0,
// otherwise will indicate where the errors detected
if (errors.Count() > 0){
Console.WriteLine($"Format of JSON input not acceptable. See ref:{Environment.NewLine}{JsonConvert.SerializeObject(errors)}");
}

Format of JSON input not acceptable. See ref:
[{“Kind”:39,”Property”:”supplier_”,”Path”:”#/supplier_”,”HasLineInfo”:true,”LineNumber”:1,”LinePosition”:13,”Schema”:{“title”:”TestResultInBound”,”type”:”object”,”additionalProperties”:false,”properties”:{“supplier_code”:{“type”:[“null”,”string”]},”supplier_name”:{“type”:[“null”,”string”]},”commodity_name”:{“type”:[“null”,”string”]},”site”:{“type”:[“null”,”string”]},”Unit”:{“type”:[“null”,”string”]},”test_value”:{“type”:[“null”,”string”]},”test_result”:{“type”:[“null”,”string”]}}}}]

As we could see that above errors result will give us direction why format not pass by the validation process. That’s all 😺.

Reference

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store