Example #1
0
        private static Type GetTypeAndValue(Type expectedType, ODataParameterValue parameter, out object value)
        {
            // The only exceptional case.
            if (expectedType == typeof(ContentOperations.SetPermissionsRequest))
            {
                value = parameter.ToObject <ContentOperations.SetPermissionsRequest>();
                return(typeof(ContentOperations.SetPermissionsRequest));
            }

            switch (parameter.Type)
            {
            case JTokenType.String:
                value = parameter.Value <string>();
                return(typeof(string));

            case JTokenType.Integer:
                if (expectedType == typeof(int?))
                {
                    value = parameter.Value <int?>();
                    return(typeof(int?));
                }
                if (expectedType == typeof(long))
                {
                    value = parameter.Value <long>();
                    return(typeof(long));
                }
                if (expectedType == typeof(long?))
                {
                    value = parameter.Value <long?>();
                    return(typeof(long?));
                }
                if (expectedType == typeof(byte))
                {
                    value = parameter.Value <byte>();
                    return(typeof(byte));
                }
                if (expectedType == typeof(byte?))
                {
                    value = parameter.Value <byte?>();
                    return(typeof(byte?));
                }
                value = parameter.Value <int>();
                return(typeof(int));

            case JTokenType.Boolean:
                if (expectedType == typeof(bool?))
                {
                    value = parameter.Value <bool?>();
                    return(typeof(bool?));
                }
                value = parameter.Value <bool>();
                return(typeof(bool));

            case JTokenType.Float:
                if (expectedType == typeof(float))
                {
                    value = parameter.Value <float>();
                    return(typeof(float));
                }
                if (expectedType == typeof(float?))
                {
                    value = parameter.Value <float?>();
                    return(typeof(float?));
                }
                if (expectedType == typeof(decimal))
                {
                    value = parameter.Value <decimal>();
                    return(typeof(decimal));
                }
                if (expectedType == typeof(decimal?))
                {
                    value = parameter.Value <decimal?>();
                    return(typeof(decimal?));
                }
                if (expectedType == typeof(double?))
                {
                    value = parameter.Value <double?>();
                    return(typeof(double?));
                }
                value = parameter.Value <double>();
                return(typeof(double));

            case JTokenType.Object:
                try
                {
                    value = parameter.ToObject(expectedType, ValueDeserializer);
                    return(expectedType);
                }
                catch (JsonSerializationException)
                {
                    value = null;
                    return(typeof(object));
                }

            case JTokenType.Array:
                value = parameter.ToArray(expectedType, out var realType);
                return(realType);

            case JTokenType.None:
            case JTokenType.Null:
            case JTokenType.Undefined:
                value = expectedType.IsValueType ? Activator.CreateInstance(expectedType) : null;
                return(expectedType);

            case JTokenType.Date:
            case JTokenType.Guid:
            case JTokenType.TimeSpan:
            case JTokenType.Uri:
                value = parameter.Value <string>();
                return(typeof(string));

            //case JTokenType.Constructor:
            //case JTokenType.Property:
            //case JTokenType.Comment:
            //case JTokenType.Raw:
            //case JTokenType.Bytes:
            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Example #2
0
        private static bool TryParseParameter(Type expectedType, ODataParameterValue parameter, bool strict, out object parsed)
        {
            if (expectedType == GetTypeAndValue(expectedType, parameter, out parsed))
            {
                return(true);
            }

            Type nullableBaseType;

            if ((nullableBaseType = Nullable.GetUnderlyingType(expectedType)) != null)
            {
                expectedType = nullableBaseType;
            }

            if (!strict)
            {
                if (parameter.Type == JTokenType.String)
                {
                    var stringValue = parameter.Value <string>();
                    #region int, long, byte, bool, decimal, float, double
                    if (expectedType == typeof(int))
                    {
                        if (int.TryParse(stringValue, out var v))
                        {
                            parsed = v;
                            return(true);
                        }
                    }
                    else if (expectedType == typeof(long))
                    {
                        if (long.TryParse(stringValue, out var v))
                        {
                            parsed = v;
                            return(true);
                        }
                    }
                    else if (expectedType == typeof(byte))
                    {
                        if (byte.TryParse(stringValue, out var v))
                        {
                            parsed = v;
                            return(true);
                        }
                    }
                    else if (expectedType == typeof(bool))
                    {
                        if (bool.TryParse(stringValue, out var v))
                        {
                            parsed = v;
                            return(true);
                        }
                    }
                    else if (expectedType == typeof(decimal))
                    {
                        if (decimal.TryParse(stringValue, out var v))
                        {
                            parsed = v;
                            return(true);
                        }
                        if (decimal.TryParse(stringValue, NumberStyles.Any, CultureInfo.InvariantCulture, out v))
                        {
                            parsed = v;
                            return(true);
                        }
                    }
                    else if (expectedType == typeof(float))
                    {
                        if (float.TryParse(stringValue, out var v))
                        {
                            parsed = v;
                            return(true);
                        }
                        if (float.TryParse(stringValue, NumberStyles.Any, CultureInfo.InvariantCulture, out v))
                        {
                            parsed = v;
                            return(true);
                        }
                    }
                    else if (expectedType == typeof(double))
                    {
                        if (double.TryParse(stringValue, out var v))
                        {
                            parsed = v;
                            return(true);
                        }
                        if (double.TryParse(stringValue, NumberStyles.Any, CultureInfo.InvariantCulture, out v))
                        {
                            parsed = v;
                            return(true);
                        }
                    }
                    else if (expectedType == typeof(DateTime))
                    {
                        if (DateTime.TryParse(stringValue, CultureInfo.CurrentCulture, DateTimeStyles.AssumeUniversal, out var v) ||
                            DateTime.TryParse(stringValue, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out v))
                        {
                            //var date = new DateTime(v.Year, v.Month, v.Day, v.Hour, v.Minute, v.Second, v.Millisecond,
                            //    DateTimeKind.Utc);
                            parsed = v.ToUniversalTime();
                            return(true);
                        }
                    }

                    #endregion
                    #region enum
                    else if (expectedType.IsEnum)
                    {
                        if (int.TryParse(stringValue, out var intValue))
                        {
                            parsed = intValue;
                            return(true);
                        }
                        try
                        {
                            parsed = Enum.Parse(expectedType, stringValue, true);
                            return(true);
                        }
                        catch
                        {
                            // do nothing
                        }
                        return(false);
                    }
                    #endregion
                    #region string[]
                    else if (expectedType == typeof(ODataArray <string>))
                    {
                        try
                        {
                            parsed = new ODataArray <string>(stringValue);
                            return(true);
                        }
                        catch { /* ignored */ }
                    }
                    else if (expectedType == typeof(List <string>))
                    {
                        parsed = new List <string> {
                            stringValue
                        };
                        return(true);
                    }
                    else if (expectedType == typeof(string[]) || expectedType == typeof(IEnumerable <string>))
                    {
                        parsed = new[] { stringValue };
                        return(true);
                    }
                    #endregion
                    #region int[]
                    else if (expectedType == typeof(ODataArray <int>))
                    {
                        try
                        {
                            parsed = new ODataArray <int>(stringValue);
                            return(true);
                        }
                        catch { /* ignored */ }
                    }
                    else if (typeof(IEnumerable <int>).IsAssignableFrom(expectedType))
                    {
                        if (int.TryParse(stringValue, out var v))
                        {
                            if (expectedType == typeof(List <int>))
                            {
                                parsed = new List <int> {
                                    v
                                }
                            }
                            ;
                            else
                            {
                                parsed = new[] { v }
                            };
                            return(true);
                        }
                    }
                    #endregion
                    #region long[]
                    else if (expectedType == typeof(ODataArray <long>))
                    {
                        try
                        {
                            parsed = new ODataArray <long>(stringValue);
                            return(true);
                        }
                        catch { /* ignored */ }
                    }
                    else if (typeof(IEnumerable <long>).IsAssignableFrom(expectedType))
                    {
                        if (long.TryParse(stringValue, out var v))
                        {
                            if (expectedType == typeof(List <long>))
                            {
                                parsed = new List <long> {
                                    v
                                }
                            }
                            ;
                            else
                            {
                                parsed = new[] { v }
                            };
                            return(true);
                        }
                    }
                    #endregion
                    #region byte[]
                    else if (expectedType == typeof(ODataArray <byte>))
                    {
                        try
                        {
                            parsed = new ODataArray <byte>(stringValue);
                            return(true);
                        }
                        catch { /* ignored */ }
                    }
                    else if (typeof(IEnumerable <byte>).IsAssignableFrom(expectedType))
                    {
                        if (byte.TryParse(stringValue, out var v))
                        {
                            if (expectedType == typeof(List <byte>))
                            {
                                parsed = new List <byte> {
                                    v
                                }
                            }
                            ;
                            else
                            {
                                parsed = new[] { v }
                            };
                            return(true);
                        }
                    }
                    #endregion
                    #region bool[]
                    else if (expectedType == typeof(ODataArray <bool>))
                    {
                        try
                        {
                            parsed = new ODataArray <bool>(stringValue);
                            return(true);
                        }
                        catch { /* ignored */ }
                    }
                    else if (typeof(IEnumerable <bool>).IsAssignableFrom(expectedType))
                    {
                        if (bool.TryParse(stringValue, out var v))
                        {
                            if (expectedType == typeof(List <bool>))
                            {
                                parsed = new List <bool> {
                                    v
                                }
                            }
                            ;
                            else
                            {
                                parsed = new[] { v }
                            };
                            return(true);
                        }
                    }
                    #endregion
                    #region decimal[]
                    else if (expectedType == typeof(ODataArray <decimal>))
                    {
                        try
                        {
                            parsed = new ODataArray <decimal>(stringValue);
                            return(true);
                        }
                        catch { /* ignored */ }
                    }
                    else if (typeof(IEnumerable <decimal>).IsAssignableFrom(expectedType))
                    {
                        if (decimal.TryParse(stringValue, out var v) ||
                            decimal.TryParse(stringValue, NumberStyles.Any, CultureInfo.InvariantCulture, out v))
                        {
                            if (expectedType == typeof(List <decimal>))
                            {
                                parsed = new List <decimal> {
                                    v
                                }
                            }
                            ;
                            else
                            {
                                parsed = new[] { v }
                            };
                            return(true);
                        }
                    }
                    #endregion
                    #region float[]
                    else if (expectedType == typeof(ODataArray <float>))
                    {
                        try
                        {
                            parsed = new ODataArray <float>(stringValue);
                            return(true);
                        }
                        catch { /* ignored */ }
                    }
                    else if (typeof(IEnumerable <float>).IsAssignableFrom(expectedType))
                    {
                        if (float.TryParse(stringValue, out var v) ||
                            float.TryParse(stringValue, NumberStyles.Any, CultureInfo.InvariantCulture, out v))
                        {
                            if (expectedType == typeof(List <float>))
                            {
                                parsed = new List <float> {
                                    v
                                }
                            }
                            ;
                            else
                            {
                                parsed = new[] { v }
                            };
                            return(true);
                        }
                    }
                    #endregion
                    #region double[]
                    else if (expectedType == typeof(ODataArray <double>))
                    {
                        try
                        {
                            parsed = new ODataArray <double>(stringValue);
                            return(true);
                        }
                        catch { /* ignored */ }
                    }
                    else if (typeof(IEnumerable <double>).IsAssignableFrom(expectedType))
                    {
                        if (double.TryParse(stringValue, out var v) ||
                            double.TryParse(stringValue, NumberStyles.Any, CultureInfo.InvariantCulture, out v))
                        {
                            if (expectedType == typeof(List <double>))
                            {
                                parsed = new List <double> {
                                    v
                                }
                            }
                            ;
                            else
                            {
                                parsed = new[] { v }
                            };
                            return(true);
                        }
                    }
                    #endregion

                    else if (typeof(ODataArray).IsAssignableFrom(expectedType))
                    {
                        try
                        {
                            parsed = ODataTools.CreateODataArray(expectedType, stringValue);
                            return(true);
                        }
                        catch { /* ignored */ }
                    }

                    //TODO: try parse further opportunities from string to "expectedType"
                }
                else if (parameter.Type == JTokenType.Integer)
                {
                    var intValue = parameter.Value <int>();
                    #region enum
                    if (expectedType.IsEnum)
                    {
                        parsed = intValue;
                        return(true);
                    }
                    #endregion
                }
            }

            parsed = null;
            return(false);
        }
Example #3
0
        private static bool TryParseParameter(Type expectedType, ODataParameterValue parameter, bool strict, out object parsed)
        {
            if (expectedType == GetTypeAndValue(expectedType, parameter, out parsed))
            {
                return(true);
            }

            Type nullableBaseType;

            if ((nullableBaseType = Nullable.GetUnderlyingType(expectedType)) != null)
            {
                expectedType = nullableBaseType;
            }

            if (!strict)
            {
                if (parameter.Type == JTokenType.String)
                {
                    var stringValue = parameter.Value <string>();
                    if (expectedType == typeof(int))
                    {
                        if (int.TryParse(stringValue, out var v))
                        {
                            parsed = v;
                            return(true);
                        }
                    }
                    if (expectedType == typeof(long))
                    {
                        if (long.TryParse(stringValue, out var v))
                        {
                            parsed = v;
                            return(true);
                        }
                    }
                    if (expectedType == typeof(byte))
                    {
                        if (byte.TryParse(stringValue, out var v))
                        {
                            parsed = v;
                            return(true);
                        }
                    }
                    if (expectedType == typeof(bool))
                    {
                        if (bool.TryParse(stringValue, out var v))
                        {
                            parsed = v;
                            return(true);
                        }
                    }
                    if (expectedType == typeof(decimal))
                    {
                        if (decimal.TryParse(stringValue, out var v))
                        {
                            parsed = v;
                            return(true);
                        }
                        if (decimal.TryParse(stringValue, NumberStyles.Any, CultureInfo.InvariantCulture, out v))
                        {
                            parsed = v;
                            return(true);
                        }
                    }
                    if (expectedType == typeof(float))
                    {
                        if (float.TryParse(stringValue, out var v))
                        {
                            parsed = v;
                            return(true);
                        }
                        if (float.TryParse(stringValue, NumberStyles.Any, CultureInfo.InvariantCulture, out v))
                        {
                            parsed = v;
                            return(true);
                        }
                    }
                    if (expectedType == typeof(double))
                    {
                        if (double.TryParse(stringValue, out var v))
                        {
                            parsed = v;
                            return(true);
                        }
                        if (double.TryParse(stringValue, NumberStyles.Any, CultureInfo.InvariantCulture, out v))
                        {
                            parsed = v;
                            return(true);
                        }
                    }
                    //TODO: try parse further opportunities from string to "expectedType"
                }
            }

            parsed = null;
            return(false);
        }
Example #4
0
        private static bool TryParseParameter(Type expectedType, ODataParameterValue parameter, bool strict, out object parsed)
        {
            if (expectedType == GetTypeAndValue(expectedType, parameter, out parsed))
            {
                return(true);
            }

            Type nullableBaseType;

            if ((nullableBaseType = Nullable.GetUnderlyingType(expectedType)) != null)
            {
                expectedType = nullableBaseType;
            }

            if (!strict)
            {
                if (parameter.Type == JTokenType.String)
                {
                    var stringValue = parameter.Value <string>();
                    #region int, long, byte, bool, decimal, float, double
                    if (expectedType == typeof(int))
                    {
                        if (int.TryParse(stringValue, out var v))
                        {
                            parsed = v;
                            return(true);
                        }
                    }
                    else if (expectedType == typeof(long))
                    {
                        if (long.TryParse(stringValue, out var v))
                        {
                            parsed = v;
                            return(true);
                        }
                    }
                    else if (expectedType == typeof(byte))
                    {
                        if (byte.TryParse(stringValue, out var v))
                        {
                            parsed = v;
                            return(true);
                        }
                    }
                    else if (expectedType == typeof(bool))
                    {
                        if (bool.TryParse(stringValue, out var v))
                        {
                            parsed = v;
                            return(true);
                        }
                    }
                    else if (expectedType == typeof(decimal))
                    {
                        if (decimal.TryParse(stringValue, out var v))
                        {
                            parsed = v;
                            return(true);
                        }
                        if (decimal.TryParse(stringValue, NumberStyles.Any, CultureInfo.InvariantCulture, out v))
                        {
                            parsed = v;
                            return(true);
                        }
                    }
                    else if (expectedType == typeof(float))
                    {
                        if (float.TryParse(stringValue, out var v))
                        {
                            parsed = v;
                            return(true);
                        }
                        if (float.TryParse(stringValue, NumberStyles.Any, CultureInfo.InvariantCulture, out v))
                        {
                            parsed = v;
                            return(true);
                        }
                    }
                    else if (expectedType == typeof(double))
                    {
                        if (double.TryParse(stringValue, out var v))
                        {
                            parsed = v;
                            return(true);
                        }
                        if (double.TryParse(stringValue, NumberStyles.Any, CultureInfo.InvariantCulture, out v))
                        {
                            parsed = v;
                            return(true);
                        }
                    }
                    #endregion
                    #region string[]
                    else if (expectedType == typeof(ODataArray <string>))
                    {
                        try
                        {
                            parsed = new ODataArray <string>(stringValue);
                            return(true);
                        }
                        catch { /* ignored */ }
                    }
                    else if (expectedType == typeof(List <string>))
                    {
                        parsed = new List <string> {
                            stringValue
                        };
                        return(true);
                    }
                    else if (expectedType == typeof(string[]) || expectedType == typeof(IEnumerable <string>))
                    {
                        parsed = new[] { stringValue };
                        return(true);
                    }
                    #endregion
                    #region int[]
                    else if (expectedType == typeof(ODataArray <int>))
                    {
                        try
                        {
                            parsed = new ODataArray <int>(stringValue);
                            return(true);
                        }
                        catch { /* ignored */ }
                    }
                    else if (typeof(IEnumerable <int>).IsAssignableFrom(expectedType))
                    {
                        if (int.TryParse(stringValue, out var v))
                        {
                            if (expectedType == typeof(List <int>))
                            {
                                parsed = new List <int> {
                                    v
                                }
                            }
                            ;
                            else
                            {
                                parsed = new[] { v }
                            };
                            return(true);
                        }
                    }
                    #endregion
                    #region long[]
                    else if (expectedType == typeof(ODataArray <long>))
                    {
                        try
                        {
                            parsed = new ODataArray <long>(stringValue);
                            return(true);
                        }
                        catch { /* ignored */ }
                    }
                    else if (typeof(IEnumerable <long>).IsAssignableFrom(expectedType))
                    {
                        if (long.TryParse(stringValue, out var v))
                        {
                            if (expectedType == typeof(List <long>))
                            {
                                parsed = new List <long> {
                                    v
                                }
                            }
                            ;
                            else
                            {
                                parsed = new[] { v }
                            };
                            return(true);
                        }
                    }
                    #endregion
                    #region byte[]
                    else if (expectedType == typeof(ODataArray <byte>))
                    {
                        try
                        {
                            parsed = new ODataArray <byte>(stringValue);
                            return(true);
                        }
                        catch { /* ignored */ }
                    }
                    else if (typeof(IEnumerable <byte>).IsAssignableFrom(expectedType))
                    {
                        if (byte.TryParse(stringValue, out var v))
                        {
                            if (expectedType == typeof(List <byte>))
                            {
                                parsed = new List <byte> {
                                    v
                                }
                            }
                            ;
                            else
                            {
                                parsed = new[] { v }
                            };
                            return(true);
                        }
                    }
                    #endregion
                    #region bool[]
                    else if (expectedType == typeof(ODataArray <bool>))
                    {
                        try
                        {
                            parsed = new ODataArray <bool>(stringValue);
                            return(true);
                        }
                        catch { /* ignored */ }
                    }
                    else if (typeof(IEnumerable <bool>).IsAssignableFrom(expectedType))
                    {
                        if (bool.TryParse(stringValue, out var v))
                        {
                            if (expectedType == typeof(List <bool>))
                            {
                                parsed = new List <bool> {
                                    v
                                }
                            }
                            ;
                            else
                            {
                                parsed = new[] { v }
                            };
                            return(true);
                        }
                    }
                    #endregion
                    #region decimal[]
                    else if (expectedType == typeof(ODataArray <decimal>))
                    {
                        try
                        {
                            parsed = new ODataArray <decimal>(stringValue);
                            return(true);
                        }
                        catch { /* ignored */ }
                    }
                    else if (typeof(IEnumerable <decimal>).IsAssignableFrom(expectedType))
                    {
                        if (decimal.TryParse(stringValue, out var v) ||
                            decimal.TryParse(stringValue, NumberStyles.Any, CultureInfo.InvariantCulture, out v))
                        {
                            if (expectedType == typeof(List <decimal>))
                            {
                                parsed = new List <decimal> {
                                    v
                                }
                            }
                            ;
                            else
                            {
                                parsed = new[] { v }
                            };
                            return(true);
                        }
                    }
                    #endregion
                    #region float[]
                    else if (expectedType == typeof(ODataArray <float>))
                    {
                        try
                        {
                            parsed = new ODataArray <float>(stringValue);
                            return(true);
                        }
                        catch { /* ignored */ }
                    }
                    else if (typeof(IEnumerable <float>).IsAssignableFrom(expectedType))
                    {
                        if (float.TryParse(stringValue, out var v) ||
                            float.TryParse(stringValue, NumberStyles.Any, CultureInfo.InvariantCulture, out v))
                        {
                            if (expectedType == typeof(List <float>))
                            {
                                parsed = new List <float> {
                                    v
                                }
                            }
                            ;
                            else
                            {
                                parsed = new[] { v }
                            };
                            return(true);
                        }
                    }
                    #endregion
                    #region double[]
                    else if (expectedType == typeof(ODataArray <double>))
                    {
                        try
                        {
                            parsed = new ODataArray <double>(stringValue);
                            return(true);
                        }
                        catch { /* ignored */ }
                    }
                    else if (typeof(IEnumerable <double>).IsAssignableFrom(expectedType))
                    {
                        if (double.TryParse(stringValue, out var v) ||
                            double.TryParse(stringValue, NumberStyles.Any, CultureInfo.InvariantCulture, out v))
                        {
                            if (expectedType == typeof(List <double>))
                            {
                                parsed = new List <double> {
                                    v
                                }
                            }
                            ;
                            else
                            {
                                parsed = new[] { v }
                            };
                            return(true);
                        }
                    }
                    #endregion

                    else if (typeof(ODataArray).IsAssignableFrom(expectedType))
                    {
                        try
                        {
                            parsed = ODataTools.CreateODataArray(expectedType, stringValue);
                            return(true);
                        }
                        catch { /* ignored */ }
                    }

                    //TODO: try parse further opportunities from string to "expectedType"
                }
            }

            parsed = null;
            return(false);
        }