我想使用 NJsonSchema 生成 C# 类。问题是对于嵌套数组,类型最终看起来像这样:
System.Collections.ObjectModel.ObservableCollection<System.Tuple<System.Linq.Enumerable+WhereSelectEnumerableIterator2[NJsonSchema.JsonSchema4,System.String]>>
我用于生成类的测试代码如下所示:
var schemaAsync = NJsonSchema.JsonSchema4.FromFileAsync(
@"<myPath>/MyFile.json");
var schema = schemaAsync.Result;
var generator = new NJsonSchema.CodeGeneration.CSharp.CSharpGenerator(schema);
var file = generator.GenerateFile();
System.IO.File.WriteAllText(
@"<myPath>/SomeClass.cs",
file);
我有两个 JSON 模式文件,还有定义文件(Def1.json):
{
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"InnerList": {
"description": "Only ever has 2 items",
"type": "array",
"items": [
{
"type": "integer",
"minimum": 0,
"maximum": 100,
"default": 0
},
{
"type": "integer",
"minimum": 0,
"maximum": 100,
"default": 0
}
],
"additionalItems": false
},
"OuterList": {
"type": "array",
"items": {
"$ref": "#/definitions/InnerList"
}
},
}
}
还有一个使用定义文件的单独模式:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "MySchema",
"type": "object",
"required": [ "OuterList" ],
"properties": {
"OuterList": { "$ref": "Def1.json#/definitions/OuterList" }
},
"additionalProperties": false
}
我是否错误地使用了图书馆?