JSON Schema Validation: Difference between revisions
Jump to navigation
Jump to search
Line 37: | Line 37: | ||
{| | {| | ||
| valign="top" | | | valign="top" | | ||
* [https://stackoverflow.com/questions/27952472/ Jackson Serialize/Deserialize for Java 8 <code>java.time</code>] | |||
* [https://stackoverflow.com/questions/62117492/ NetworkNT JSON Schema With Unknown keywords] | * [https://stackoverflow.com/questions/62117492/ NetworkNT JSON Schema With Unknown keywords] | ||
* [https://github.com/networknt/json-schema-validator NetworkNT fastest Java JSON Schema Validator] | * [https://github.com/networknt/json-schema-validator NetworkNT fastest Java JSON Schema Validator] |
Revision as of 04:02, 24 June 2021
<dependency>
<groupId>com.networknt</groupId>
<artifactId>json-schema-validator</artifactId>
<version>1.0.42</version>
</dependency>
private static InputStream inputStreamFromClasspath(String path) {
return Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
}
public static void main(String[] args) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
JsonSchemaFactory schemaFactory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909);
try (
InputStream jsonStream = inputStreamFromClasspath("example.json");
InputStream schemaStream = inputStreamFromClasspath("example-schema.json")
) {
JsonNode json = objectMapper.readTree(jsonStream);
JsonSchema schema = schemaFactory.getSchema(schemaStream);
Set<ValidationMessage> validationResult = schema.validate(json);
// print validation errors
if (validationResult.isEmpty()) {
System.out.println("no validation errors :-)");
} else {
validationResult.forEach(vm -> System.out.println(vm.getMessage()));
}
}
}