StringEnumConverterWithDefault.cs
1 using System;
2 using Newtonsoft.Json;
3 using Newtonsoft.Json.Converters;
4 
5 namespace Microsoft.Bot.Builder.Calling.ObjectModel.Misc
6 {
11  public class StringEnumConverterWithDefault<TEnum> : StringEnumConverter where TEnum : struct
12  {
14  {
15  this.CamelCaseText = true;
16  }
17 
18  public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
19  {
20  string s = reader.Value.ToString();
21  TEnum t;
22  Enum.TryParse<TEnum>(s, ignoreCase: true, result: out t);
23  return t;
24  }
25  }
26 }
override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
Specialized StringEnumConverter that returns the default enum value instead of throwing if the string...