2015-12-25 21 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仍然保持不變!

解析器告訴我們數據文件中有附加信息,但在模式文件中沒有描述!