本文整理汇总了Java中com.github.fge.jackson.JsonLoader.fromString方法的典型用法代码示例。如果您正苦于以下问题:Java JsonLoader.fromString方法的具体用法?Java JsonLoader.fromString怎么用?Java JsonLoader.fromString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.github.fge.jackson.JsonLoader
的用法示例。
在下文中一共展示了JsonLoader.fromString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: validate
import com.github.fge.jackson.JsonLoader; //导入方法依赖的package包/类
@SneakyThrows
private boolean validate(Map<String, Object> data, String jsonSchema) {
String stringData = objectMapper.writeValueAsString(data);
log.debug("Validation data. map: {}, jsonData: {}", data, stringData);
JsonNode schemaNode = JsonLoader.fromString(jsonSchema);
JsonNode dataNode = JsonLoader.fromString(stringData);
JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
JsonSchema schema = factory.getJsonSchema(schemaNode);
val report = schema.validate(dataNode);
boolean isSuccess = report.isSuccess();
if (!isSuccess) {
log.info("Validation data report: {}", report.toString().replaceAll("\n", " | "));
}
return isSuccess;
}
示例2: isValid
import com.github.fge.jackson.JsonLoader; //导入方法依赖的package包/类
public static boolean isValid(String json, String schema) throws Exception {
JsonNode schemaNode = JsonLoader.fromString(schema);
JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
JsonSchema jsonSchema = factory.getJsonSchema(schemaNode);
JsonNode jsonNode = JsonLoader.fromString(json);
ProcessingReport report = jsonSchema.validate(jsonNode);
logger.debug("report: {}", report);
return report.isSuccess();
}
示例3: validate
import com.github.fge.jackson.JsonLoader; //导入方法依赖的package包/类
public ValidationErrors validate(String schema, String requestBody) {
ValidationErrors errors = new ValidationErrors();
try {
JsonNode schemaNode = JsonLoader.fromString(schema);
JsonNode dataNode = JsonLoader.fromString(requestBody);
ProcessingReport report = JsonSchemaFactory.byDefault().getJsonSchema(schemaNode).validate(dataNode);
if (!report.isSuccess())
report.iterator().forEachRemaining(m -> errors.addMessage("[ %s ] [ body ] %s", bodyType, m.getMessage()));
} catch (Exception e) {
errors.addMessage("[ %s ] [ body ] Unexpected error of [ %s ] while validating JSON content", bodyType, e.getMessage());
}
return errors;
}
示例4: assertMatchesJsonSchema
import com.github.fge.jackson.JsonLoader; //导入方法依赖的package包/类
private void assertMatchesJsonSchema(final String json) {
try {
final ObjectNode rootNode = (ObjectNode) JsonLoader.fromString(json);
final ObjectNode contextNode = (ObjectNode) rootNode.get("context");
if (contextNode != null) {
contextNode.remove("logger");
contextNode.remove("MDC_KEY");
contextNode.remove("CONTEXT_KEY1");
contextNode.remove("CONTEXT_KEY2");
contextNode.remove("CONTEXT_KEY3");
contextNode.remove("CONTEXT_KEY4");
contextNode.remove("CONTEXT_KEY5");
contextNode.remove("CONTEXT_KEY6");
}
final ProcessingReport report = _validator.validate(STENO_SCHEMA, rootNode);
Assert.assertTrue(report.toString(), report.isSuccess());
} catch (final IOException | ProcessingException e) {
Assert.fail("Failed with exception: " + e);
}
}
示例5: validateJson
import com.github.fge.jackson.JsonLoader; //导入方法依赖的package包/类
protected void validateJson(String jsonFileName) throws IOException, ProcessingException, JsonLdError {
JsonNode schema = getSchema();
assertNotNull(schema);
String jsonStr = getJson(jsonFileName);
assertNotNull(jsonStr);
Object jsonObj = JsonUtils.fromString(jsonStr);
List<Object> expandedJson = JsonLdProcessor.expand(jsonObj);
jsonStr = JsonUtils.toString(expandedJson);
JsonNode json = JsonLoader.fromString(jsonStr);
JsonValidator jsonValidator = JsonSchemaFactory.byDefault().getValidator();
ProcessingReport processingReport = jsonValidator.validate(schema, json);
assertNotNull(processingReport);
if (!processingReport.isSuccess()) {
ArrayNode jsonArray = JsonNodeFactory.instance.arrayNode();
assertNotNull(jsonArray);
Iterator<ProcessingMessage> iterator = processingReport.iterator();
while (iterator.hasNext()) {
ProcessingMessage processingMessage = iterator.next();
jsonArray.add(processingMessage.asJson());
}
String errorJson = JsonUtils.toPrettyString(jsonArray);
assertNotNull(errorJson);
fail(errorJson);
}
}
示例6: validateJSON
import com.github.fge.jackson.JsonLoader; //导入方法依赖的package包/类
/**
* Uses {@link JsonSchema} to validate JSON against A JSON Schema File
*
* @param jsonSchema the schema
* @param json the json to be validated.
* @return true if the json matches the json schema
* @see <a href="https://github.com/fge/json-schema-validator">JSON Schema Validator</a>
*/
public static boolean validateJSON(@NotNull String jsonSchema, @NotNull String json) {
try {
JsonSchema schema = JsonSchemaFactory.byDefault().getJsonSchema(JsonLoader.fromString(jsonSchema));
JsonNode jsonToBeValidated = JsonLoader.fromString(json);
return schema.validInstance(jsonToBeValidated);
} catch (IOException | ProcessingException ignored) {
}
return false;
}
示例7: setup
import com.github.fge.jackson.JsonLoader; //导入方法依赖的package包/类
@Override
public void setup(OperatorContext context)
{
try {
if (jsonSchema != null) {
JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
JsonNode schemaNode = JsonLoader.fromString(jsonSchema);
schema = factory.getJsonSchema(schemaNode);
}
objMapper = new ObjectMapper();
objMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
} catch (ProcessingException | IOException e) {
DTThrowable.wrapIfChecked(e);
}
}
示例8: validate
import com.github.fge.jackson.JsonLoader; //导入方法依赖的package包/类
public void validate(String ConfigurationFile) throws IOException, ProcessingException, IllegalSchemaException {
JsonNode Configuration = JsonLoader.fromString(ConfigurationFile);
ProcessingReport r1 = VALIDATOR.validate(
ConfigurationSchema,
Configuration
);
if (!r1.isSuccess()) {
Iterator<ProcessingMessage> it = r1.iterator();
ProcessingMessage pm = it.next();
JSONObject o = new JSONObject(pm.asJson().toString());
throw new IllegalSchemaException(o.toString(4));
}
}
示例9: validateJsonData
import com.github.fge.jackson.JsonLoader; //导入方法依赖的package包/类
protected boolean validateJsonData(final String jsonSchema, final String jsonData) throws Exception {
final JsonNode d = JsonLoader.fromString(jsonData);
final JsonNode s = JsonLoader.fromString(jsonSchema);
final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
JsonValidator v = factory.getValidator();
ProcessingReport report = v.validate(s, d);
return report.toString().contains("success");
}
示例10: create
import com.github.fge.jackson.JsonLoader; //导入方法依赖的package包/类
public JsonSchema create(String schemaString) {
JsonSchema jsonSchema = null;
try {
JsonNode schemaNode = JsonLoader.fromString(schemaString);
if (!getJsonSchemaFactory().getSyntaxValidator().schemaIsValid(schemaNode)) {
throw new InvalidSchemaException("JSON Schema is invalid" + getDescriptionAppendable());
}
jsonSchema = getJsonSchemaFactory().getJsonSchema(schemaNode);
}
catch (NullPointerException | IOException | ProcessingException ex) {
throw new InvalidSchemaException("JSON Schema could not be loaded" + getDescriptionAppendable(), ex);
}
return jsonSchema;
}
示例11: validate
import com.github.fge.jackson.JsonLoader; //导入方法依赖的package包/类
@Override
public final ValidationResult validate(String json) throws IOException {
JsonNode jsonNode = JsonLoader.fromString(json);
return getValidationResult(jsonNode);
}
示例12: getJsonNode
import com.github.fge.jackson.JsonLoader; //导入方法依赖的package包/类
public static JsonNode getJsonNode(String jsonText)
throws IOException
{
return JsonLoader.fromString(jsonText);
}
示例13: validate
import com.github.fge.jackson.JsonLoader; //导入方法依赖的package包/类
protected String validate(String jsonStr, JsonNode validationSchema) throws ProcessingException, IOException {
JsonNode json = JsonLoader.fromString(jsonStr);
JsonValidator jsonValidator = JsonSchemaFactory.byDefault().getValidator();
ProcessingReport processingReport = jsonValidator.validate(validationSchema, json);
if (!processingReport.isSuccess()) {
ArrayNode jsonArray = JsonNodeFactory.instance.arrayNode();
for (ProcessingMessage processingMessage : processingReport) {
jsonArray.add(processingMessage.asJson());
}
return JsonUtils.toPrettyString(jsonArray);
}
return null;
}
示例14: getSchema
import com.github.fge.jackson.JsonLoader; //导入方法依赖的package包/类
private JsonNode getSchema() throws IOException {
String jsonStr = getJson(getSchemaFileName());
return JsonLoader.fromString(jsonStr);
}
示例15: processTuple
import com.github.fge.jackson.JsonLoader; //导入方法依赖的package包/类
@Override
public void processTuple(byte[] tuple)
{
if (tuple == null) {
if (err.isConnected()) {
err.emit(new KeyValPair<String, String>(null, "null tuple"));
}
errorTupleCount++;
return;
}
String incomingString = new String(tuple);
try {
if (schema != null) {
ProcessingReport report = null;
JsonNode data = JsonLoader.fromString(incomingString);
report = schema.validate(data);
if (report != null && !report.isSuccess()) {
Iterator<ProcessingMessage> iter = report.iterator();
StringBuilder s = new StringBuilder();
while (iter.hasNext()) {
ProcessingMessage pm = iter.next();
s.append(pm.asJson().get("instance").findValue("pointer")).append(":").append(pm.asJson().get("message"))
.append(",");
}
s.setLength(s.length() - 1);
errorTupleCount++;
if (err.isConnected()) {
err.emit(new KeyValPair<String, String>(incomingString, s.toString()));
}
return;
}
}
if (parsedOutput.isConnected()) {
parsedOutput.emit(new JSONObject(incomingString));
parsedOutputCount++;
}
if (out.isConnected()) {
out.emit(objMapper.readValue(tuple, clazz));
emittedObjectCount++;
}
} catch (JSONException | ProcessingException | IOException e) {
errorTupleCount++;
if (err.isConnected()) {
err.emit(new KeyValPair<String, String>(incomingString, e.getMessage()));
}
logger.error("Failed to parse json tuple {}, Exception = {} ", tuple, e);
}
}