简体   繁体   English

使用RestSharp反序列化时是否可以实例化Null值

[英]Is it possible to instantiate Null values while deserializing with RestSharp

How to default-ly instantiate null values while deserializing with RestSharp. 在使用RestSharp反序列化时,如何默认实例化null值。

For example you have this POCO 例如你有这个POCO

public class Address {
    public string Street { get; set; }
    public string City { get; set; }
    public int Number { get; set; }
    public string Postal { get; set; }
}

public class Root {
    public string Name { get; set; }
    public Address Address { get; set; }
}

And this JSON: 而这个JSON:

{ 
    "Root": {
        "Name" : "John",
        "Address" : null 
    }
}

Is it possible when I do RestClient.ExecuteAsGet<Root>( ... ) , that even though the Address is null, it still gets instantiated ( new Address() )? 当我执行RestClient.ExecuteAsGet<Root>( ... )时,即使Address为null,它仍会实例化( new Address() )吗?

This may have changed in newer versions of RestSharp, but objects should be null by default if they are not present or null. 在较新的RestSharp版本中,这可能已更改,但是如果对象不存在,则默认情况下应为null或null。

You can accomplish this with value types by using Nullable . 您可以使用Nullable使用值类型完成此操作。 https://msdn.microsoft.com/en-us/library/b3h38hb0.aspx https://msdn.microsoft.com/zh-CN/library/b3h38hb0.aspx

public class Root {
    public Nullable<DateTime> date { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM