Skip to content

校验JSON数据是否符合schema;类生成JSON schema字符串;

Notifications You must be signed in to change notification settings

gaiwey/json-schema-util

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

工程介绍

验证json是否符合schema格式; 类生成JSON scheme;

JsonSchemaUtil介绍

JsonSchemaUtil provides json schema verification. If the verification is successful, it returns TRUE, and if the verification fails, it returns FALSE.

用法:

用法1:

String json = "{\"name\":\"TOM\",\"age\":23,\"declawed\":false,\"description\":\"TOM loves to sleep all day.\"}";

String jsonSchema = "{\"$schema\":\"http://json-schema.org/draft-04/schema#\",\"title\":\"cat\",\"properties\":{\"name\":{\"type\":\"string\"},\"age\":{\"type\":\"number\",\"description\":\"Your cat's age in years\"},\"declawed\":{\"type\":\"boolean\"},\"description\":{\"type\":\"string\"}},\"required\":[\"name\",\"age\",\"declawed\"]}";

System.out.println(JsonSchemaUtil.validate(json, jsonSchema));

用法2:

String json = "{\"name\":\"TOM\",\"age\":23,\"declawed\":false,\"description\":\"TOM loves to sleep all day.\"}";

String jsonSchema = "{\"$schema\":\"http://json-schema.org/draft-04/schema#\",\"title\":\"cat\",\"properties\":{\"name\":{\"type\":\"string\"},\"age\":{\"type\":\"number\",\"description\":\"Your cat's age in years\"},\"declawed\":{\"type\":\"boolean\"},\"description\":{\"type\":\"string\"}},\"required\":[\"name\",\"age\",\"declawed\"]}";

JsonNode jsonNode = strToJsonNode(json);

JsonNode schemaNode = strToJsonNode(jsonSchema);

System.out.println(JsonSchemaUtil.validate(jsonNode, schemaNode));

GenerateJsonSchemaUtil介绍

Class生产Json schema string

用法:

GenerateJsonSchemaUtil.classToJsonSchema(MyClass.class, true)

img.png

json-schema

官网:https://json-schema.org/

性能

<=1ms

About

校验JSON数据是否符合schema;类生成JSON schema字符串;

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages