2015-12-25 23 views
3

我有一个问题让我的代码工作(再次)。可悲的是它在工作,但我不知道为什么它现在不起作用。JSON Schema Reference

加载模式的代码示例:

// ----------------- 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 
    } 
} 
+0

哪条线路发生故障并收到什么错误消息?在停止工作之前什么工作,什么改变了?编辑您的问题以提供这些详细信息。 –

回答

0

我发现一个回答我的问题。

我改变了代码回:


我发现,接下来的事情就是:

,如果我更改架构文件

解析器不会引发任何错误信息
{ 
    "$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仍然保持不变!

解析器告诉我们数据文件中有附加信息,但在模式文件中没有描述!