When we first approached XML, we used a DTD file to define what elements and attributes could be in the XML, and later evolved to XML Schama (an XSD file that provided more powerful features such as namespaces on a DTD basis)
Now that the RESTful interface and JSON are large, we can store the JSON directly in the database, providing the ability to scale more easily than the original relational table (though the JSON string's storage still uses the relational table). The RESTful interface returns JSON data directly, and is more concise and readable than returning XML.
XML schemas can have XML schema to define and verify XML data, and in the same way JSON has a corresponding mechanism called JSON schema, to describe and define JSON data, and provide a mechanism to verify that a JSON string conforms to JSON The definition of the schema.
JSON schema syntax Next I'll write a separate article to summarize, below is a simple example of JSON schema, is used in our project:
{ "$schema": "Http://json-schema.org/draft-04/schema", "type": "Object", "Properties": { "name" : { "Type": "String" }, "versions": { "type": "Array", "items": { "type": "Object", " Properties ": {" id ": { " type ":" String " }, " version ": { " type ":" Integer " }, " Comment ": { " type ":" String " } }, " required ": [" id "," version "], " Minitems ": 1 } } }, "required": ["Name", "Versions"]}
The root object is of type object, there are two properties, name and version where name is a string, and version is an array, an array of element object types, including the ID of the string type, the comment of the shaping version and the string type.
JSON scheam specifies what elements and their types should be in the JSON string, what elements must exist, the range of elements, and so forth.
To verify that a given JSON string conforms to a given JSON schema, in Java we can use Json-schame-validator Https://github.com/fge/json-schema-validator
Below is the code for JSON schema validation:
Import Java.io.ioexception;import Org.apache.commons.logging.log;import org.apache.commons.logging.LogFactory; Import Com.fasterxml.jackson.databind.jsonnode;import Com.github.fge.jackson.jsonloader;import Com.github.fge.jsonschema.core.exceptions.processingexception;import Com.github.fge.jsonschema.core.report.processingreport;import Com.github.fge.jsonschema.main.jsonschema;import Com.github.fge.jsonschema.main.jsonschemafactory;public class Jsonshemavalidator {private static log log = Logfactory. GetLog (Jsonshemavalidator.class); Private final static Jsonschemafactory factory = Jsonschemafactory.bydefault (); /** * Validate instance and schema,here including, functions. as follows: * first:the Draft v4 would check the syntax both of the schema and instance. * Second:instance validation. * * @param Mainschema * @param instance * @return * @throws IOException * @throws processingexception */public static Processingreport ValidatorschEMA (string Mainschema, string instance) throws IOException, processingexception {jsonnode Mainnode = jsonloader.fr Omstring (Mainschema); Jsonnode Instancenode = jsonloader.fromstring (instance); Jsonschema schema = Factory.getjsonschema (Mainnode); Processingreport Processingreport = schema.validate (Instancenode); Log.info (Processingreport); return processingreport; }}
The results of the validation can be obtained from the returned Processingreport object: Success or failure, error message, etc.
Reprint: https://www.cnblogs.com/jiaoyiping/p/4799754.html
Jsonschema and Jsonschemavalidator