3

我在让我的代码(再次)工作时遇到问题。可悲的是它正在工作,但我不知道为什么它现在不起作用。

加载模式的代码示例:

// ----------------- 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 -----------------


json 模式的主文件:

{
    "$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" }
    }
}


对其他模式文件的引用将无法解析!

我按照链接中的说明进行操作: java json schema validation relative path not working (URI not found)

有人知道如何以相对方式解决引用吗?

@Sabir Khan
我在 json 模式文件中没有改变任何东西!我只是更改了一些代码行的顺序。我没有任何例外。它只是不能解决裁判。

前:

    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();
    }


后:

    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
    }
}


这是 MyBoolean_1.json 文件:

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

1 回答 1

0

我找到了我的问题的答案。

我将代码改回:

jsonSchema = new File("./src/main


我发现的下一件事是:

如果我将架构文件更改为,解析器不会抛出任何错误消息

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

并且 data.json 仍然保持不变!

解析器是否有可能告诉我们数据文件中有其他信息,但模式文件中没有描述!??

于 2015-12-25T14:18:10.867 回答