11

I have an enum:

public enum IdleDelayBreakMode { Repeat, ShowNext } 

I'm using NewtonSoft.Json to convert to json objects containing properties of that enum's type. What's the best solution to serialize values of that enum to arbitrary integers? Ideally i would like to do something like on the snippet below and i want to know if maybe there's a built-in solution for that:

public enum IdleDelayBreakMode { [JsonValue(100)] Repeat, // When serializing will be converted to 100 [JsonValue(200)] ShowNext // When serializing will be converted to 200 } 
3
  • What if you just set their values? Repeat = 100 and ShowNext = 200. Commented Jul 14, 2016 at 19:05
  • You want to serialize the enum to different integers than how the enum is defined? Commented Jul 14, 2016 at 19:06
  • You need to create a class that inherits from JsonConverter, then apply this attribute to your enum [JsonConverter(typeof(MyConverter))] Commented Jul 14, 2016 at 19:07

3 Answers 3

11

You can just set enum values like that:

public enum IdleDelayBreakMode { Repeat = 100, ShowNext = 200 } 

Newtonsoft.Json will use enum values. There is no need to set attributes. This way it will be consistent across the system whether you need to serialize it to JSON, save it in the database using Entity Framework etc.


Are you using your enum as integer constants storage?

In this case you might want to inherit it from int:

public enum IdleDelayBreakMode : int 
Sign up to request clarification or add additional context in comments.

1 Comment

This doesn't work if the enum is used as a key in a dictionary.
7

Assuming you don't want to modify the underlying enum values (as is shown in the other answers), you can decorate your enum vales with [EnumMember(Value = "Name")] attributes and use the alternate numeric values as the name strings:

[JsonConverter(typeof(StringEnumConverter))] [DataContract] public enum IdleDelayBreakMode { [EnumMember(Value = "100")] Repeat, [EnumMember(Value = "200")] ShowNext } 

You will also need to serialize using StringEnumConverter, either by adding [JsonConverter(typeof(StringEnumConverter))] directly to the enum or by applying it globally according to this answer.

This works with [Flags] as well. Serializing both values of the following:

[Flags] [DataContract] [JsonConverter(typeof(StringEnumConverter))] public enum IdleDelayBreakModeFlags { [EnumMember(Value = "100")] Repeat = (1 << 0), [EnumMember(Value = "200")] ShowNext = (1 << 1), } 

produces "100, 200".

Adding these attributes will cause DataContractSerializer as well as Json.NET to use these alternate name strings. If you would prefer not to affect the behavior of the data contract serializer, remove [DataContract] but keep the [EnumMember] attributes.

Comments

5

Just set the integer values in your enum items directly instead of using an attribute:

public enum IdleDelayBreakMode { Repeat = 100, ShowNext = 200 } 

JSON.Net will them use the integer values when serializing/deserializing a property of type IdleDelayBreakMode

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.