예제 #1
0
 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));
 }
예제 #2
0
        public TEnum Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
        {
            if (reader.ReadIsNull())
            {
                return(null);
            }

            return(SmartEnum <TEnum, TValue> .FromValue(ReadValue(ref reader)));
        }
예제 #3
0
 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);
     }
 }
예제 #4
0
        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);
            }
        }