Esempio n. 1
0
        public new object Encode(object obj)
        {
            if (obj is AVObject av)
            {
                return(EncodeParseObject(av, false));
            }
            var list = Conversion.As <IList <object> >(obj);

            if (list != null)
            {
                var newArray = new List <object>();
                // We need to explicitly cast `list` to `List<object>` rather than
                // `IList<object>` because IL2CPP is stricter than the usual Unity AOT compiler pipeline.
                if (list.GetType().IsArray)
                {
                    list = new List <object>(list);
                }
                foreach (var item in list)
                {
                    if (!IsValidType(item))
                    {
                        throw new ArgumentException("Invalid type for value in an array");
                    }
                    newArray.Add(Encode(item));
                }
                return(newArray);
            }
            if (AVEncoder.IsValidType(obj))
            {
                return(base.Encode(obj));
            }
            return(obj);
        }
        public object Encode(object value)
        {
            // If this object has a special encoding, encode it and return the
            // encoded object. Otherwise, just return the original object.
            switch (value)
            {
            case DateTime _:
                return(new Dictionary <string, object>
                {
                    {
                        "iso",
                        ((DateTime)value).ToString(ParseClient.DateFormatStrings.First(), CultureInfo.InvariantCulture)
                    },
                    { "__type", "Date" }
                });

            case byte[] bytes:
                return(new Dictionary <string, object>
                {
                    { "__type", "Bytes" },
                    { "base64", Convert.ToBase64String(bytes) }
                });

            case ParseObject obj:
                return(EncodeParseObject(obj));

            case IJsonConvertible jsonConvertible:
                return(jsonConvertible.ToJson());
            }

            var dict = Conversion.As <IDictionary <string, object> >(value);

            if (dict != null)
            {
                var json = new Dictionary <string, object>();
                foreach (var pair in dict)
                {
                    json[pair.Key] = Encode(pair.Value);
                }

                return(json);
            }

            var list = Conversion.As <IList <object> >(value);

            if (list != null)
            {
                return(EncodeList(list));
            }

            // TODO (hallucinogen): convert IParseFieldOperation to IJsonConvertible
            if (value is IParseFieldOperation operation)
            {
                return(operation.Encode());
            }

            return(value);
        }
Esempio n. 3
0
        public object Apply(object oldValue, string key)
        {
            if (oldValue == null)
            {
                return(new List <object>());
            }
            var oldList = Conversion.As <IList <object> >(oldValue);

            return(oldList.Except(objects, ParseFieldOperations.ParseObjectComparer).ToList());
        }
 public static bool IsValidType(object value)
 {
     return(value == null ||
            ReflectionHelpers.IsPrimitive(value.GetType()) ||
            value is string ||
            value is ParseObject ||
            value is ParseACL ||
            value is ParseFile ||
            value is ParseGeoPoint ||
            value is ParseRelationBase ||
            value is DateTime ||
            value is byte[] ||
            Conversion.As <IDictionary <string, object> >(value) != null ||
            Conversion.As <IList <object> >(value) != null);
 }
Esempio n. 5
0
        public IParseFieldOperation MergeWithPrevious(IParseFieldOperation previous)
        {
            switch (previous)
            {
            case null:
                return(this);

            case ParseDeleteOperation _:
                return(previous);

            case ParseSetOperation _:
                var setOp   = (ParseSetOperation)previous;
                var oldList = Conversion.As <IList <object> >(setOp.Value);
                return(new ParseSetOperation(Apply(oldList, null)));

            case ParseRemoveOperation _:
                var oldOp = (ParseRemoveOperation)previous;
                return(new ParseRemoveOperation(oldOp.Objects.Concat(_objects)));
            }

            throw new InvalidOperationException("Operation is invalid after previous operation.");
        }
Esempio n. 6
0
 public IAVFieldOperation MergeWithPrevious(IAVFieldOperation previous)
 {
     if (previous == null)
     {
         return(this);
     }
     if (previous is AVDeleteOperation)
     {
         return(previous);
     }
     if (previous is AVSetOperation)
     {
         var setOp   = (AVSetOperation)previous;
         var oldList = Conversion.As <IList <object> >(setOp.Value);
         return(new AVSetOperation(this.Apply(oldList, null)));
     }
     if (previous is AVRemoveOperation)
     {
         var oldOp = (AVRemoveOperation)previous;
         return(new AVRemoveOperation(oldOp.Objects.Concat(objects)));
     }
     throw new InvalidOperationException("Operation is invalid after previous operation.");
 }
Esempio n. 7
0
        private IList <Task <IDictionary <string, object> > > ExecuteBatchRequest(IList <ParseCommand> requests,
                                                                                  string sessionToken,
                                                                                  CancellationToken cancellationToken)
        {
            var tasks     = new List <Task <IDictionary <string, object> > >();
            int batchSize = requests.Count;
            var tcss      = new List <TaskCompletionSource <IDictionary <string, object> > >();

            for (int i = 0; i < batchSize; ++i)
            {
                var tcs = new TaskCompletionSource <IDictionary <string, object> >();
                tcss.Add(tcs);
                tasks.Add(tcs.Task);
            }

            var encodedRequests = requests.Select(r => {
                var results = new Dictionary <string, object> {
                    { "method", r.Method },
                    { "path", r.Uri.AbsolutePath },
                };

                if (r.DataObject != null)
                {
                    results["body"] = r.DataObject;
                }
                return(results);
            }).Cast <object>().ToList();
            var command = new ParseCommand("batch",
                                           method: "POST",
                                           sessionToken: sessionToken,
                                           data: new Dictionary <string, object> {
                { "requests", encodedRequests }
            });

            commandRunner.RunCommandAsync(command, cancellationToken: cancellationToken).ContinueWith(t => {
                if (t.IsFaulted || t.IsCanceled)
                {
                    foreach (var tcs in tcss)
                    {
                        if (t.IsFaulted)
                        {
                            tcs.TrySetException(t.Exception);
                        }
                        else if (t.IsCanceled)
                        {
                            tcs.TrySetCanceled();
                        }
                    }
                    return;
                }

                var resultsArray = Conversion.As <IList <object> >(t.Result.Item2["results"]);
                int resultLength = resultsArray.Count;
                if (resultLength != batchSize)
                {
                    foreach (var tcs in tcss)
                    {
                        tcs.TrySetException(new InvalidOperationException(
                                                "Batch command result count expected: " + batchSize + " but was: " + resultLength + "."));
                    }
                    return;
                }

                for (int i = 0; i < batchSize; ++i)
                {
                    var result = resultsArray[i] as Dictionary <string, object>;
                    var tcs    = tcss[i];

                    if (result.ContainsKey("success"))
                    {
                        tcs.TrySetResult(result["success"] as IDictionary <string, object>);
                    }
                    else if (result.ContainsKey("error"))
                    {
                        var error      = result["error"] as IDictionary <string, object>;
                        long errorCode = (long)error["code"];
                        tcs.TrySetException(new ParseException((ParseException.ErrorCode)errorCode, error["error"] as string));
                    }
                    else
                    {
                        tcs.TrySetException(new InvalidOperationException(
                                                "Invalid batch command response."));
                    }
                }
            });

            return(tasks);
        }
Esempio n. 8
0
        public object Encode(object value)
        {
            // If this object has a special encoding, encode it and return the
            // encoded object. Otherwise, just return the original object.
            if (value is DateTime)
            {
                return(new Dictionary <string, object> {
                    { "iso", ((DateTime)value).ToString(ParseClient.DateFormatStrings.First()) },
                    { "__type", "Date" }
                });
            }

            var bytes = value as byte[];

            if (bytes != null)
            {
                return(new Dictionary <string, object> {
                    { "__type", "Bytes" },
                    { "base64", Convert.ToBase64String(bytes) }
                });
            }

            var obj = value as ParseObject;

            if (obj != null)
            {
                return(EncodeParseObject(obj));
            }

            var jsonConvertible = value as IJsonConvertible;

            if (jsonConvertible != null)
            {
                return(jsonConvertible.ToJSON());
            }

            var dict = Conversion.As <IDictionary <string, object> >(value);

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

            var list = Conversion.As <IList <object> >(value);

            if (list != null)
            {
                return(EncodeList(list));
            }

            // TODO (hallucinogen): convert IParseFieldOperation to IJsonConvertible
            var operation = value as IParseFieldOperation;

            if (operation != null)
            {
                return(operation.Encode());
            }

            return(value);
        }
Esempio n. 9
0
 public static bool Validate(object value) => value is null || value.GetType().IsPrimitive || value is string || value is ParseObject || value is ParseACL || value is ParseFile || value is ParseGeoPoint || value is ParseRelationBase || value is DateTime || value is byte[] || Conversion.As <IDictionary <string, object> >(value) is
 {