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