com.github.fge.jsonschema.main.JsonSchema类的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(12.2k)|赞(0)|评价(0)|浏览(235)

本文整理了Java中com.github.fge.jsonschema.main.JsonSchema类的一些代码示例,展示了JsonSchema类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JsonSchema类的具体详情如下:
包路径:com.github.fge.jsonschema.main.JsonSchema
类名称:JsonSchema

JsonSchema介绍

[英]Single-schema instance validator

This is the interface you will use the most often. It is, in essence, a JsonValidator initialized with a single JSON Schema. Note however that this class still retains the ability to resolve JSON References.
[中]单模式实例验证程序
这是您最常使用的界面。本质上,它是一个用单个JSON模式初始化的JsonValidator。但是请注意,这个类仍然保留解析JSON引用的能力。

代码示例

代码示例来源:origin: Vedenin/useful-java-links

public static void main(final String... args)
      throws IOException, ProcessingException {
    final JsonNode fstabSchema = Utils.loadResource("/fstab.json");
    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(fstabSchema);

    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);
  }
}

代码示例来源:origin: rest-assured/rest-assured

@Override
protected boolean matchesSafely(String content) {
  try {
    JsonNode contentAsJsonNode = JsonLoader.fromString(content);
    JsonSchemaFactory jsonSchemaFactory = instanceSettings.jsonSchemaFactory();
    Schema loadedSchema = loadSchema(schema, instanceSettings);
    final JsonSchema jsonSchema;
    if (loadedSchema.hasType(JsonNode.class)) {
      jsonSchema = jsonSchemaFactory.getJsonSchema(JsonNode.class.cast(loadedSchema.schema));
    } else if (loadedSchema.hasType(String.class)) {
      jsonSchema = jsonSchemaFactory.getJsonSchema(String.class.cast(loadedSchema.schema));
    } else {
      throw new RuntimeException("Internal error when loading schema from factory. Type was " + loadedSchema.schema.getClass().getName());
    }
    if (instanceSettings.shouldUseCheckedValidation()) {
      report = jsonSchema.validate(contentAsJsonNode);
    } else {
      report = jsonSchema.validateUnchecked(contentAsJsonNode);
    }
    return report.isSuccess();
  } catch (Exception e) {
    throw new JsonSchemaValidationException(e);
  }
}

代码示例来源:origin: java-json-tools/json-schema-validator

@Override
  public RetCode validateInstance(final JsonSchema schema,
    final String fileName, final JsonNode node)
    throws IOException, ProcessingException
  {
    return schema.validInstance(node) ? ALL_OK : VALIDATION_FAILURE;
  }
}

代码示例来源:origin: loklak/loklak_server

public ProcessingReport validate(String jsonText) throws IOException {

    ProcessingReport report;
    JsonNode toValidate = JsonLoader.fromString(jsonText);
    try {
      report = this.schema.validate(toValidate);
    } catch (ProcessingException e) {
      throw new IOException("Error validating json text : " +  e.getMessage());
    }
    return report;
  }
}

代码示例来源:origin: java-json-tools/json-schema-validator

public static void main(final String... args)
    throws IOException, ProcessingException
  {
    final JsonNode fstabSchema = Utils.loadResource("/fstab-inline.json");
    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 LoadingConfiguration cfg = LoadingConfiguration.newBuilder()
      .dereferencing(Dereferencing.INLINE).freeze();
    final JsonSchemaFactory factory = JsonSchemaFactory.newBuilder()
      .setLoadingConfiguration(cfg).freeze();

    final JsonSchema schema = factory.getJsonSchema(fstabSchema);

    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);
  }
}

代码示例来源:origin: stackoverflow.com

JsonNode data = JsonLoader.fromString(jsonData);         
  JsonSchemaFactory factory = JsonSchemaFactory.byDefault(); 
  JsonSchema schema = factory.getJsonSchema(schemaNode);
  report = schema.validate(data);
} catch (JsonParseException jpex) {
  System.out.println("Error. Something went wrong trying to parse json data: #<#<"+jsonData+

代码示例来源:origin: org.bitbucket.bradleysmithllc.etlunit/core

try
  JsonSchemaFactory fact = JsonSchemaFactory.byDefault();
  ProcessingReport report = configurationJsonSchema.validate(configJson);

代码示例来源:origin: org.n52.arctic-sea/svalbard-json-common

public ProcessingReport validate(JsonNode node, String schema) {
  JsonSchema jsonSchema;
  try {
    jsonSchema = getJsonSchemaFactory().getJsonSchema(schema);
  } catch (ProcessingException ex) {
    throw new IllegalArgumentException("Unknown schema: " + schema, ex);
  }
  return jsonSchema.validateUnchecked(node);
}

代码示例来源:origin: java-json-tools/json-schema-validator

@Override
  public RetCode validateInstance(final JsonSchema schema,
    final String fileName, final JsonNode node)
    throws IOException, ProcessingException
  {
    final ListProcessingReport report
      = (ListProcessingReport) schema.validate(node, true);
    final boolean success = report.isSuccess();
    System.out.println("--- BEGIN " + fileName + "---");
    System.out.println("validation: " + (success ? "SUCCESS"
      : "FAILURE"));
    if (!success)
      System.out.println(JacksonUtils.prettyPrint(report
        .asJson()));
    System.out.println("--- END " + fileName + "---");
    return success ? ALL_OK : VALIDATION_FAILURE;
  }
},

代码示例来源:origin: java-json-tools/json-schema-validator

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 LoadingConfiguration cfg = LoadingConfiguration.newBuilder()
    .addScheme("foobar", CustomDownloader.getInstance()).freeze();
  final JsonSchemaFactory factory = JsonSchemaFactory.newBuilder()
    .setLoadingConfiguration(cfg).freeze();
  final JsonSchema schema
    = factory.getJsonSchema("foobar:/fstab.json#");
  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);
}

代码示例来源:origin: stackoverflow.com

public void validate(String schema, String msg) {
    JsonNode schemaNode = JsonLoader.fromString(schema);
    JsonNode msgNode = JsonLoader.fromString(msg);

    JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
    JsonSchema jsonSchema = factory.getJsonSchema(schemaNode);

    ProcessingReport report = jsonSchema.validate(msgNode);
    if (!report.isSuccess()) {
      throw new RuntimeException(report.toString());
    }
}

代码示例来源:origin: org.swisspush/gateleen-validation

JsonSchema schema;
try {
  schema = factory.getJsonSchema(JsonLoader.fromString(dataString));
} catch (ProcessingException | IOException e) {
  String message = "Cannot load schema " + base;
  ProcessingReport report = schema.validateUnchecked(JsonLoader.fromString(jsonBuffer.toString()));
  if(report.isSuccess()) {
    log.debug("Valid ("+type+")");

代码示例来源:origin: java-json-tools/json-schema-validator

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);
  }
}

代码示例来源:origin: java-json-tools/json-schema-validator

private static void doValidate(final Map<String, JsonNode> schemas,
    final int i)
    throws ProcessingException
  {
    String name;
    JsonNode value;
    ProcessingReport report;

    for (final Map.Entry<String, JsonNode> entry: schemas.entrySet()) {
      name = entry.getKey();
      value = entry.getValue();
      report = SCHEMA.validate(value);
      if (!report.isSuccess()) {
        System.err.println("ERROR: schema " + name + " did not "
          + "validate (iteration " + i + ')');
        System.exit(1);
      }
    }
  }
}

代码示例来源:origin: java-json-tools/json-schema-validator

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 URITranslatorConfiguration translatorCfg
      = URITranslatorConfiguration.newBuilder()
      .setNamespace(NAMESPACE).freeze();
    final LoadingConfiguration cfg = LoadingConfiguration.newBuilder()
      .setURITranslatorConfiguration(translatorCfg).freeze();

    final JsonSchemaFactory factory = JsonSchemaFactory.newBuilder()
      .setLoadingConfiguration(cfg).freeze();

    final JsonSchema schema = factory.getJsonSchema("fstab.json");

    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);
  }
}

代码示例来源:origin: motown-io/motown

public boolean isValidRequest(String request, MessageProcUri procUri) {
  JsonSchema schema = schemas.get(procUri);
  ProcessingReport report = null;
  if(schema == null) {
    try {
      String schemaName = procUri.toString().toLowerCase();
      JsonNode fstabSchema = JsonLoader.fromResource("/schemas/v15/" + schemaName + ".json");
      schema = factory.getJsonSchema(fstabSchema);
      schemas.put(procUri, schema);
    } catch (IOException e) {
      LOG.error("IOException while loading schema for procUri: " + procUri, e);
      return false;
    } catch (ProcessingException e) {
      LOG.error("ProcessingException getting JSON schema for procUri: " + procUri, e);
      return false;
    }
  }
  try {
    JsonNode jsonNode = JsonLoader.fromString(request);
    report = schema.validate(jsonNode);
  } catch (IOException e) {
    LOG.error("IOException while loading request for validation. ProcUri: " + procUri, e);
  } catch (ProcessingException e) {
    LOG.error("ProcessingException while validating request for procUri: " + procUri, e);
  }
  return report != null && report.isSuccess();
}

代码示例来源:origin: io.rest-assured/json-schema-validator

@Override
protected boolean matchesSafely(String content) {
  try {
    JsonNode contentAsJsonNode = JsonLoader.fromString(content);
    JsonSchemaFactory jsonSchemaFactory = instanceSettings.jsonSchemaFactory();
    Schema loadedSchema = loadSchema(schema, instanceSettings);
    final JsonSchema jsonSchema;
    if (loadedSchema.hasType(JsonNode.class)) {
      jsonSchema = jsonSchemaFactory.getJsonSchema(JsonNode.class.cast(loadedSchema.schema));
    } else if (loadedSchema.hasType(String.class)) {
      jsonSchema = jsonSchemaFactory.getJsonSchema(String.class.cast(loadedSchema.schema));
    } else {
      throw new RuntimeException("Internal error when loading schema from factory. Type was " + loadedSchema.schema.getClass().getName());
    }
    if (instanceSettings.shouldUseCheckedValidation()) {
      report = jsonSchema.validate(contentAsJsonNode);
    } else {
      report = jsonSchema.validateUnchecked(contentAsJsonNode);
    }
    return report.isSuccess();
  } catch (Exception e) {
    throw new JsonSchemaValidationException(e);
  }
}

代码示例来源:origin: org.swisspush/gateleen-validation

JsonSchema schema;
try {
  schema = factory.getJsonSchema(JsonLoader.fromString(schemaAsString));
} catch (ProcessingException | IOException e) {
  String message = "Cannot load schema";
  ProcessingReport report = schema.validateUnchecked(JsonLoader.fromString(dataToBeValidated.toString()));
  if(report.isSuccess()) {
    return new ValidationResult(ValidationStatus.VALIDATED_POSITIV);

代码示例来源:origin: java-json-tools/json-schema-validator

@Override
  public RetCode validateInstance(final JsonSchema schema,
    final String fileName, final JsonNode node)
    throws IOException, ProcessingException
  {
    final boolean valid = schema.validInstance(node);
    System.out.printf("%s: %s\n", fileName, valid ? "OK": "NOT OK");
    return valid ? ALL_OK : VALIDATION_FAILURE;
  }
},

代码示例来源:origin: java-json-tools/json-schema-validator

public static void main(final String... args)
    throws IOException, ProcessingException
  {
    final JsonNode fstabSchema = Utils.loadResource("/fstab.json");
    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(fstabSchema);

    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);
  }
}

相关文章