Ejemplo n.º 1
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            bool flag = ReflectionUtils.IsNullableType(objectType);
            Type type = flag ? Nullable.GetUnderlyingType(objectType) : objectType;

            if (reader.TokenType != JsonToken.Null)
            {
                try
                {
                    if (reader.TokenType == JsonToken.String)
                    {
                        string text = reader.Value.ToString();
                        object result;
                        if (text == string.Empty && flag)
                        {
                            result = null;
                            return(result);
                        }
                        BidirectionalDictionary <string, string> enumNameMap = this.GetEnumNameMap(type);
                        string value;
                        if (text.IndexOf(',') != -1)
                        {
                            string[] array = text.Split(new char[]
                            {
                                ','
                            });
                            for (int i = 0; i < array.Length; i++)
                            {
                                string enumText = array[i].Trim();
                                array[i] = StringEnumConverter.ResolvedEnumName(enumNameMap, enumText);
                            }
                            value = string.Join(", ", array);
                        }
                        else
                        {
                            value = StringEnumConverter.ResolvedEnumName(enumNameMap, text);
                        }
                        result = Enum.Parse(type, value, true);
                        return(result);
                    }
                    else if (reader.TokenType == JsonToken.Integer)
                    {
                        if (!this.AllowIntegerValues)
                        {
                            throw JsonSerializationException.Create(reader, "Integer value {0} is not allowed.".FormatWith(CultureInfo.InvariantCulture, reader.Value));
                        }
                        object result = ConvertUtils.ConvertOrCast(reader.Value, CultureInfo.InvariantCulture, type);
                        return(result);
                    }
                }
                catch (Exception ex)
                {
                    throw JsonSerializationException.Create(reader, "Error converting value {0} to type '{1}'.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.FormatValueForPrint(reader.Value), objectType), ex);
                }
                throw JsonSerializationException.Create(reader, "Unexpected token {0} when parsing enum.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
            }
            if (!ReflectionUtils.IsNullableType(objectType))
            {
                throw JsonSerializationException.Create(reader, "Cannot convert null value to {0}.".FormatWith(CultureInfo.InvariantCulture, objectType));
            }
            return(null);
        }