protected override Task <JToken> OnExecuteAsync()
        {
            // for insert operations version should not be sent so strip it out
            JObject item = MobileServiceSerializer.RemoveSystemProperties(this.Item, out _);

            return(this.Table.InsertAsync(item));
        }
        public async Task RefreshAsync(T instance)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            MobileServiceSerializer serializer = this.MobileServiceClient.Serializer;
            object objId = serializer.GetId(instance, ignoreCase: false, allowDefault: true);

            if (objId == null)
            {
                return; // refresh is not supposed to throw if your object does not have an id for some reason
            }

            string id = EnsureIdIsString(objId);

            // Get the latest version of this element
            JObject refreshed = await base.LookupAsync(id);

            if (refreshed == null)
            {
                throw new InvalidOperationException(
                          string.Format(
                              CultureInfo.InvariantCulture,
                              "Item not found in local store."));
            }

            // Deserialize that value back into the current instance
            serializer.Deserialize <T>(refreshed, instance);
        }
        public Task DeleteAsync(T instance)
        {
            MobileServiceSerializer serializer = this.MobileServiceClient.Serializer;

            var value = serializer.Serialize(instance) as JObject;

            return(base.DeleteAsync(value));
        }
        // we don't support int id tables for offline. therefore id must be of type string
        private static string EnsureIdIsString(JObject instance)
        {
            Arguments.IsNotNull(instance, nameof(instance));

            object id = MobileServiceSerializer.GetId(instance, ignoreCase: false, allowDefault: false);

            return(EnsureIdIsString(id));
        }
Example #5
0
        public Task DeleteAsync(T instance)
        {
            Arguments.IsNotNull(instance, nameof(instance));

            MobileServiceSerializer serializer = this.MobileServiceClient.Serializer;
            var value = serializer.Serialize(instance) as JObject;

            return(base.DeleteAsync(value));
        }
 // we want to keep version as it rides on the object until the sync operation happens using classic table.
 internal static JObject RemoveSystemPropertiesKeepVersion(JObject instance)
 {
     instance = MobileServiceSerializer.RemoveSystemProperties(instance, out string version);
     if (version != null)
     {
         instance[MobileServiceSystemColumns.Version] = version;
     }
     return(instance);
 }
        // we don't support int id tables for offline. therefore id must be of type string
        private static string EnsureIdIsString(JObject instance)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            object id = MobileServiceSerializer.GetId(instance, ignoreCase: false, allowDefault: false);

            return(EnsureIdIsString(id));
        }
        public Task DeleteAsync(T instance)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            MobileServiceSerializer serializer = this.MobileServiceClient.Serializer;
            var value = serializer.Serialize(instance) as JObject;

            return(base.DeleteAsync(value));
        }
        public async Task InsertAsync(T instance)
        {
            MobileServiceSerializer serializer = this.MobileServiceClient.Serializer;

            var value = serializer.Serialize(instance) as JObject;

            // remove system properties since the jtoken insert overload doesn't remove them
            value = RemoveSystemPropertiesKeepVersion(value);

            JObject inserted = await base.InsertAsync(value);

            serializer.Deserialize(inserted, instance);
        }
        public async Task <JObject> InsertAsync(JObject instance)
        {
            object id = MobileServiceSerializer.GetId(instance, ignoreCase: false, allowDefault: true);

            if (id == null)
            {
                id       = Guid.NewGuid().ToString();
                instance = (JObject)instance.DeepClone();
                instance[MobileServiceSystemColumns.Id] = (string)id;
            }
            else
            {
                EnsureIdIsString(id);
            }

            await this.syncContext.InsertAsync(this.TableName, this.Kind, (string)id, instance);

            return(instance);
        }
        public void IndentedFormattingCustomSerialization()
        {
            MobileServiceSerializer serializer = new MobileServiceSerializer();
            serializer.SerializerSettings.Formatting = Formatting.Indented;

            // Need to ensure that the type is registered as a table to force the id property check
            serializer.SerializerSettings.ContractResolver.ResolveTableName(typeof(PocoType));

            PocoType pocoType = new PocoType();

            string actual = serializer.Serialize(pocoType).ToString();
            Assert.AreEqual(actual, "{" + Environment.NewLine + "  \"PublicField\": null," + Environment.NewLine + "  \"PublicProperty\": null" + Environment.NewLine + "}");
        }
        public void IndentedFormattingCustomSerialization()
        {
            MobileServiceSerializer serializer = new MobileServiceSerializer();
            serializer.SerializerSettings.Formatting = Formatting.Indented;

            PocoType pocoType = new PocoType();

            string actual = serializer.Serialize(pocoType);
            Assert.AreEqual(actual, "{\r\n  \"PublicField\": null,\r\n  \"PublicProperty\": null\r\n}");
        }
        public void IgnoreNullValuesCustomSerialization()
        {
            MobileServiceSerializer serializer = new MobileServiceSerializer();
            serializer.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;

            PocoType pocoType = new PocoType();

            string actual = serializer.Serialize(pocoType);
            Assert.AreEqual(actual, "{}");
        }
 public void Initialize()
 {
     this.serializer = new MobileServiceSerializer();
 }
        public void CamelCaseCustomSerialization()
        {
            MobileServiceSerializer serializer = new MobileServiceSerializer();
            serializer.SerializerSettings.CamelCasePropertyNames = true;

            PocoType pocoType = new PocoType();

            string actual = serializer.Serialize(pocoType);
            Assert.AreEqual(actual, "{\"publicField\":null,\"publicProperty\":null}");
        }
        public void IgnoreNullValuesCustomSerialization()
        {
            MobileServiceSerializer serializer = new MobileServiceSerializer();
            serializer.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;

            // Need to ensure that the type is registered as a table to force the id property check
            serializer.SerializerSettings.ContractResolver.ResolveTableName(typeof(PocoType));

            PocoType pocoType = new PocoType();

            string actual = serializer.Serialize(pocoType).ToString();
            Assert.AreEqual(actual, "{}");
        }
 public MobileServiceTableOperationErrorTests()
 {
     this.serializer = new MobileServiceSerializer();
 }
        public void CamelCaseCustomSerialization()
        {
            MobileServiceSerializer serializer = new MobileServiceSerializer();
            serializer.SerializerSettings.CamelCasePropertyNames = true;

            // Need to ensure that the type is registered as a table to force the id property check
            serializer.SerializerSettings.ContractResolver.ResolveTableName(typeof(PocoType));

            PocoType pocoType = new PocoType();

            string actual = serializer.Serialize(pocoType).ToString(Formatting.None);
            Assert.AreEqual(actual, "{\"publicField\":null,\"publicProperty\":null}");
        }
 public void Initialize()
 {
     this.serializer = new MobileServiceSerializer();
 }