1

我在项目中有一个 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"
        }
    ]
}
4

1 回答 1

1

终于我实现了我所需要的。

这个想法是将自定义传递CSharpTypeResolverCSharpGenerator

新的 CSharpGenerator(jsonSchema4,设置,新的 CustomCSharpTypeResolver(设置,jsonSchema4),空);

好像不是NJsonSchema作者有意的。在CustomCSharpTypeResolver我覆盖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实例,所以我需要实现explicitimplicit操作符将其转换为生成的模型。

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更灵活会更好。

于 2017-04-22T07:58:39.503 回答