示例#1
0
        ///// <summary>
        /////
        ///// </summary>
        ///// <typeparam name="T"></typeparam>
        ///// <param name="name"></param>
        ///// <param name="optional"></param>
        ///// <param name="defValue"></param>
        ///// <returns></returns>
        //public T FormValue<T>(string name, bool optional = false, T defValue = default(T)) {
        //    object val = null;
        //    if (!FormModel.TryGetValue(name, out val)) {
        //        if(!optional)
        //            throw new InvalidOperationException(name + " is required");
        //        return defValue;
        //    }
        //    if (val == null)
        //        return defValue;
        //    return (T)Convert.ChangeType(val, typeof(T));
        //}



        private void ParseDates(Dictionary <string, object> data)
        {
            foreach (var item in data.ToList())
            {
                Dictionary <string, object> childObject = item.Value as Dictionary <string, object>;
                if (childObject != null)
                {
                    ParseDates(childObject);
                    continue;
                }

                string val = item.Value as string;
                if (val == null)
                {
                    continue;
                }

                if (val.StartsWith("/Date(") && val.EndsWith(")/"))
                {
                    // change date..
                    object date = AtomJavaScriptSerializer.ToDateTime(val);
                    data[item.Key] = date;
                }
            }
        }
示例#2
0
        private object DeserializeValue(object value)
        {
            if (value == null)
            {
                return(null);
            }

            string val = value as string;

            if (val != null)
            {
                //if (val.StartsWith("/DateISO"))
                //{
                //    int i = val.IndexOf('(');
                //    val = val.Substring(i + 1);
                //    i = val.LastIndexOf(')');
                //    val = val.Substring(0, i);
                //    return DateTime.Parse(val, null, System.Globalization.DateTimeStyles.RoundtripKind);
                //}
                if (val.StartsWith("/Date"))
                {
                    return(AtomJavaScriptSerializer.ToDateTime(val));
                }
                return(val);
            }

            if (value is Dictionary <string, object> )
            {
                IJavaScriptDeserializable d = new AtomDictionary();
                d.Deserialize(value as Dictionary <string, object>);
                return(d);
            }

            if (value is System.Collections.IEnumerable)
            {
                List <object> items = new List <object>();
                foreach (var item in (System.Collections.IEnumerable)value)
                {
                    items.Add(DeserializeValue(item));
                }

                return(items);
            }

            return(value);
        }
示例#3
0
        private Expression ParseLinq(Expression root, Type type, string key, object value)
        {
            string[] tokens = key.Split(':', ' ').Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => x.Trim()).ToArray();

            string field = tokens[0];

            string operation = "==";

            if (tokens.Length > 1)
            {
                operation = tokens[1];
            }

            operation = operation.ToLower();

            foreach (var item in field.Split('.'))
            {
                PropertyInfo p = null;
                if (item.Equals("$id", StringComparison.OrdinalIgnoreCase))
                {
                    p = type.GetEntityProperties(true).FirstOrDefault().Property;
                }
                else
                {
                    p = type.GetProperty(item);
                }
                type = p.PropertyType;
                root = Expression.Property(root, p);
            }

            switch (operation)
            {
            case "any":
                return(ParseCollectionLinq("Any", root, type, value));

            case "all":
                return(ParseCollectionLinq("All", root, type, value));

            case "in":
                return(ParseCollectionLinq("Contains", Expression.Constant(CreateList(value, type)), type, root));

            case "!in":
                return(Expression.Not(ParseCollectionLinq("Contains", Expression.Constant(CreateList(value, type)), type, root)));

            case "!any":
                return(Expression.Not(ParseCollectionLinq("Any", root, type, value)));

            case "!all":
                return(Expression.Not(ParseCollectionLinq("All", root, type, value)));
            }

            Expression ve = Expression.Constant(null);

            Type vt = Nullable.GetUnderlyingType(type);

            if (value != null)
            {
                Type valueType = value.GetType();
                if (valueType.IsArray || valueType == type)
                {
                    ve = Expression.Constant(value);
                }
                else if (value is ArrayList)
                {
                    ve = Expression.Constant(value);
                }
                else
                {
                    if (vt != null)
                    {
                        if (vt == typeof(DateTime) && value.GetType() == typeof(string))
                        {
                            ve = Expression.Constant(AtomJavaScriptSerializer.ToDateTime(value as string), type);
                        }
                        else
                        {
                            value = Convert.ChangeType(value, vt);
                            ve    = Expression.Convert(Expression.Constant(value), type);
                        }
                    }
                    else
                    {
                        value = Convert.ChangeType(value, type);
                        ve    = Expression.Constant(value);
                    }
                }
            }

            switch (operation)
            {
            case "==":
                return(Expression.Equal(root, ve));

            case "!=":
                return(Expression.NotEqual(root, ve));

            case ">":
                return(Expression.GreaterThan(root, ve));

            case ">=":
                return(Expression.GreaterThanOrEqual(root, ve));

            case "<":
                return(Expression.LessThan(root, ve));

            case "<=":
                return(Expression.LessThanOrEqual(root, ve));

            case "between":
                if (value == null)
                {
                    return(null);
                }
                var objArray = new List <object>(CreateList(value, typeof(object)).Cast <object>());
                if (objArray.Any(x => x == null))
                {
                    return(null);
                }
                if (vt == typeof(DateTime))
                {
                    objArray = objArray.Select(x => (object)AtomJavaScriptSerializer.ToDateTime(x as string)).ToList();
                }
                return(Expression.And(
                           Expression.GreaterThanOrEqual(root, Expression.Constant(objArray.FirstOrDefault(), type)),
                           Expression.LessThanOrEqual(root, Expression.Constant(objArray.LastOrDefault(), type))));

            case "contains":
                return(Expression.Call(root, StringContainsMethod, ve));

            case "startswith":
                return(Expression.Call(root, StringStartsWithMethod, ve));

            case "endswith":
                return(Expression.Call(root, StringEndsWithMethod, ve));

            case "!contains":
                return(Expression.Not(Expression.Call(root, StringContainsMethod, ve)));

            case "!startswith":
                return(Expression.Not(Expression.Call(root, StringStartsWithMethod, ve)));

            case "!endswith":
                return(Expression.Not(Expression.Call(root, StringEndsWithMethod, ve)));

            default:
                break;
            }
            throw new ArgumentException(operation + " not supported");
        }