简体   繁体   English

如果在反序列化期间字符串为null,则设置默认值

[英]Put a default value if the string is null during deserializing

I have a model like this: 我有这样的模型:

public class Artwork
{
    public string small { get; set; }
    public string medium { get; set; }
    public string large { get; set; }
    public string @default { get; set; }
}

I use this model to deserialize artwork path. 我使用此模型反序列化图稿路径。 When one of these strings is null I want to put a default path so I can still display a picture. 当其中一个字符串为null时,我想放置一个默认路径,这样我仍然可以显示图片。 I tried to use some attribute from Json.NET library: 我试图使用Json.NET库中的一些属性:

[DefaultValue("path/to/any/picture")]
[JsonProperty(DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]

But it does not work, when deserializing it throws an "System.NullReferenceException". 但是当它反序列化时抛出“System.NullReferenceException”它不起作用。

How can I manage to display these pictures when I get a path when deserializing and display a default picture when the path is null? 当我在反序列化时获得路径并在路径为空时显示默认图片时,如何设置显示这些图片?

I managed to solve my problem doing like this 我设法像这样解决了我的问题

public class Artwork
{
    private static string defaultAtwork = "https://pixabay.com/static/uploads/photo/2016/01/06/07/06/bokeh-1123696_960_720.jpg";
    private string _small = defaultAtwork;
    private string _medium = defaultAtwork;
    private string _large = defaultAtwork;
    private string _default = defaultAtwork;

    public string small
    {
        get { return _small; }
        set { _small = value; }
    } 
    ...
}

public class Album
{
    private Artwork _artwork = new Artwork();
    [JsonProperty(NullValueHandling = NullValueHandling.Include)]
    public Artwork artwork
    {
        get { return _artwork; }
        set { _artwork = value; }
    }

暂无
暂无

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

相关问题 System.Text.Json:反序列化时,如何忽略在构造函数中设置了默认值的属性的 null 或空字符串值? - System.Text.Json: when deserializing, how can I ignore a null or empty string value for a property that has a default value set in the constructor? 为反序列化XML时为null的属性设置默认值 - Set default value for attributes which are null while deserializing XML RestSharp 不反序列化字符串(始终为空) - RestSharp not deserializing a string (always null) 使用自定义 ContractResolver,在将 null JSON 属性反序列化为值类型成员时如何设置默认值而不是 null? - Using a custom ContractResolver, how to set a default value instead of null when deserializing a null JSON property to a value-type member? MVC保存空字符串默认值null - MVC save empty string default value null 如果实体中的字符串为null,则在SQL中使用默认值 - Use default value in SQL if string in entity is null 如果默认值为null并转换为字符串会发生什么 - what happens if default value is null and convert to string JSON反序列化字符串值到列表 <T> 使用Newtonsoft-构造函数arg在C#中为null - JSON Deserializing string value to List<T> using Newtonsoft - constructor arg is null in C# 通过字符串转换反序列化枚举时,如何获取空值而不是序列化错误? - How can I get a null value instead of a serialization error when deserializing an enum by string conversion? 使用 JavaScriptSerializer 获取 NULL 反序列化 JSON 字符串 - Getting NULL Deserializing JSON string using JavaScriptSerializer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM