4

I have string property in my class_ for example

        [DataMember]
        [JsonProperty(PropertyName = "email")]
        [StringLength(40, ErrorMessage = "The Mobile value cannot exceed 40 characters. ")]
        public string Email { get; set; }

By some reason during Convert.Deserialize process I need to have empty string in this property instead on null in case this value is not setup in JSON object. How to do it ?

arrowd
  • 33,231
  • 8
  • 79
  • 110
Arbejdsglæde
  • 13,670
  • 26
  • 78
  • 144

1 Answers1

8

You could use the DefaultValue attribute.

Decorate it as

[DataMember]
[JsonProperty(PropertyName = "email", DefaultValueHandling = DefaultValueHandling.Populate)]
[StringLength(40, ErrorMessage = "The Mobile value cannot exceed 40 characters. ")]
[DefaultValue("")]
public string Email { get; set; }
bushed
  • 1,050
  • 1
  • 15
  • 27
Srikanth Venugopalan
  • 9,011
  • 3
  • 36
  • 76