简体   繁体   中英

JSON Schema Reference

I have a problem getting my code to work (again). The sad thing is that it was working, but I do not know why it does not work now.

Code sample of loading the schema:

// ----------------- JSON Schema -----------------
jsonSchema = new File("src/main/java/de/project/jsonvalidator/test1/test.json");
final URI uri = jsonSchema.toURI();
System.out.println("URI = " + uri.toString());
final JsonSchemaFactory factory = JsonSchemaFactory.byDefault(); 
final com.github.fge.jsonschema.main.JsonSchema schema = factory.getJsonSchema(uri.toString());
// ----------------- JSON Schema -----------------


The main file for the json schema:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "description" : "schema validating people and vehicles",
    "type" : "object",
    "properties": {
        "billing_address": { "$ref": "MyBoolean.json#/MyBool" },
        "shipping_address": { "$ref": "MyBoolean_1.json#/MyBoolABC" }
    }
}


The references to the other schema files will not resolved!

I followed the instructions at the link: java json schema validation relative path not working (URI not found)

Has somebody an idea how to resolve the references in a relative way?

@Sabir Khan
I changed nothing at the json schema files! I just changed the order of some code lines. I don't get any exception. It just doesn't solve the refs.

Before:

    ProcessingReport report = null;
    boolean result = false;
    File jsonSchema = null;
    File jsonData = null;
    try {
        jsonSchema = new File("./src/main/java/de/project/jsonvalidator/test1/test.json");
        final URI uri = jsonSchema.toURI();
        final JsonSchemaFactory factory = JsonSchemaFactory.byDefault(); 

        jsonData = new File("./src/main/java/de/project/jsonvalidator/test1/data.json");
        JsonNode data = JsonLoader.fromFile(jsonData);
        final com.github.fge.jsonschema.main.JsonSchema schema = factory.getJsonSchema(uri.toString());

        report = schema.validate(data, true);

        System.out.println("Success = " + report.isSuccess());
        Iterator<ProcessingMessage> it = report.iterator();
        while(it.hasNext())
            System.out.println("msg = " + it.next().getMessage());

    } catch (JsonParseException jpex) {
        System.out.println("Error. Something went wrong trying to parse json data: #<#<"
                + jsonData
                + ">#># or json schema: @<@<"
                + jsonSchema
                + ">@>@. Are the double quotes included? "+jpex.getMessage());
        jpex.printStackTrace();

    } catch (ProcessingException pex) {  
        System.out.println("Error. Something went wrong trying to process json data: #<#<"
                + jsonData
                + ">#># with json schema: @<@<"
                + jsonSchema
                + ">@>@ "+pex.getMessage());
        pex.printStackTrace();

    } catch (IOException e) {
        System.out.println("Error. Something went wrong trying to read json data: #<#<"
                + jsonData
                + ">#># or json schema: @<@<"
                + jsonSchema
                + ">@>@");
        e.printStackTrace();

    } catch ( Exception ex) {
         ex.printStackTrace();
    }


After:

    ProcessingReport report = null;
    boolean result = false;
    File jsonSchema = null;
    File jsonData = null;
    try {
        //File jsonSchema = new File("./src/main/java/de/project/jsonvalidator/test/test.json");

        // ----------------- JSON Schema -----------------
        jsonSchema = new File("src/main/java/de/project/jsonvalidator/test1/test.json");
        final URI uri = jsonSchema.toURI();
        System.out.println("URI = " + uri.toString());
        //JsonNode jnSchema = JsonLoader.fromFile(jsonSchema);
        final JsonSchemaFactory factory = JsonSchemaFactory.byDefault(); 
        final com.github.fge.jsonschema.main.JsonSchema schema = factory.getJsonSchema(uri.toString());
        // ----------------- JSON Schema -----------------


        // ----------------- JSON Daten -----------------
        jsonData = new File("src/main/java/de/project/jsonvalidator/test1/data.json");
        JsonNode data = JsonLoader.fromFile(jsonData);
        // ----------------- JSON Daten -----------------


        // ----------------- JSON Validierung -----------------
        //boolean ret = schema.validInstance(jnSchema);
        report = schema.validate(data, true);
        // ----------------- JSON Validierung -----------------


        // ----------------- JSON Auswertung -----------------
        //System.out.println("ret = " + ret);
        System.out.println("Success = " + report.isSuccess());
        Iterator<ProcessingMessage> it = report.iterator();
        while(it.hasNext())
            System.out.println("msg = " + it.next().getMessage());
        // ----------------- JSON Auswertung -----------------



    } catch (JsonParseException jpex) {
        System.out.println("Error. Something went wrong trying to parse json data: #<#<"
                + jsonData
                + ">#># or json schema: @<@<"
                + jsonSchema
                + ">@>@. Are the double quotes included? "+jpex.getMessage());
        jpex.printStackTrace();

    } catch (ProcessingException pex) {  
        System.out.println("Error. Something went wrong trying to process json data: #<#<"
                + jsonData
                + ">#># with json schema: @<@<"
                + jsonSchema
                + ">@>@ "+pex.getMessage());
        pex.printStackTrace();

    } catch (IOException e) {
        System.out.println("Error. Something went wrong trying to read json data: #<#<"
                + jsonData
                + ">#># or json schema: @<@<"
                + jsonSchema
                + ">@>@");
        e.printStackTrace();

    } catch ( Exception ex) {
         ex.printStackTrace();
    }


MyBoolean.json

{
    "MyBool": {
        "type": "object",
        "properties": {
            "value" :{
                "type": "string",
                "enum": [ "true", "false", "file not found" ]
            }
        },
        "required": ["value"],
        "additionalProperties": false
    }
}


and here is the MyBoolean_1.json file:

{
    "MyBoolABC": {
        "type": "object",
        "properties": {
            "value1" :{
                "type": "string",
                "enum": [ "true", "false", "file not found" ]
            }
        },
        "required": ["value1"],
        "additionalProperties": false
    }
}

I found an answer to my question.

I changed the code back to:

jsonSchema = new File("./src/main


The next thing I found out is:

That the parser doesn't throw any error message if I change the schema file to

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "description" : "schema validating people and vehicles",
    "type" : "object",
    "properties": {
        "billing_address": { "$ref": "MyBoolean.json#/MyBool" }
    }
}

and the data.json still remains the same!!

Is there any posibility that the parser tell us that there additional informations in the data file, but it is not described in the schema file!??

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM