■
java6でjsonValidation
<groupId>com.github.erosb</groupId>
<artifactId>everit-json-schema-jdk6</artifactId>
<version>1.9.2</version>
</dependency>
jackson2.6.7
try {
BufferedReader reader = new BufferedReader(new FileReader(new File("/Applications/Eclipse_2019-06.app/Contents/workspace/mavenTest/src/main/java/mavenTest/person.json")));
String line = null;
StringBuilder builder = new StringBuilder();
while *1 != null) {
builder.append(line);
}
BufferedReader reader2 = new BufferedReader(new FileReader(new File("/Applications/Eclipse_2019-06.app/Contents/workspace/mavenTest/src/main/java/mavenTest/person.schema.json")));
String line2 = null;
StringBuilder builder2 = new StringBuilder();
while *2 != null) {
builder2.append(line2);
}
JSONObject jsonSchema = new JSONObject(new JSONTokener(builder2.toString()));
JSONObject jsonSubject = new JSONObject(new JSONTokener(builder.toString()));
Schema schema = SchemaLoader.load(jsonSchema);
try {
schema.validate(jsonSubject);
System.out.println("json is valid!");
}catch(ValidationException ve) {
System.out.println(ve.getMessage());
for(int i=0;i<ve.getCausingExceptions().size();i++) {
System.out.println(ve.getCausingExceptions().get(i).getMessage());
}
System.out.println("json is invalid!");
}
//return builder.toString();
} catch (IOException e) {
e.printStackTrace();
}finally {
}
json schema
{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "integer",
"minimum": 0
},
"code": {
"type": "string",
"maxLength": 4
},
"birthday": {
"type": "string",
"pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$"
},
"hobbies": {
"type": "array",
"items": {
"type": "string"
}
}
},
"required": ["address"]
}