将字段反序列化为空列表而不是空列表

时间:2021-05-17 16:45:34

If I have a class like this:

如果我有这样的课:

[DataContract(Name = "", Namespace = "")]
public class MyDataObject
{
    [DataMember(Name = "NeverNull")]
    public IList<int> MyInts { get; set; }
}

Is there a way I can make MyInts field a non-null empty list when the following string is deserialized?

是否有一种方法可以使MyInts字段在以下字符串被反序列化时成为非空列表?

string serialized = @"{""NeverNull"":null}";

MyDataObject myDataObject = JsonConvert.DeserializeObject<MyDataObject>(serialized);

I’m using Newtonsoft.Json

我用Newtonsoft.Json

The reason I ask is that I have a fairly complicated json request to parse, it contains nests of lists of objects and I'd like the deserialization code to create these object so I can avoid lots of null checks:

我问这个问题的原因是我有一个相当复杂的json请求要解析,它包含了对象列表的集合,我希望反序列化代码来创建这些对象,这样我就可以避免很多空检查:

if (foo.bar != null)
{
    foreach (var bar in foo.bar)
    {
        if (bar.baz != null)
        {
            foreach (var baz in bar.baz)
            {
                ...

2 个解决方案

#1


11  

Perhaps add a post-serialization callback that checks this at the end of deserialization?

或者添加一个后序列化回调,在反序列化结束时检查它?

[DataContract(Name = "", Namespace = "")]
public class MyDataObject
{
    [OnDeserialized]
    public void OnDeserialized(StreamingContext context)
    {
        if (MyInts == null) MyInts = new List<int>();
    }
    [DataMember(Name = "NeverNull")]
    public IList<int> MyInts { get; set; }
}

Note also that JsonConvert (unlike DataContractSerializer) executes the default constructor, so usually you could also have just added a default constructor:

还要注意,JsonConvert(不像DataContractSerializer)执行默认构造函数,所以通常你也可以添加一个默认构造函数:

    public MyDataObject()
    {
        MyInts = new List<int>();
    }

however, in this case the explict "NeverNull":null changes it back to null during deserialization, hence why I've used a callback above instead.

然而,在这种情况下,显式的“NeverNull”:null在反序列化期间将它更改为null,因此我使用了上面的回调。

#2


1  

Initialization of IList<int>with new int[0] will help you out! Its the solution that gives me the best results.

使用新的int[0]初始化IList 将帮助您解决问题!它的解决方案给了我最好的结果。

#1


11  

Perhaps add a post-serialization callback that checks this at the end of deserialization?

或者添加一个后序列化回调,在反序列化结束时检查它?

[DataContract(Name = "", Namespace = "")]
public class MyDataObject
{
    [OnDeserialized]
    public void OnDeserialized(StreamingContext context)
    {
        if (MyInts == null) MyInts = new List<int>();
    }
    [DataMember(Name = "NeverNull")]
    public IList<int> MyInts { get; set; }
}

Note also that JsonConvert (unlike DataContractSerializer) executes the default constructor, so usually you could also have just added a default constructor:

还要注意,JsonConvert(不像DataContractSerializer)执行默认构造函数,所以通常你也可以添加一个默认构造函数:

    public MyDataObject()
    {
        MyInts = new List<int>();
    }

however, in this case the explict "NeverNull":null changes it back to null during deserialization, hence why I've used a callback above instead.

然而,在这种情况下,显式的“NeverNull”:null在反序列化期间将它更改为null,因此我使用了上面的回调。

#2


1  

Initialization of IList<int>with new int[0] will help you out! Its the solution that gives me the best results.

使用新的int[0]初始化IList 将帮助您解决问题!它的解决方案给了我最好的结果。