开发者

Validate JSON against a schema in .NET

开发者 https://www.devze.com 2022-12-26 11:15 出处:网络
I know there is a proposed standard for JSON schema validation, is the开发者_Python百科re an implementation in .NET?A free and open-source alternative to Json.NET is NJsonSchema (JSON Schema draft 4).

I know there is a proposed standard for JSON schema validation, is the开发者_Python百科re an implementation in .NET?


A free and open-source alternative to Json.NET is NJsonSchema (JSON Schema draft 4).


Json.NET has this functionality.


Add Newtonsoft's Json NuGet Package in your solution. Add below function and pass Schema and your json response in string to below function.

  public void ValidateSchema(JsonSchema JSchema, string JsonString)  {
        JsonString = JsonString.Replace("\"", "'");
        var ArrJobj = JArray.Parse(JsonString);

        foreach (JObject jo in ArrJobj)
        {
            if (!jo.IsValid(JSchema)) throw new Exception("Schems Validation failed");

        }

    }

Hope this helps


  • Json Everything and its predecesor Manatee.Json are quite good and fast.

  • NJsonSchema comfortable api however too slow for our use case (schema closing to 100kb the json in 10s of kbs); the above mentioned Manatee and json-everything have a "flag-only" validation mode which is missing here

  • Newtonsoft (Paid) i have not checked this one

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号