public enum TimeFormat
{
@12-hour,
@24-hour
}
Hi,
I use newtonsoft deserializer for deserialize json string to an object.
JsonDeserializer checks enum parameter name. if it's same with json string. it converts string to enum.
Can I use Dash,Minus (-) character in an enum as enum parameter. I tried to use as above, But I couldn't compile project.
Then I tried this.
[JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public enum TimeFormat
{
[JsonProperty("12-hour")]
hour12,
[JsonProperty("24-hour")]
hour24,
}
Deserializer couldn't deserialize json string.
Error : Requested value '12-hour' was not foun
I fixed issue.
[JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public enum TimeFormat
{
[System.Runtime.Serialization.EnumMember(Value = "12-hour")]
hour12,
[System.Runtime.Serialization.EnumMember(Value = "24-hour")]
hour24,
}
StringEnumConverter
checks EnumMemberAttribute
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With