Json.NET using JSON schema to validate JSON format

Source: Internet
Author: User

Json.NET supports the JSON Schema standard via the Jsonschema and jsonvalidatingreader classes. It sits under the Newtonsoft.Json.Schema namespace.

Json.NET supports the JSON schema standard through the Jsonschema and Jsonvalidatingreader classes. These two classes are located in the Newtonsoft.Json.Schema namespace.

JSON schema is used-validate the structure and data types of a piece of JSON, similar to XML Schema for XML. Read more on JSON Schema at json-schema.org

The JSON schema is used to validate the structure of the JSON and the data type, similar to XML XML schema. Information on more JSON schemas can be found in json-schema.org.

Validating with JSON SchemaValidating with JSON schema

The simplest-to-check if JSON is valid-to-load the JSON into a jobject or jarray and then use the IsValid (jtoke N, Jsonschema) method with the JSON Schema.

The easiest way to test whether the JSON is compliant is to load the JSON string into Jobject or Jarray, and then call IsValid (Jtoken, Jsonschema) with the JSON schema.

 string  Schemajson = @ " {' description ': ' A person ', ' type ': ' Object ', ' properties ': {' name ': {' type ': ' String '}, ' hobbies ': {' type ': ' array ', ' I TEMs ': {' type ': ' String '}}}    = Jsonschema.parse (Schemajson); Jobject person  = jobject.parse (@ " {' name ': ' James ', ' hobbies ': ['. NET ', ' Blogging ', ' Reading ', ' Xbox ', ' LOLCATS ']}   "   bool  valid = person. IsValid (schema);  //  

To get validation error messages use the IsValid (Jtoken, Jsonschema, IList<string>) or Validate (Jtoken, Jsonschema, ValidationEventHandler) overloads.

To get a validation error message can be used IsValid (Jtoken, Jsonschema, IList<string>) or validate (Jtoken, Jsonschema, ValidationEventHandler) overloads.

 Jsonschema schema = Jsonschema.parse (Schemajson); Jobject person  = jobject.parse (@ " {' name ': null, ' hobbies ': [' Invalid content ', 0.123456789]}   '  

Internally IsValid uses Jsonvalidatingreader to perform the JSON Schema validation. To skip the overhead of loading JSON in a jobject/jarray, validating the JSON and then deserializing the JSON into a CLA SS, Jsonvalidatingreader can be used with Jsonserializer to validate JSON while the object is being deserialized.

Skipping the overhead of loading the JSON string to Jobject/jarray, validating the JSON and then deserializing it into a class, Jsonvalidatingreader can validate the JSON with Jsonserializer when an object is deserialized.

stringJSON =@"{' name ': ' James ', ' hobbies ': ['. NET ', ' Blogging ', ' Reading ', ' Xbox ', ' LOLCATS ']}"; JsonTextReader Reader=NewJsonTextReader (NewStringReader (JSON)); Jsonvalidatingreader Validatingreader=NewJsonvalidatingreader (reader); Validatingreader.schema=Jsonschema.parse (Schemajson); IList<string> messages =Newlist<string>(); Validatingreader.validationeventhandler+ = (o, a) = =messages. ADD (A.message); Jsonserializer Serializer=NewJsonserializer (); Person P= Serializer. Deserialize<person> (Validatingreader);
Creating JSON Schemas GenerationJSON Schemas

The simplest-to-get a Jsonschema object is to-load it from a string or a file.

The easiest way to get a Jsonschema object is to load it from a string or file.

// load from a string Jsonschema schema1 = jsonschema.parse (@ "{' type ': ' object '}"); // load from a file using (TextReader reader = File.OpenText (@ "c:\schema\Person.json")) {    = Jsonschema.read (new  jsontextreader (reader));     // Do Stuff}

It is also possible to create Jsonschema objects in code.

You can also create Jsonschema objects from your code.

Jsonschema schema =NewJsonschema (); schema. Type=Jsonschematype.object;schema. Properties=Newdictionary<string, jsonschema>{    { "name",NewJsonschema {Type =jsonschematype.string}}, {"Hobbies",NewJsonschema {Type=Jsonschematype.array, Items=Newlist<jsonschema> {NewJsonschema {Type =jsonschematype.string}}},}; Jobject Person= Jobject.parse (@"{' name ': ' James ', ' hobbies ': ['. NET ', ' Blogging ', ' Reading ', ' Xbox ', ' LOLCATS ']}");BOOLvalid =Person . IsValid (schema);//true

Original link: http://james.newtonking.com/json/help/index.html

More information: http://json-schema.org/

Json.NET using JSON schema to validate JSON format

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.