本文整理汇总了Java中org.everit.json.schema.Schema类的典型用法代码示例。如果您正苦于以下问题:Java Schema类的具体用法?Java Schema怎么用?Java Schema使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Schema类属于org.everit.json.schema包,在下文中一共展示了Schema类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import org.everit.json.schema.Schema; //导入依赖的package包/类
public static void main(final String[] args) {
JSONObject root = new JSONObject(
new JSONTokener(EveritPerf.class.getResourceAsStream("/perftest.json")));
JSONObject schemaObject = new JSONObject(
new JSONTokener(EveritPerf.class.getResourceAsStream("/schema-draft4.json")));
Schema schema = SchemaLoader.load(schemaObject);
JSONObject subjects = root.getJSONObject("schemas");
String[] names = JSONObject.getNames(subjects);
System.out.println("Now test overit...");
long startAt = System.currentTimeMillis();
for (int i = 0; i < 500; ++i) {
for (String subjectName : names) {
JSONObject subject = (JSONObject) subjects.get(subjectName);
schema.validate(subject);
}
if (i % 20 == 0) {
System.out
.println("Iteration " + i + " (in " + (System.currentTimeMillis() - startAt) + "ms)");
}
}
long endAt = System.currentTimeMillis();
long execTime = endAt - startAt;
System.out.println("total time: " + execTime + " ms");
}
示例2: schemaFromFileTest
import org.everit.json.schema.Schema; //导入依赖的package包/类
@Test
public void schemaFromFileTest() throws Exception {
Schema jsonSchema = BaseUtil.schemaFromFile("sample/component-schema.json");
Assert.assertNotNull(jsonSchema);
String schemaStr = jsonSchema.toString();
Schema copySchema = BaseUtil.schemaFromString(schemaStr);
Assert.assertNotNull(copySchema);
Assert.assertEquals(jsonSchema, copySchema);
File jsonFile = new File("sample/test-component.json");
Assert.assertNotNull(jsonFile);
String jsonData = FileUtils.readFileToString(jsonFile, Charset.defaultCharset());
Assert.assertTrue("JSON string length greater than zero", (0 < jsonData.length()));
BaseUtil.validateData(jsonSchema, jsonData);
}
示例3: compareItemSchemaArray
import org.everit.json.schema.Schema; //导入依赖的package包/类
private static void compareItemSchemaArray(final ArraySchema original, final ArraySchema update,
final Stack<String> jsonPath, final List<SchemaChange> changes) {
final List<Schema> emptyList = ImmutableList.of();
final List<Schema> originalSchemas = MoreObjects.firstNonNull(original.getItemSchemas(), emptyList);
final List<Schema> updateSchemas = MoreObjects.firstNonNull(update.getItemSchemas(), emptyList);
if (originalSchemas.size() != updateSchemas.size()) {
SchemaDiff.addChange(NUMBER_OF_ITEMS_CHANGED, jsonPath, changes);
} else {
final Iterator<Schema> originalIterator = originalSchemas.iterator();
final Iterator<Schema> updateIterator = updateSchemas.iterator();
int index = 0;
while (originalIterator.hasNext()) {
jsonPath.push("items/" + index);
SchemaDiff.recursiveCheck(originalIterator.next(), updateIterator.next(), jsonPath, changes);
jsonPath.pop();
index += 1;
}
}
}
示例4: recursiveCheck
import org.everit.json.schema.Schema; //导入依赖的package包/类
static void recursiveCheck(final CombinedSchema combinedSchemaOriginal, final CombinedSchema combinedSchemaUpdate,
final Stack<String> jsonPath,
final List<SchemaChange> changes) {
if(combinedSchemaOriginal.getSubschemas().size() != combinedSchemaUpdate.getSubschemas().size()) {
SchemaDiff.addChange(SUB_SCHEMA_CHANGED, jsonPath, changes);
} else {
if (!combinedSchemaOriginal.getCriterion().equals(combinedSchemaUpdate.getCriterion())) {
SchemaDiff.addChange(COMPOSITION_METHOD_CHANGED, jsonPath, changes);
} else {
final Iterator<Schema> originalIterator = combinedSchemaOriginal.getSubschemas().iterator();
final Iterator<Schema> updateIterator = combinedSchemaUpdate.getSubschemas().iterator();
int index = 0;
while (originalIterator.hasNext()) {
jsonPath.push(validationCriteria(combinedSchemaOriginal.getCriterion()) + "/" + index);
SchemaDiff.recursiveCheck(originalIterator.next(), updateIterator.next(), jsonPath, changes);
jsonPath.pop();
index += 1;
}
}
}
}
示例5: compareProperties
import org.everit.json.schema.Schema; //导入依赖的package包/类
private static void compareProperties(final ObjectSchema original, final ObjectSchema update,
final Stack<String> jsonPath, final List<SchemaChange> changes) {
jsonPath.push("properties");
for (final Map.Entry<String, Schema> property : original.getPropertySchemas().entrySet()) {
jsonPath.push(property.getKey());
if (!update.getPropertySchemas().containsKey(property.getKey())) {
SchemaDiff.addChange(PROPERTY_REMOVED, jsonPath, changes);
} else {
SchemaDiff.recursiveCheck(property.getValue(), update.getPropertySchemas().get(property.getKey()),
jsonPath, changes);
}
jsonPath.pop();
}
if (update.getPropertySchemas().size() > original.getPropertySchemas().size()) {
SchemaDiff.addChange(PROPERTIES_ADDED, jsonPath, changes);
}
jsonPath.pop();
}
示例6: checkJsonSchemaCompatibility
import org.everit.json.schema.Schema; //导入依赖的package包/类
@Test
public void checkJsonSchemaCompatibility() throws Exception {
final JSONArray testCases = new JSONArray(
readFile("org/zalando/nakadi/validation/invalid-schema-evolution-examples.json"));
for(final Object testCaseObject : testCases) {
final JSONObject testCase = (JSONObject) testCaseObject;
final Schema original = SchemaLoader.load(testCase.getJSONObject("original_schema"));
final Schema update = SchemaLoader.load(testCase.getJSONObject("update_schema"));
final List<String> errorMessages = testCase
.getJSONArray("errors")
.toList()
.stream()
.map(Object::toString)
.collect(toList());
final String description = testCase.getString("description");
assertThat(description, service.collectChanges(original, update).stream()
.map(change -> change.getType().toString() + " " + change.getJsonPath())
.collect(toList()), is(errorMessages));
}
}
示例7: get
import org.everit.json.schema.Schema; //导入依赖的package包/类
public Schema get(
String schemaStoreDb,
BsonValue schemaId)
throws JsonSchemaNotFoundException {
if (Bootstrapper.getConfiguration().isSchemaCacheEnabled()) {
Optional<Schema> _schema = schemaCache.get(
schemaStoreDb
+ SEPARATOR
+ schemaId);
if (_schema != null && _schema.isPresent()) {
return _schema.get();
} else {
// load it
Schema s = load(schemaStoreDb, schemaId);
schemaCache.put(schemaStoreDb + SEPARATOR + schemaId, s);
return s;
}
} else {
return load(schemaStoreDb, schemaId);
}
}
示例8: testLoadAndValidateInvalid
import org.everit.json.schema.Schema; //导入依赖的package包/类
@Test
public void testLoadAndValidateInvalid() throws Exception
{
String schemaJson = gson.toJson(of("title", "Hello World Job", "type", "object", "properties",
of("p1", of("type", "integer"), "p2", of("type", "integer")), "required", singletonList("p2")));
Schema schema = jsonValidator.loadSchema(schemaJson);
try
{
jsonValidator.validate("{\"p1\":\"10\"}", schema);
}
catch (MolgenisValidationException expected)
{
assertEquals(expected.getViolations(),
Sets.newHashSet(new ConstraintViolation("#/p1: expected type: Number, found: String"),
new ConstraintViolation("#: required key [p2] not found")));
}
}
示例9: validate
import org.everit.json.schema.Schema; //导入依赖的package包/类
/**
* Validates a given JSON Object against the a given profile schema.
* @param jsonObjectToValidate
* @param profileId
* @throws DataPackageException
* @throws ValidationException
*/
public void validate(JSONObject jsonObjectToValidate, String profileId) throws DataPackageException, ValidationException{
InputStream inputStream = Validator.class.getResourceAsStream("/schemas/" + profileId + ".json");
if(inputStream != null){
JSONObject rawSchema = new JSONObject(new JSONTokener(inputStream));
Schema schema = SchemaLoader.load(rawSchema);
schema.validate(jsonObjectToValidate); // throws a ValidationException if this object is invalid
}else{
throw new DataPackageException("Invalid profile id: " + profileId);
}
}
示例10: schemaFromFile
import org.everit.json.schema.Schema; //导入依赖的package包/类
/**
* Create JSON Schema object from json schema file
*
* @param jsonFile JSON file containing the schema
* @return Schema JSON schema object used to validate corresponding json data against
* @see <a href="https://github.com/everit-org/json-schema" target="_blank">Schema</a>
*/
public static Schema schemaFromFile(final String jsonFile) throws IOException {
File file = new File(jsonFile);
String jsonStr = FileUtils.readFileToString(file, Charset.defaultCharset());
JSONObject schemaObject = new JSONObject(jsonStr);
Schema jsonSchema = SchemaLoader.load(schemaObject);
return jsonSchema;
}
示例11: schemaFromResource
import org.everit.json.schema.Schema; //导入依赖的package包/类
/**
* Create JSON Schema object from json schema resource
*
* @param resource JSON resource file containing the schema
* @return Schema JSON schema object used to validate corresponding json data against
* @see <a href="https://github.com/everit-org/json-schema" target="_blank">Schema</a>
*/
public static Schema schemaFromResource(final String resource) throws IOException {
String jsonStr = IOUtils.resourceToString(resource, Charset.defaultCharset());
JSONObject schemaObject = new JSONObject(jsonStr);
Schema jsonSchema = SchemaLoader.load(schemaObject);
return jsonSchema;
}
示例12: deploy
import org.everit.json.schema.Schema; //导入依赖的package包/类
/**
* Deploy a component with given JSON configuration
*
* @param deployConfig Json configuration corresponding to a component to be deployed
* @param future Future to provide the status of deployment
* @see <a href="http://vertx.io/docs/apidocs/io/vertx/core/Future.html" target="_blank">Future</a>
*/
public void deploy(JsonObject deployConfig, Future<Boolean> future) {
if (null == this.vertx) {
String errMesg = "Not setup yet! Call 'setup' method first.";
logger.error(errMesg);
future.fail(new Exception(errMesg));
return;
}
try {
Schema jsonSchema = BaseUtil.schemaFromResource("/deploy-opts-schema.json");
String jsonData = deployConfig.toString();
BaseUtil.validateData(jsonSchema, jsonData);
} catch (ValidationException vldEx) {
logger.error("Issue with deployment configuration validation!", vldEx);
vldEx
.getCausingExceptions()
.stream()
.map(ValidationException::getMessage)
.forEach((errMsg) -> {
logger.error(errMsg);
});
future.fail(vldEx);
return;
} catch (IOException ioEx) {
logger.error("Issue while loading schema configuration!", ioEx);
future.fail(ioEx);
return;
}
DeployComponent component = this.setupComponent(deployConfig);
this.deployRecords.deploy(component.getIdentifier(), component.getDeployOpts(), future);
}
示例13: schemaFromResourceTest
import org.everit.json.schema.Schema; //导入依赖的package包/类
@Test
public void schemaFromResourceTest() throws Exception {
Schema jsonSchema = BaseUtil.schemaFromResource("/deployer-schema.json");
Assert.assertNotNull(jsonSchema);
File jsonFile = new File("sample/no-cluster.json");
Assert.assertNotNull(jsonFile);
String jsonData = FileUtils.readFileToString(jsonFile, Charset.defaultCharset());
Assert.assertTrue("JSON string length greater than zero", (0 < jsonData.length()));
BaseUtil.validateData(jsonSchema, jsonData);
}
示例14: invalidSchemaTest
import org.everit.json.schema.Schema; //导入依赖的package包/类
@Test
public void invalidSchemaTest() throws Exception {
thrown.expect(ValidationException.class);
Schema jsonSchema = BaseUtil.schemaFromResource("/deployer-schema.json");
Assert.assertNotNull(jsonSchema);
File jsonFile = new File("sample/test-component.json");
Assert.assertNotNull(jsonFile);
String jsonData = FileUtils.readFileToString(jsonFile, Charset.defaultCharset());
Assert.assertTrue("JSON string length greater than zero", (0 < jsonData.length()));
BaseUtil.validateData(jsonSchema, jsonData);
}
示例15: validate
import org.everit.json.schema.Schema; //导入依赖的package包/类
public static void validate(String schema, String input){
JSONObject jsonSchema = new JSONObject(schema);
JSONObject jsonSubject = new JSONObject(input);
Schema schema_ = SchemaLoader.load(jsonSchema);
schema_.validate(jsonSubject);
}