2017-04-04 27 views
0

我生成的Json架構不在當前存在的類中。自動生成的架構僅在內存中完成時才起作用

JSchemaGenerator generator = new JSchemaGenerator();      
JSchema schema = generator.Generate(typeof(Client)); 

這將驗證正常,但是,我需要把對依賴關係(你不能從類做的),所以我複製架構結果到一個文件中。現在,該文件將在http://www.jsonschemavalidator.net/上驗證爲無效。然而,當我嘗試使用加載了下列文件:

using (StreamReader file = File.OpenText("c:\\myJson.json")) 
{ 
    file.BaseStream.Position = 0; 

    using (JsonTextReader reader = new JsonTextReader(file)) 
    { 
     JSchema schema2 = JSchema.Load(reader); 
    } 
} 

我總是會得到錯誤的任何內部引用的文件中:

"Contact": {"$ref": "#/definitions/Contact"}, 

An exception of type 'Newtonsoft.Json.Schema.JSchemaReaderException' 
occurred in Newtonsoft.Json.Schema.dll but was not handled in user code 

Additional information: Could not resolve schema reference 
'#/definitions/Contact/ Path 'definitions.Contact' Line 120, position 20 

爲什麼會變成這樣的罰款,如果在內存中完成,但如果從文件加載將失敗?

+1

模式的定義部分是否存在模式「聯繫人」? –

回答

0

所以我發現了這個問題。雖然您可以從現有模型生成架構,但它可能無法正確執行。

我們發現一些引用的類在其中添加了一個額外的Item層(不知道爲什麼)一旦我們在最後刪除了「Item」的引用,它工作正常。它還在對象中生成了第二個Item層,我必須刪除它才能驗證該對象。