File tree Expand file tree Collapse file tree
java/org/baeldung/json/schema Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+ <classpath >
3+ <classpathentry kind =" src" output =" target/classes" path =" src/main/java" >
4+ <attributes >
5+ <attribute name =" optional" value =" true" />
6+ <attribute name =" maven.pomderived" value =" true" />
7+ </attributes >
8+ </classpathentry >
9+ <classpathentry excluding =" **" kind =" src" output =" target/classes" path =" src/main/resources" >
10+ <attributes >
11+ <attribute name =" maven.pomderived" value =" true" />
12+ </attributes >
13+ </classpathentry >
14+ <classpathentry kind =" src" output =" target/test-classes" path =" src/test/java" >
15+ <attributes >
16+ <attribute name =" optional" value =" true" />
17+ <attribute name =" maven.pomderived" value =" true" />
18+ </attributes >
19+ </classpathentry >
20+ <classpathentry excluding =" **" kind =" src" output =" target/test-classes" path =" src/test/resources" >
21+ <attributes >
22+ <attribute name =" maven.pomderived" value =" true" />
23+ </attributes >
24+ </classpathentry >
25+ <classpathentry kind =" con" path =" org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER" >
26+ <attributes >
27+ <attribute name =" maven.pomderived" value =" true" />
28+ </attributes >
29+ </classpathentry >
30+ <classpathentry kind =" con" path =" org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8" />
31+ <classpathentry kind =" output" path =" target/classes" />
32+ </classpath >
Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+ <projectDescription >
3+ <name >json</name >
4+ <comment ></comment >
5+ <projects >
6+ </projects >
7+ <buildSpec >
8+ <buildCommand >
9+ <name >org.eclipse.jdt.core.javabuilder</name >
10+ <arguments >
11+ </arguments >
12+ </buildCommand >
13+ <buildCommand >
14+ <name >org.eclipse.m2e.core.maven2Builder</name >
15+ <arguments >
16+ </arguments >
17+ </buildCommand >
18+ </buildSpec >
19+ <natures >
20+ <nature >org.eclipse.jdt.core.javanature</nature >
21+ <nature >org.eclipse.m2e.core.maven2Nature</nature >
22+ </natures >
23+ </projectDescription >
Original file line number Diff line number Diff line change 1+ <project xmlns =" http://maven.apache.org/POM/4.0.0" xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
2+ xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
3+ <modelVersion >4.0.0</modelVersion >
4+ <groupId >org.baeldung</groupId >
5+ <artifactId >json</artifactId >
6+ <version >0.0.1-SNAPSHOT</version >
7+
8+ <dependencies >
9+
10+ <dependency >
11+ <groupId >org.everit.json</groupId >
12+ <artifactId >org.everit.json.schema</artifactId >
13+ <version >1.3.0</version >
14+ </dependency >
15+
16+ <dependency >
17+ <groupId >junit</groupId >
18+ <artifactId >junit</artifactId >
19+ <version >4.12</version >
20+ <scope >test</scope >
21+ </dependency >
22+
23+ </dependencies >
24+
25+ </project >
Original file line number Diff line number Diff line change 1+ package org .baeldung .json .schema ;
2+
3+ import org .everit .json .schema .Schema ;
4+ import org .everit .json .schema .ValidationException ;
5+ import org .everit .json .schema .loader .SchemaLoader ;
6+
7+ import org .json .JSONObject ;
8+ import org .json .JSONTokener ;
9+ import org .junit .Test ;
10+
11+ public class JSONSchemaTest {
12+
13+ @ Test
14+ public void validateJSON () {
15+
16+ JSONObject jsonSchema = new JSONObject (new JSONTokener (JSONSchemaTest .class .getResourceAsStream ("/schema.json" )));
17+ JSONObject jsonSubject = new JSONObject (new JSONTokener (JSONSchemaTest .class .getResourceAsStream ("/product.json" )));
18+
19+ Schema schema = SchemaLoader .load (jsonSchema );
20+
21+ try {
22+
23+ schema .validate (jsonSubject );
24+ }
25+
26+ catch (ValidationException e ) {
27+
28+ System .out .println (e .getMessage ());
29+ e .getCausingExceptions ().stream ().map (ValidationException ::getMessage ).forEach (System .out ::println );
30+ }
31+ }
32+ }
Original file line number Diff line number Diff line change 1+ {
2+ "id" : 1 ,
3+ "name" : " Lampshade" ,
4+ "price" : 0
5+ }
Original file line number Diff line number Diff line change 1+ {
2+ "$schema" : " http://json-schema.org/draft-04/schema#" ,
3+ "title" : " Product" ,
4+ "description" : " A product from the catalog" ,
5+ "type" : " object" ,
6+ "properties" : {
7+ "id" : {
8+ "description" : " The unique identifier for a product" ,
9+ "type" : " integer"
10+ },
11+ "name" : {
12+ "description" : " Name of the product" ,
13+ "type" : " string"
14+ },
15+ "price" : {
16+ "type" : " number" ,
17+ "minimum" : 0 ,
18+ "exclusiveMinimum" : true
19+ }
20+ },
21+ "required" : [" id" , " name" , " price" ]
22+ }
You can’t perform that action at this time.
0 commit comments