public object Apply(object oldValue, string key)
        {
            if (AdditionQueue.Count == 0 && RemovalQueue.Count == 0)
            {
                return(null);
            }

            switch (oldValue)
            {
            case null:
                return(ParseRelationBase.CreateRelation(null, key, TargetClassName));

            case ParseRelationBase oldRelation:
            {
                if (oldRelation.TargetClassName is string oldClassName && oldClassName != TargetClassName)
                {
                    throw new InvalidOperationException($"Related object must be a {oldClassName}, but a {TargetClassName} was passed in.");
                }

                oldRelation.TargetClassName = TargetClassName;
                return(oldRelation);
            }

            default:
                throw new InvalidOperationException("Operation is invalid after previous operation.");
            }
        }
        public object Apply(object oldValue, string key)
        {
            if (_adds.Count == 0 && _removes.Count == 0)
            {
                return(null);
            }

            if (oldValue == null)
            {
                return(ParseRelationBase.CreateRelation(null, key, TargetClassName));
            }

            if (oldValue is ParseRelationBase)
            {
                var oldRelation  = (ParseRelationBase)oldValue;
                var oldClassName = oldRelation.TargetClassName;
                if (oldClassName != null && oldClassName != TargetClassName)
                {
                    throw new InvalidOperationException("Related object must be a " + oldClassName
                                                        + ", but a " + TargetClassName +
                                                        " was passed in.");
                }

                oldRelation.TargetClassName = TargetClassName;
                return(oldRelation);
            }

            throw new InvalidOperationException("Operation is invalid after previous operation.");
        }
        public object Decode(object data)
        {
            if (data == null)
            {
                return(null);
            }

            var dict = data as IDictionary <string, object>;

            if (dict != null)
            {
                if (dict.ContainsKey("__op"))
                {
                    return(ParseFieldOperations.Decode(dict));
                }

                object type;
                dict.TryGetValue("__type", out type);
                var typeString = type as string;

                if (typeString == null)
                {
                    var newDict = new Dictionary <string, object>();
                    foreach (var pair in dict)
                    {
                        newDict[pair.Key] = Decode(pair.Value);
                    }
                    return(newDict);
                }

                if (typeString == "Date")
                {
                    return(ParseDate(dict["iso"] as string));
                }

                if (typeString == "Bytes")
                {
                    return(Convert.FromBase64String(dict["base64"] as string));
                }

                if (typeString == "Pointer")
                {
                    return(DecodePointer(dict["className"] as string, dict["objectId"] as string));
                }

                if (typeString == "File")
                {
                    return(new ParseFile(dict["name"] as string, new Uri(dict["url"] as string)));
                }

                if (typeString == "GeoPoint")
                {
                    return(new ParseGeoPoint(Conversion.To <double>(dict["latitude"]),
                                             Conversion.To <double>(dict["longitude"])));
                }

                if (typeString == "Object")
                {
                    var state = ParseObjectCoder.Instance.Decode(dict, this);
                    return(ParseObject.FromState <ParseObject>(state, dict["className"] as string));
                }

                if (typeString == "Relation")
                {
                    return(ParseRelationBase.CreateRelation(null, null, dict["className"] as string));
                }

                var converted = new Dictionary <string, object>();
                foreach (var pair in dict)
                {
                    converted[pair.Key] = Decode(pair.Value);
                }
                return(converted);
            }

            var list = data as IList <object>;

            if (list != null)
            {
                return((from item in list
                        select Decode(item)).ToList());
            }

            return(data);
        }
Example #4
0
        public object Decode(object data)
        {
            if (data is null)
            {
                return(null);
            }

            if (data is IDictionary <string, object> dict)
            {
                if (dict.ContainsKey("__op"))
                {
                    return(ParseFieldOperations.Decode(dict));
                }

                dict.TryGetValue("__type", out object type);

                switch (type)
                {
                case "Date":
                    return(ParseDate(dict["iso"] as string));

                case "Bytes":
                    return(Convert.FromBase64String(dict["base64"] as string));

                case "Pointer":
                    return(DecodePointer(dict["className"] as string, dict["objectId"] as string));

                case "File":
                    return(new ParseFile(dict["name"] as string, new Uri(dict["url"] as string)));

                case "GeoPoint":
                    return(new ParseGeoPoint(ConversionHelpers.DowncastValue <double>(dict["latitude"]),
                                             ConversionHelpers.DowncastValue <double>(dict["longitude"])));

                case "Object":
                    IObjectState state = ParseObjectCoder.Instance.Decode(dict, this);
                    return(ParseObject.FromState <ParseObject>(state, dict["className"] as string));

                case "Relation":
                    return(ParseRelationBase.CreateRelation(null, null, dict["className"] as string));

                case object anything when !(anything is string):
                    Dictionary <string, object> newDict = new Dictionary <string, object> {
                    };
                    foreach (KeyValuePair <string, object> pair in dict)
                    {
                        newDict[pair.Key] = Decode(pair.Value);
                    }
                    return(newDict);
                }

                Dictionary <string, object> converted = new Dictionary <string, object>();
                foreach (KeyValuePair <string, object> pair in dict)
                {
                    converted[pair.Key] = Decode(pair.Value);
                }

                return(converted);
            }

            return(data is IList <object> list ? (from item in list select Decode(item)).ToList() : data);
        }