Exemplo n.º 1
0
 public override JToken this[object key]
 {
     get
     {
         ValidationUtils.ArgumentNotNull(key, "o");
         if (!(key is int))
         {
             throw new ArgumentException("Accessed JConstructor values with invalid key value: {0}. Argument position index expected.".FormatWith(CultureInfo.get_InvariantCulture(), new object[]
             {
                 MiscellaneousUtils.ToString(key)
             }));
         }
         return(this.GetItem((int)key));
     }
     set
     {
         ValidationUtils.ArgumentNotNull(key, "o");
         if (!(key is int))
         {
             throw new ArgumentException("Set JConstructor values with invalid key value: {0}. Argument position index expected.".FormatWith(CultureInfo.get_InvariantCulture(), new object[]
             {
                 MiscellaneousUtils.ToString(key)
             }));
         }
         this.SetItem((int)key, value);
     }
 }
Exemplo n.º 2
0
 public override JToken this[object key]
 {
     get
     {
         ValidationUtils.ArgumentNotNull(key, "o");
         string text = key as string;
         if (text == null)
         {
             throw new ArgumentException("Accessed JObject values with invalid key value: {0}. Object property name expected.".FormatWith(CultureInfo.InvariantCulture, new object[]
             {
                 MiscellaneousUtils.ToString(key)
             }));
         }
         return(this[text]);
     }
     set
     {
         ValidationUtils.ArgumentNotNull(key, "o");
         string text = key as string;
         if (text == null)
         {
             throw new ArgumentException("Set JObject values with invalid key value: {0}. Object property name expected.".FormatWith(CultureInfo.InvariantCulture, new object[]
             {
                 MiscellaneousUtils.ToString(key)
             }));
         }
         this[text] = value;
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Gets the <see cref="JToken"/> with the specified key.
        /// </summary>
        /// <value>The <see cref="JToken"/> with the specified key.</value>
        public override JToken?this[object key]
        {
            get
            {
                ValidationUtils.ArgumentNotNull(key, nameof(key));

                if (!(key is int))
                {
                    throw new ArgumentException(
                              "Accessed JArray values with invalid key value: {0}. Int32 array index expected.".FormatWith(
                                  CultureInfo.InvariantCulture,
                                  MiscellaneousUtils.ToString(key)
                                  )
                              );
                }

                return(GetItem((int)key));
            }
            set
            {
                ValidationUtils.ArgumentNotNull(key, nameof(key));

                if (!(key is int))
                {
                    throw new ArgumentException(
                              "Set JArray values with invalid key value: {0}. Int32 array index expected.".FormatWith(
                                  CultureInfo.InvariantCulture,
                                  MiscellaneousUtils.ToString(key)
                                  )
                              );
                }

                SetItem((int)key, value);
            }
        }
Exemplo n.º 4
0
 public override JToken this[object key]
 {
     get
     {
         ValidationUtils.ArgumentNotNull(key, "o");
         if (!(key is int))
         {
             throw new ArgumentException(StringUtils.FormatWith("Accessed JConstructor values with invalid key value: {0}. Argument position index expected.", (IFormatProvider)CultureInfo.InvariantCulture, (object)MiscellaneousUtils.ToString(key)));
         }
         else
         {
             return(this.GetItem((int)key));
         }
     }
     set
     {
         ValidationUtils.ArgumentNotNull(key, "o");
         if (!(key is int))
         {
             throw new ArgumentException(StringUtils.FormatWith("Set JConstructor values with invalid key value: {0}. Argument position index expected.", (IFormatProvider)CultureInfo.InvariantCulture, (object)MiscellaneousUtils.ToString(key)));
         }
         this.SetItem((int)key, value);
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// Gets the <see cref="JToken"/> with the specified key.
        /// </summary>
        /// <value>The <see cref="JToken"/> with the specified key.</value>
        public override JToken this[object key]
        {
            get
            {
                ValidationUtils.ArgumentNotNull(key, "o");

                if (!(key is int))
                {
                    throw new ArgumentException("Accessed JArray values with invalid key value: {0}. Array position index expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key)));
                }

                return(GetIndex(this, (int)key));
            }
        }
        /// <summary>
        /// Reads the JSON representation of the object.
        /// </summary>
        /// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
        /// <param name="objectType">Type of the object.</param>
        /// <param name="existingValue">The existing value of object being read.</param>
        /// <param name="serializer">The calling serializer.</param>
        /// <returns>The object value.</returns>
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
            {
                if (!ReflectionUtils.IsNullableType(objectType))
                {
                    throw JsonSerializationException.Create(reader, "Cannot convert null value to {0}.".FormatWith(CultureInfo.InvariantCulture, objectType));
                }

                return(null);
            }

            bool isNullable = ReflectionUtils.IsNullableType(objectType);
            Type t          = isNullable ? Nullable.GetUnderlyingType(objectType) : objectType;

            try
            {
                if (reader.TokenType == JsonToken.String)
                {
                    string enumText = reader.Value.ToString();

                    if (enumText == string.Empty && isNullable)
                    {
                        return(null);
                    }

                    return(EnumUtils.ParseEnum(t, enumText, !AllowIntegerValues));
                }

                if (reader.TokenType == JsonToken.Integer)
                {
                    if (!AllowIntegerValues)
                    {
                        throw JsonSerializationException.Create(reader, "Integer value {0} is not allowed.".FormatWith(CultureInfo.InvariantCulture, reader.Value));
                    }

                    return(ConvertUtils.ConvertOrCast(reader.Value, CultureInfo.InvariantCulture, t));
                }
            }
            catch (Exception ex)
            {
                throw JsonSerializationException.Create(reader, "Error converting value {0} to type '{1}'.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(reader.Value), objectType), ex);
            }

            // we don't actually expect to get here.
            throw JsonSerializationException.Create(reader, "Unexpected token {0} when parsing enum.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Gets the <see cref="JToken"/> with the specified key.
        /// </summary>
        /// <value>The <see cref="JToken"/> with the specified key.</value>
        public override JToken this[object key]
        {
            get
            {
                ValidationUtils.ArgumentNotNull(key, "o");

                string propertyName = key as string;
                if (propertyName == null)
                {
                    throw new ArgumentException("Accessed JObject values with invalid key value: {0}. Object property name expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key)));
                }

                JProperty property = Property(propertyName);

                return((property != null) ? property.Value : null);
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Gets the <see cref="JToken"/> with the specified key.
        /// </summary>
        /// <value>The <see cref="JToken"/> with the specified key.</value>
        public override JToken this[object key]
        {
            get
            {
                ValidationUtils.ArgumentNotNull(key, nameof(key));

                string propertyName = key as string;
                if (propertyName == null)
                {
                    throw new ArgumentException("Accessed JObject values with invalid key value: {0}. Object property name expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key)));
                }

                return this[propertyName];
            }
            set
            {
                ValidationUtils.ArgumentNotNull(key, nameof(key));

                string propertyName = key as string;
                if (propertyName == null)
                {
                    throw new ArgumentException("Set JObject values with invalid key value: {0}. Object property name expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key)));
                }

                this[propertyName] = value;
            }
        }
Exemplo n.º 9
0
        public override JToken this[object key]
        {
            [return : Nullable(2)]
            get
            {
                ValidationUtils.ArgumentNotNull(key, "key");

                if (key is int)
                {
                    int index = (int)key;
                    return(this.GetItem(index));
                }
                throw new ArgumentException("Accessed JConstructor values with invalid key value: {0}. Argument position index expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key)));
            }
            [param: Nullable(2)]
            set
            {
                ValidationUtils.ArgumentNotNull(key, "key");
                if (key is int)
                {
                    int index = (int)key;
                    this.SetItem(index, value);
                    return;
                }
                throw new ArgumentException("Set JConstructor values with invalid key value: {0}. Argument position index expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key)));
            }
        }
Exemplo n.º 10
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            object obj;

            if (reader.TokenType == JsonToken.Null)
            {
                if (!ReflectionUtils.IsNullableType(objectType))
                {
                    throw JsonSerializationException.Create(reader, "Cannot convert null value to {0}.".FormatWith(CultureInfo.InvariantCulture, objectType));
                }
                return(null);
            }
            bool flag  = ReflectionUtils.IsNullableType(objectType);
            bool flag1 = flag;
            Type type  = (flag ? Nullable.GetUnderlyingType(objectType) : objectType);

            try
            {
                if (reader.TokenType == JsonToken.String)
                {
                    string str = reader.Value.ToString();
                    obj = (!((str == string.Empty) & flag1) ? EnumUtils.ParseEnum(type, this.NamingStrategy, str, !this.AllowIntegerValues) : null);
                }
                else if (reader.TokenType != JsonToken.Integer)
                {
                    throw JsonSerializationException.Create(reader, "Unexpected token {0} when parsing enum.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
                }
                else
                {
                    if (!this.AllowIntegerValues)
                    {
                        throw JsonSerializationException.Create(reader, "Integer value {0} is not allowed.".FormatWith(CultureInfo.InvariantCulture, reader.Value));
                    }
                    obj = ConvertUtils.ConvertOrCast(reader.Value, CultureInfo.InvariantCulture, type);
                }
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                throw JsonSerializationException.Create(reader, "Error converting value {0} to type '{1}'.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(reader.Value), objectType), exception);
            }
            return(obj);
        }
Exemplo n.º 11
0
 public override JToken this[object key]
 {
     get
     {
         ValidationUtils.ArgumentNotNull(key, "o");
         string index = key as string;
         if (index == null)
         {
             throw new ArgumentException(StringUtils.FormatWith("Accessed JObject values with invalid key value: {0}. Object property name expected.", (IFormatProvider)CultureInfo.InvariantCulture, (object)MiscellaneousUtils.ToString(key)));
         }
         else
         {
             return(this[index]);
         }
     }
     set
     {
         ValidationUtils.ArgumentNotNull(key, "o");
         string index = key as string;
         if (index == null)
         {
             throw new ArgumentException(StringUtils.FormatWith("Set JObject values with invalid key value: {0}. Object property name expected.", (IFormatProvider)CultureInfo.InvariantCulture, (object)MiscellaneousUtils.ToString(key)));
         }
         this[index] = value;
     }
 }
Exemplo n.º 12
0
 public override object ReadJson(JsonReader reader, Type objectType, [Nullable(2)] object existingValue, JsonSerializer serializer)
 {
     if (reader.TokenType != JsonToken.Null)
     {
         bool flag = ReflectionUtils.IsNullableType(objectType);
         Type type = flag ? Nullable.GetUnderlyingType(objectType) : objectType;
         try
         {
             if (reader.TokenType == JsonToken.String)
             {
                 object value  = reader.Value;
                 string value2 = (value != null) ? value.ToString() : null;
                 if (StringUtils.IsNullOrEmpty(value2) && flag)
                 {
                     return(null);
                 }
                 return(EnumUtils.ParseEnum(type, this.NamingStrategy, value2, !this.AllowIntegerValues));
             }
             else if (reader.TokenType == JsonToken.Integer)
             {
                 if (!this.AllowIntegerValues)
                 {
                     throw JsonSerializationException.Create(reader, "Integer value {0} is not allowed.".FormatWith(CultureInfo.InvariantCulture, reader.Value));
                 }
                 return(ConvertUtils.ConvertOrCast(reader.Value, CultureInfo.InvariantCulture, type));
             }
         }
         catch (Exception ex)
         {
             throw JsonSerializationException.Create(reader, "Error converting value {0} to type '{1}'.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(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);
 }