public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (int.TryParse((string)value, out int fromInteger)) { return(SmartEnum <TEnumeration, string> .FromValue(fromInteger.ToString())); } return(SmartEnum <TEnumeration, string> .FromValue((string)value)); }
public TEnum Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) { if (reader.ReadIsNull()) { return(null); } return(SmartEnum <TEnum, TValue> .FromValue(ReadValue(ref reader))); }
private TEnum GetFromValue(TValue value) { try { return(SmartEnum <TEnum, TValue> .FromValue(value)); } catch (Exception ex) { throw new JsonException($"Error converting value '{value}' to a smart enum.", ex); } }
public override TEnum ReadJson(JsonReader reader, Type objectType, TEnum existingValue, bool hasExistingValue, JsonSerializer serializer) { try { TValue value; if (reader.TokenType == JsonToken.Integer && typeof(TValue) != typeof(long) && typeof(TValue) != typeof(bool)) { value = (TValue)Convert.ChangeType(reader.Value, typeof(TValue)); } else { value = (TValue)reader.Value; } return(SmartEnum <TEnum, TValue> .FromValue(value)); } catch (Exception ex) { throw new JsonSerializationException($"Error converting {reader.Value ?? "Null"} to {objectType.Name}.", ex); } }