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 ?

1 Answer 1

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; }
Sign up to request clarification or add additional context in comments.

2 Comments

It doesn't work by without, [JsonProperty(PropertyName = "email",DefaultValueHandling = DefaultValueHandling.Populate)]
@cleric - thanks for the update. Yes, I missed that, its been a while, sorry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.