示例#1
0
        public static void AddParseBody(this HttpWebRequest request, ParseObject body)
        {
            var dict = new Dictionary <string, object>();

            foreach (var prop in body.GetType().GetProperties())
            {
                var value = prop.GetValue(body, null);

                if (prop.PropertyType == typeof(DateTime))
                {
                    value = new ParseDate((DateTime)value);
                }
                else if (prop.PropertyType == typeof(byte[]))
                {
                    value = new ParseBytes((byte[])value);
                }
                else if (typeof(ParseObject).IsAssignableFrom(prop.PropertyType))
                {
                    if (value != null)
                    {
                        value = new ParsePointer((ParseObject)value);
                    }
                }
                else if (prop.PropertyType.IsGenericType && value is IList && typeof(ParseObject).IsAssignableFrom(prop.PropertyType.GetGenericArguments()[0]))
                {
                    // explicity skip relations, need to be dealt with manually
                    continue;

                    // var pointers = ((IList) value).Cast<ParseObject>().Select(x => new ParsePointer(x)).ToList();
                    // value = pointers.Count == 0 ? null : new {__op = "AddRelation", objects = pointers};
                }

                var attrs = prop.GetCustomAttributes(true);
                JsonIgnoreForSerializationAttribute jsonIgnore = null;
                JsonPropertyAttribute jsonProp = null;
                foreach (var attr in attrs)
                {
                    var tmp1 = attr as JsonPropertyAttribute;
                    if (tmp1 != null)
                    {
                        jsonProp = tmp1;
                    }
                    var tmp2 = attr as JsonIgnoreForSerializationAttribute;
                    if (tmp2 != null)
                    {
                        jsonIgnore = tmp2;
                    }
                }
                if (jsonIgnore != null)
                {
                    continue;
                }

                if (jsonProp != null && !string.IsNullOrEmpty(jsonProp.PropertyName))
                {
                    dict[jsonProp.PropertyName] = value;
                }
                else
                {
                    dict[prop.Name] = value;
                }
            }

            request.AddBody(dict);
        }