JSON Development Notes (IV) - JSON Schema Actual Warfare (II)

Keywords: JSON Java Apache github

1. JSON Schema Third Party Auxiliary Tool

JSON Schema already has a number of third-party assistive tools based on different languages. You can see the instructions on the official website for details: http://json-schema.org/implementations . Among these third-party tools, we focus on the third-party tools available in the Java language. Currently, Java has the following third-party tools to choose from:

In fact, three third-party tools, fge, everit and networknt, are quite different. Some people have done performance tests on them on the Internet. The specific address is as follows: https://github.com/networknt/json-schema-validator-perftest . Personally, I feel that this performance test is not the only reference standard for selecting tools. After all, when we choose a third-party tool, performance is only one of the considerations, not the only criterion that determines our choice. Moreover, I think it's better to combine application scenarios and use more targeted test data sources for targeted comparison and analysis, which is also more stable. Furthermore, we need to consider whether the error hints given by third-party tools are accurate and clear enough to understand when the JSON Schema validation fails.

2. Introduction to FGE tools

The third-party tool I'm using now is fge. Here's a brief introduction to its usage.

2.1 pom.xml file configuration

<!-- fge -->
<dependency>  
    <groupId>com.github.fge</groupId>  
    <artifactId>json-schema-validator</artifactId>  
    <version>2.2.6</version>    
</dependency>
<!-- fasterxml -->
<dependency>  
    <groupId>com.fasterxml.jackson.core</groupId>  
    <artifactId>jackson-core</artifactId>  
    <version>2.3.0</version>    
</dependency>  
<dependency>  
    <groupId>com.fasterxml.jackson.core</groupId>  
    <artifactId>jackson-databind</artifactId>  
    <version>2.3.0</version>    
</dependency>  

Note that the latest stable version of fge is 2.2.6, and the fasterxml version should be selected on demand.

2.2 Code Example

The core methods of JSON Schema verification are as follows:

public static void validateJsonByFgeByJsonNode(JsonNode jsonNode, JsonNode schemaNode){
    ProcessingReport report = null;
    report = JsonSchemaFactory.byDefault().getValidator().validateUnchecked(schemaNode, jsonNode);
    if (report.isSuccess()) {
        // Verification success
        System.out.println("Check success!");
    }else {
        System.out.println("Check failed!");
        Iterator<ProcessingMessage> it = report.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
    }
}

Get JsonNode from String:

String json = "{\"foo\":1234}";
JsonNode jsonNode = JsonLoader.fromString(json);

Get JsonNode from the file address:

String filePath = "src/test/resources/Test.json";
JsonNode jsonNode = null;
try {
    jsonNode = new JsonNodeReader().fromReader(new FileReader(filePath));
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

Note: According to their own project situation and specific application background, corresponding modifications can be made.

Posted by ol4pr0 on Wed, 05 Jun 2019 14:46:01 -0700