All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nedap.archie.json.JsonSchemaValidator Maven / Gradle / Ivy

Go to download

tools that operate on the archie reference models and archetype object model

There is a newer version: 3.12.0
Show newest version
package com.nedap.archie.json;

import com.google.common.base.Charsets;
import org.leadpony.justify.api.JsonSchema;
import org.leadpony.justify.api.JsonValidationService;
import org.leadpony.justify.api.Problem;
import org.leadpony.justify.api.ProblemHandler;
import org.openehr.bmm.core.BmmModel;

import jakarta.json.JsonObject;
import jakarta.json.JsonReader;
import jakarta.json.JsonStructure;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * Validates JSON files against a schema automatically created from a BMM repository.
 *
 * 

* This is a Convenience API, you can easily manually do the same with the JsonSchemaCreator. *

* * Note that currently the root JSON element must have a '_type' property for the schema validator to know which type to validate */ public class JsonSchemaValidator { JsonSchema schema; /** * Creates a JsonSchemaValidator that validates against the json schema created from the given Bmm Model * The JSON Schema complies to the JSON format that the OpenEHR project uses. This may very well be different from * the serialization rules corresponding to your own different BMM file, if it is not an OpenEHR model. * * @param bmmModel the model to create the JSON Schema for * @param allowAdditionalProperties whether to allow additional properties in the JSON */ public JsonSchemaValidator(BmmModel bmmModel, boolean allowAdditionalProperties) { JsonObject schemaJson = new JSONSchemaCreator().allowAdditionalProperties(allowAdditionalProperties).create(bmmModel); JsonValidationService service = JsonValidationService.newInstance(); schema = service.readSchema(createByteArrayInputStream(schemaJson.toString())); } private ByteArrayInputStream createByteArrayInputStream(String json) { return new ByteArrayInputStream(json.getBytes(Charsets.UTF_8)); } /** * Validate the given json against the schema * @param json the json * @return the list of problems found during validation, or an empty list if the json validated * @throws IOException */ public List validate(String json) throws IOException { JsonValidationService service = JsonValidationService.newInstance(); List allProblems = new ArrayList<>(); ProblemHandler problemHandler = new ProblemHandler() { @Override public void handleProblems(List problems) { allProblems.addAll(problems); } }; try (JsonReader reader = service.createReader(createByteArrayInputStream(json), schema, problemHandler)) { JsonStructure structure = reader.read(); return allProblems; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy