Ejemplo n.º 1
0
        public void BasicDeserialization()
        {
            // Create a new instnace
            Person p = MobileServiceTableSerializer.Deserialize <Person>(
                new JsonObject()
                .Set("Name", "Bob")
                .Set("Age", 20)
                .Set("id", 10));

            Assert.AreEqual(10L, p.Id);
            Assert.AreEqual("Bob", p.Name);
            Assert.AreEqual(20, p.Age);

            // Deserialize into the same instance
            MobileServiceTableSerializer.Deserialize(
                new JsonObject()
                .Set("Age", 21)
                .Set("Name", "Roberto"),
                p);
            Assert.AreEqual(10L, p.Id);
            Assert.AreEqual("Roberto", p.Name);
            Assert.AreEqual(21, p.Age);

            Assert.Throws <ArgumentNullException>(() => MobileServiceTableSerializer.Deserialize(null, new object()));
            Assert.Throws <ArgumentNullException>(() => MobileServiceTableSerializer.Deserialize(JsonExtensions.Null(), null));
        }
Ejemplo n.º 2
0
        public void CustomSerialization()
        {
            SimpleTree tree = new SimpleTree
            {
                Id       = 1,
                Name     = "John",
                Children = new List <SimpleTree>
                {
                    new SimpleTree {
                        Id = 2, Name = "James"
                    },
                    new SimpleTree
                    {
                        Id       = 3,
                        Name     = "David",
                        Children = new List <SimpleTree>
                        {
                            new SimpleTree {
                                Id = 4, Name = "Jennifer"
                            }
                        }
                    }
                }
            };

            IJsonValue family = MobileServiceTableSerializer.Serialize(tree);

            Assert.AreEqual("Jennifer",
                            family.Get("children").AsArray()[1].Get("children").AsArray()[0].Get("name").AsString());

            SimpleTree second = MobileServiceTableSerializer.Deserialize <SimpleTree>(family);

            Assert.AreEqual(tree.Children[0].Name, second.Children[0].Name);
        }
Ejemplo n.º 3
0
        public void DataContractSerialization()
        {
            // Serialize a type with a data contract
            Animal bear = new Animal {
                Id = 1, Species = "Grizzly", Deadly = true, SoftAndFurry = true
            };
            IJsonValue value = MobileServiceTableSerializer.Serialize(bear);

            Assert.AreEqual(1, value.Get("id").AsInteger());
            Assert.AreEqual("Grizzly", value.Get("species").AsString());
            Assert.IsTrue(value.Get("SoftAndFurry").AsBool().Value);
            Assert.IsTrue(value.Get("Deadly").IsNull());

            // Deserialize a type with a data contract
            Animal donkey = MobileServiceTableSerializer.Deserialize <Animal>(
                new JsonObject()
                .Set("id", 2)
                .Set("species", "Stubbornus Maximums")
                .Set("Deadly", true)
                .Set("SoftAndFurry", false));

            Assert.AreEqual(2, donkey.Id);
            Assert.AreEqual("Stubbornus Maximums", donkey.Species);
            Assert.IsFalse(donkey.SoftAndFurry);
            Assert.IsFalse(donkey.Deadly); // No DataMember so not serialized

            // Ensure we throw if we're missing a required
            Assert.Throws <SerializationException>(() => MobileServiceTableSerializer.Deserialize <Animal>(
                                                       new JsonObject().Set("id", 3).Set("name", "Pterodactyl").Set("Deadly", true)));
        }
Ejemplo n.º 4
0
        public void BasicSerialization()
        {
            // Serialize an instance without an ID
            IJsonValue value = MobileServiceTableSerializer.Serialize(
                new Person {
                Name = "John", Age = 45
            });

            Assert.AreEqual("John", value.Get("Name").AsString());
            Assert.AreEqual(45, value.Get("Age").AsInteger());
            Assert.IsNull(value.Get("id").AsInteger());

            // Ensure the ID is written when provided
            // Serialize an instance without an ID
            value = MobileServiceTableSerializer.Serialize(
                new Person {
                Id = 2, Name = "John", Age = 45
            });
            Assert.AreEqual("John", value.Get("Name").AsString());
            Assert.AreEqual(45, value.Get("Age").AsInteger());
            Assert.AreEqual(2, value.Get("id").AsInteger());

            // Ensure other properties are included but null
            value = MobileServiceTableSerializer.Serialize(new Person {
                Id = 12
            });
            string text = value.Stringify();

            Assert.That(text, Contains.Substring("Name"));
            Assert.That(text, Contains.Substring("Age"));

            Assert.Throws <ArgumentNullException>(() => MobileServiceTableSerializer.Serialize(null));
        }
Ejemplo n.º 5
0
        public IJsonValue ConvertToJson(object instance)
        {
            Movie[]   movies = (Movie[])instance;
            JsonArray result = new JsonArray();

            foreach (var movie in movies)
            {
                result.Add(MobileServiceTableSerializer.Serialize(movie));
            }

            return(result);
        }
Ejemplo n.º 6
0
        IJsonValue ICustomMobileServiceTableSerialization.Serialize()
        {
            JsonArray children = new JsonArray();

            foreach (SimpleTree child in Children)
            {
                children.Append(MobileServiceTableSerializer.Serialize(child));
            }

            return(new JsonObject()
                   .Set("id", Id)
                   .Set("name", Name)
                   .Set("children", children));
        }
Ejemplo n.º 7
0
        public void Deserialize(JToken value)
        {
            // Get the ID and Age properties
            MobileServiceTableSerializer.Deserialize(value, this, true);

            if (Title != null)
            {
                string[] parts = Title.Split(':');
                if (parts.Length == 2)
                {
                    Tag   = parts[0];
                    Title = parts[1].Trim();
                }
            }
        }
Ejemplo n.º 8
0
        public void DuplicateKeyType()
        {
            var instance = new DuplicateKeyType();

            Exception expectedException = null;

            try
            {
                MobileServiceTableSerializer.Serialize(instance);
            }
            catch (InvalidOperationException exception)
            {
                expectedException = exception;
            }

            Assert.IsNotNull(expectedException);

            Assert.AreEqual(expectedException.Message, "Two or more members of type 'DuplicateKeyType' are mapped to the same name 'ColorId'. Verify that your DataMember annotations are correct.");
        }
    public async static Task <List <T> > In <T>(this IMobileServiceTable <T> table, List <int> ids)
    {
        var query = new StringBuilder("$filter=(");

        for (int i = 0; i < ids.Count; i++)
        {
            query.AppendFormat("id eq {0}", ids[i]);     //don't forget to url escape and 'quote' strings
            if (i < ids.Count - 1)
            {
                query.Append(" or ");
            }
        }
        query.Append(")");
        var list = await table.ReadAsync(query.ToString());

        var items = list.Select(i => MobileServiceTableSerializer.Deserialize <T>(i)).ToList();

        return(items);
    }
Ejemplo n.º 10
0
        public void DataMemberJsonConverter()
        {
            // Serialize with a custom JSON converter
            IJsonValue link = MobileServiceTableSerializer.Serialize(
                new Hyperlink
            {
                Href = new Uri("http://www.microsoft.com/"),
                Alt  = "Microsoft"
            });

            Assert.AreEqual("Microsoft", link.Get("Alt").AsString());
            Assert.AreEqual("http://www.microsoft.com/", link.Get("Href").AsString());

            // Deserialize with a custom JSON converter
            Hyperlink azure = MobileServiceTableSerializer.Deserialize <Hyperlink>(
                new JsonObject().Set("Alt", "Windows Azure").Set("Href", "http://windowsazure.com"));

            Assert.AreEqual("Windows Azure", azure.Alt);
            Assert.AreEqual("windowsazure.com", azure.Href.Host);
        }
Ejemplo n.º 11
0
        public void ExpectSerializationFailure <T>(Action <DataTypes, T> setter, params T[] values)
        {
            foreach (T value in values)
            {
                DataTypes instance = new DataTypes();
                setter(instance, value);
                try
                {
                    IJsonValue data = MobileServiceTableSerializer.Serialize(instance);
                    MobileServiceTableSerializer.Deserialize(data, instance);
                }
                catch (Exception)
                {
                    continue;
                }

                Failures.Add(new Exception(
                                 string.Format(
                                     CultureInfo.InvariantCulture,
                                     "No expected serialiation failure for '{0}' value '{1}'",
                                     typeof(T).Name, value)));
            }
        }
Ejemplo n.º 12
0
        private async Task <MobileServiceCollectionView <Speaker> > InsertBatch(IEnumerable <Speaker> speakers, string tableName = "speakers", string idProperty = "Id")
        {
            // convert to an array in JSON
            var arr = new JsonArray();

            foreach (var speaker in speakers)
            {
                arr.Add(MobileServiceTableSerializer.Serialize(speaker));
            }

            // Now create a JSON body
            var body = new JsonObject()
            {
                { tableName, arr }
            };

            // insert!
            IJsonValue response = await Table.InsertAsync(body);

            await Task.Delay(TimeSpan.FromSeconds(5));

            return(GetSpeakerView());
        }