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); }
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))); }
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)); }
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(); } } }
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); }
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); }
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))); } }