我在一个项目中有一个JSON模式,并希望添加构建步骤以从它们中生成类,这些模式包含一系列对象和字符串,简化了示例:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "testSchema",
"type": "object",
"properties": {
"array": {
"type": "array",
"items": {
"anyOf": [
{
"type": "string"
},
{
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
}
]
}
}
}
}
我正在使用NJSonschema从此架构中生成C#代码。结果,我将获得以下输出:
//----------------------
// <auto-generated>
// Generated using the NJsonSchema v8.32.6319.16936 (http://NJsonSchema.org)
// </auto-generated>
//----------------------
namespace TestSchema
{
#pragma warning disable // Disable all warnings
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "8.32.6319.16936")]
public partial class TestSchema : System.ComponentModel.INotifyPropertyChanged
{
private System.Collections.ObjectModel.ObservableCollection<Anonymous> _array = new System.Collections.ObjectModel.ObservableCollection<Anonymous>();
[Newtonsoft.Json.JsonProperty("array", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public System.Collections.ObjectModel.ObservableCollection<Anonymous> Array
{
get { return _array; }
set
{
if (_array != value)
{
_array = value;
RaisePropertyChanged();
}
}
}
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
public string ToJson()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(this);
}
public static TestSchema FromJson(string data)
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<TestSchema>(data);
}
protected virtual void RaisePropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "8.32.6319.16936")]
public partial class Anonymous : System.ComponentModel.INotifyPropertyChanged
{
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
public string ToJson()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(this);
}
public static Anonymous FromJson(string data)
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<Anonymous>(data);
}
protected virtual void RaisePropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}
因此,我有这个奇怪的Anonymous
类,如果我尝试在下面的JSON文件中进行验证,则会获得错误:string
无法将其转换为Anonymous
。为了挑选我使用以下生成的方法:
TestSchema.FromJson
可以将代码生成调整以使object
的集合并在其中获得具有正确类型的供应对象?
{
"array": [
"stringItem1",
{
"name": "complexObj1"
}
]
}
最后我实现了我所需要的。
这个想法是将自定义CSharpTypeResolver
传递到CSharpGenerator
:
新的csharpgenerator(jsonschema4,设置,新的CustomCsharptyPeresolver(设置,jsonschema4(,null(;
看来NJsonSchema
作者的意图不是打算。在CustomCSharpTypeResolver
中,i覆盖Resolve
方法以添加以下行为:
if (schema.AnyOf.Count > 0)
return "object";
作为简化示例的结果,我有以下模型:
//----------------------
// <auto-generated>
// Generated using the NJsonSchema v8.32.6319.16936 (http://NJsonSchema.org)
// </auto-generated>
//----------------------
namespace JsonSchemaClassGenerator.TestSchema
{
#pragma warning disable // Disable all warnings
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "8.32.6319.16936")]
public partial class TestSchema : System.ComponentModel.INotifyPropertyChanged
{
private System.Collections.ObjectModel.ObservableCollection<object> _array = new System.Collections.ObjectModel.ObservableCollection<object>();
[Newtonsoft.Json.JsonProperty("array", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public System.Collections.ObjectModel.ObservableCollection<object> Array
{
get { return _array; }
set
{
if (_array != value)
{
_array = value;
RaisePropertyChanged();
}
}
}
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
public string ToJson()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(this);
}
public static TestSchema FromJson(string data)
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<TestSchema>(data);
}
protected virtual void RaisePropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "8.32.6319.16936")]
public partial class Object : System.ComponentModel.INotifyPropertyChanged
{
private string _name;
[Newtonsoft.Json.JsonProperty("name", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string Name
{
get { return _name; }
set
{
if (_name != value)
{
_name = value;
RaisePropertyChanged();
}
}
}
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
public string ToJson()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(this);
}
public static Object FromJson(string data)
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<Object>(data);
}
protected virtual void RaisePropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}
避难所化效果很好。我可以按照自己的意愿施放对象。只有一个问题:将对象保存为JObject
实例,因此我需要实现explicit
或implicit
操作员将其转换为生成的模型。
namespace JsonSchemaClassGenerator.TestSchema
{
public partial class Object
{
public static implicit operator Object(JObject json)
{
return FromJson(json.ToString());
}
}
}
之后,将可以将JObject
转换为生成的模型(Object
不是System.Object
它只是以这种名称生成(:
Object a = config.Entries[1] as JObject;
这是我发现的最简单解决方案。我认为也可以实现自定义CSharpTypeResolver
以拥有更多类型安全的东西。但是不确定我是否会尝试,因为对我来说,最好先使NJsonSchema
更灵活。