private static object CreateProperty(object obj, string[] names, int nameIndex)
        {
            // If next field is index then create an array
            string subField      = names.Length > nameIndex + 1 ? names[nameIndex + 1] : null;
            int?   subFieldIndex = IntegerConverter.ToNullableInteger(subField);

            if (subFieldIndex != null)
            {
                return(new List <object>());
            }

            // Else create a dictionary
            return(new Dictionary <string, object>());
        }
示例#2
0
        public static bool MatchEnum(object expectedType, object value)
        {
            if (value == null)
            {
                return(false);
            }

            if (!(expectedType is Type))
            {
                return(false);
            }

            var type = expectedType as Type;

#if !CORE_NET
            if (type.IsEnum == false)
            {
                return(false);
            }
#endif

            try
            {
                int?intValue = IntegerConverter.ToNullableInteger(value);
                if (intValue != null && Enum.ToObject(type, intValue) != null)
                {
                    return(true);
                }
            }
            catch
            {
                // Ignore...
            }

            try
            {
                string strValue = StringConverter.ToNullableString(value);
                if (strValue != null && Enum.Parse(type, strValue) != null)
                {
                    return(true);
                }
            }
            catch
            {
                // Ignore...
            }

            return(false);
        }
示例#3
0
 /// <summary>
 /// Converts array element into an integer or returns null if conversion is not possible.
 /// </summary>
 /// <param name="index">an index of element to get.</param>
 /// <returns>integer value of element or null if conversion is not supported.</returns>
 /// See <see cref="IntegerConverter.ToNullableInteger(object)"/>
 public int?GetAsNullableInteger(int index)
 {
     return(IntegerConverter.ToNullableInteger(this[index]));
 }
        /// <summary>
        /// Checks if object has a property with specified name.
        /// The object can be a user defined object, map or array.The property name
        /// correspondently must be object property, map key or array index.
        /// </summary>
        /// <param name="obj">an object to introspect.</param>
        /// <param name="name">a name of the property to check.</param>
        /// <returns>true if the object has the property and false if it doesn't.</returns>
        public static bool HasProperty(object obj, string name)
        {
            if (obj == null || name == null)
            {
                return(false);
            }

            var jObject = obj as JObject;

            if (jObject != null)
            {
                var thisObj = jObject;
                foreach (var property in thisObj.Properties())
                {
                    if (name.Equals(property.Name, StringComparison.OrdinalIgnoreCase))
                    {
                        return(true);
                    }
                }
                return(false);
            }

            var jArray = obj as JArray;

            if (jArray != null)
            {
                var list  = jArray;
                var index = IntegerConverter.ToNullableInteger(name);
                return(index >= 0 && index < list.Count);
            }

            var map = obj as IDictionary;

            if (map != null)
            {
                foreach (var key in map.Keys)
                {
                    if (name.Equals(key.ToString(), StringComparison.OrdinalIgnoreCase))
                    {
                        return(true);
                    }
                }
                return(false);
            }

            var genDictionary = obj as IDictionary <string, object>;

            if (genDictionary != null)
            {
                foreach (var key in genDictionary.Keys)
                {
                    if (name.Equals(key, StringComparison.OrdinalIgnoreCase))
                    {
                        return(true);
                    }
                }
                return(false);
            }

            var enumerable = obj as IEnumerable;

            if (enumerable != null)
            {
                var index       = IntegerConverter.ToNullableInteger(name);
                var genericEnum = enumerable.Cast <object>();
                return(index >= 0 && index < genericEnum.Count());
            }

            return(PropertyReflector.HasProperty(obj, name));
        }
        /// <summary>
        /// Gets value of object property specified by its name.
        /// The object can be a user defined object, map or array.The property name
        /// correspondently must be object property, map key or array index.
        /// </summary>
        /// <param name="obj">an object to read property from.</param>
        /// <param name="name">a name of the property to get.</param>
        /// <returns>the property value or null if property doesn't exist or introspection failed.</returns>
        public static object GetProperty(object obj, string name)
        {
            if (obj == null || name == null)
            {
                return(false);
            }

            name = name.ToLower();

            var jObject = obj as JObject;

            if (jObject != null)
            {
                var thisObj = jObject;
                foreach (var property in thisObj.Properties())
                {
                    if (name.Equals(property.Name, StringComparison.OrdinalIgnoreCase))
                    {
                        return(property.Value);
                    }
                }
                return(null);
            }

            var jArray = obj as JArray;

            if (jArray != null)
            {
                var list  = jArray;
                var index = IntegerConverter.ToNullableInteger(name);
                return(index >= 0 && index < list.Count ? list[index.Value] : null);
            }

            var map = obj as IDictionary;

            if (map != null)
            {
                foreach (var key in map.Keys)
                {
                    if (name.Equals(key.ToString(), StringComparison.OrdinalIgnoreCase))
                    {
                        return(map[key]);
                    }
                }
            }

            var genDictionary = obj as IDictionary <string, object>;

            if (genDictionary != null)
            {
                foreach (var key in genDictionary.Keys)
                {
                    if (name.Equals(key, StringComparison.OrdinalIgnoreCase))
                    {
                        return(genDictionary[key]);
                    }
                }
            }

            var enumerable = obj as IEnumerable;

            if (enumerable != null)
            {
                var index = IntegerConverter.ToNullableInteger(name);
                if (index >= 0)
                {
                    var list = (IEnumerable)obj;
                    foreach (var value in list)
                    {
                        if (index == 0)
                        {
                            return(value);
                        }
                        index--;
                    }
                }
                return(null);
            }

            return(PropertyReflector.GetProperty(obj, name));
        }
示例#6
0
        /// <summary>
        /// Converts map element into an integer or returns null if conversion is not possible.
        /// </summary>
        /// <param name="key">a key of element to get.</param>
        /// <returns>integer value of the element or null if conversion is not supported.</returns>
        /// See <see cref="IntegerConverter.ToNullableInteger(object)"/>
        public int?GetAsNullableInteger(string key)
        {
            var value = Get(key);

            return(IntegerConverter.ToNullableInteger(value));
        }
示例#7
0
 public int?GetAsNullableInteger()
 {
     return(IntegerConverter.ToNullableInteger(Value));
 }
 public PagingParams(object skip, object take, object total = null)
 {
     Skip  = IntegerConverter.ToNullableInteger(skip);
     Take  = IntegerConverter.ToNullableInteger(take);
     Total = BooleanConverter.ToBooleanWithDefault(total, false);
 }