xpertss.json.schema.examples.Example4 Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of json-schema Show documentation
Show all versions of json-schema Show documentation
A Java implementation of the JSON Schema specification.
The newest version!
package xpertss.json.schema.examples;
import com.fasterxml.jackson.databind.JsonNode;
import xpertss.json.schema.core.exceptions.ProcessingException;
import xpertss.json.schema.core.report.ProcessingReport;
import xpertss.json.schema.main.JsonSchema;
import xpertss.json.schema.main.JsonSchemaFactory;
import java.io.IOException;
/**
* Fourth example: schema loading via URIs, and subschema addressing
*
*
*
*
*
* This demonstrates two capabilities of {@link JsonSchemaFactory}:
*
*
* - the ability to Utils.load schemas via URIs;
* - the ability to address subschemas in a schema.
*
*
* The implementation provides a {@code resource} scheme which allows to Utils.load
* JSON from files in the classpath. It is strictly equivalent to calling {@link
* Class#getResourceAsStream(String)}.
*
* The URI used is {@code
* resource:/org/eel/kitchen/jsonschema/examples/fstab-sub.json}. Because we
* want to validate against the {@code fstab} subschema, we use {@link
* JsonSchemaFactory#getJsonSchema(String)} to Utils.load the actual schema; the URI
* used as an argument also has a JSON Pointer as a fragment.
*
* Files validated, and the validation outputs, are the same as for {@link
* Example2}.
*/
public final class Example4
{
private static final String SCHEMA_URI
= "resource:/com/github/fge/jsonschema/examples/fstab-sub.json#/fstab";
public static void main(final String... args)
throws IOException, ProcessingException
{
final JsonNode good = Utils.loadResource("/fstab-good.json");
final JsonNode bad = Utils.loadResource("/fstab-bad.json");
final JsonNode bad2 = Utils.loadResource("/fstab-bad2.json");
final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
final JsonSchema schema = factory.getJsonSchema(SCHEMA_URI);
ProcessingReport report;
report = schema.validate(good);
System.out.println(report);
report = schema.validate(bad);
System.out.println(report);
report = schema.validate(bad2);
System.out.println(report);
}
}