/
JsonSchemaValidator.java
67 lines (60 loc) · 1.93 KB
/
JsonSchemaValidator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.ansel.testall.json;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JsonLoader;
import com.github.fge.jackson.JsonNodeReader;
import com.github.fge.jsonschema.core.report.LogLevel;
import com.github.fge.jsonschema.core.report.ProcessingMessage;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
public class JsonSchemaValidator {
public static Map<String, Object> validateJsonByFgeByJsonNode(JsonNode jsonNode, JsonNode schemaNode) {
Map<String, Object> result = new HashMap<String, Object>();
ProcessingReport report = null;
report = JsonSchemaFactory.byDefault().getValidator().validateUnchecked(schemaNode, jsonNode);
if (report.isSuccess()) {
// 校验成功
result.put("message", "校验成功!");
result.put("success", true);
return result;
} else {
System.out.println("校验失败!");
Iterator<ProcessingMessage> it = report.iterator();
String ms = "";
while (it.hasNext()) {
ProcessingMessage pm = it.next();
if (!LogLevel.WARNING.equals(pm.getLogLevel())) {
ms += pm;
}
}
result.put("message", "校验失败!" + ms);
result.put("success", false);
return result;
}
}
public static JsonNode getJsonNodeFromString(String jsonStr) {
JsonNode jsonNode = null;
try {
jsonNode = JsonLoader.fromString(jsonStr);
} catch (IOException e) {
e.printStackTrace();
}
return jsonNode;
}
public static JsonNode getJsonNodeFromFile(String filePath) {
JsonNode jsonNode = null;
try {
jsonNode = new JsonNodeReader().fromReader(new FileReader(filePath));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return jsonNode;
}
}