예제 #1
0
        public TEnum Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
        {
            if (reader.ReadIsNull())
            {
                return(null);
            }

            var name = reader.ReadString();

            return(SmartEnum <TEnum, TValue> .FromName(name));
        }
예제 #2
0
 private TEnum GetFromName(string name)
 {
     try
     {
         return(SmartEnum <TEnum, TValue> .FromName(name));
     }
     catch (Exception ex)
     {
         throw new JsonException($"Error converting value '{name}' to a smart enum.", ex);
     }
 }
        public TEnum Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            if (MessagePackBinary.IsNil(bytes, offset))
            {
                readSize = 1;
                return(null);
            }

            var name = MessagePackBinary.ReadString(bytes, offset, out readSize);

            return(SmartEnum <TEnum, TValue> .FromName(name));
        }
        public override TEnum ReadJson(JsonReader reader, Type objectType, TEnum existingValue, bool hasExistingValue, JsonSerializer serializer)
        {
            switch (reader.TokenType)
            {
            case JsonToken.String:
                return(GetFromName((string)reader.Value));

            default:
                throw new JsonSerializationException($"Unexpected token {reader.TokenType} when parsing a smart enum.");
            }

            TEnum GetFromName(string name)
            {
                try
                {
                    return(SmartEnum <TEnum, TValue> .FromName(name, false));
                }
                catch (Exception ex)
                {
                    throw new JsonSerializationException($"Error converting value '{name}' to a smart enum.", ex);
                }
            }
        }
예제 #5
0
        public TEnum Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
        {
            var name = reader.ReadString();

            return(SmartEnum <TEnum, TValue> .FromName(name));
        }