Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Dash (-) Character in enum parameter

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

like image 323
halit Avatar asked Feb 25 '13 14:02

halit


1 Answers

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.

like image 50
halit Avatar answered Oct 21 '22 14:10

halit