/// <summary> /// Tests for <see cref="JsonValue"/> instances with dynamic references. /// </summary> public void DynamicTest() { int seed = GetRandomSeed(); Log.Info("Seed: {0}", seed); Random rndGen = new Random(seed); AssertIsTrue(Assembly.GetExecutingAssembly().IsFullyTrusted == false, "Executing assembly not expected to be fully trusted!"); Person person = new Person(rndGen); person.AddFriends(1, rndGen); dynamic jo = JsonValueExtensions.CreateFrom(person); AssertAreEqual(person.Friends[0].Name, jo.Friends[0].Name.ReadAs<string>(), "Friends[0].Name"); AssertAreEqual(person.Address.City, jo.Address.City.ReadAs<string>(), "Address.City"); AssertAreEqual(person.Friends[0].Age, (int)jo.Friends[0].Age, "Friends[0].Age"); string newCityName = "Bellevue"; jo.Address.City = newCityName; AssertAreEqual(newCityName, (string)jo.Address.City, "Address.City2"); AssertAreEqual(person.Friends[0].Address.City, jo.ValueOrDefault("Friends").ValueOrDefault(0).ValueOrDefault("Address").ValueOrDefault("City").ReadAs<string>(), "Friends[0].Address.City"); AssertAreEqual(person.Friends[0].Age, (int)jo.ValueOrDefault("Friends").ValueOrDefault(0).ValueOrDefault("Age"), "Friends[0].Age2"); AssertAreEqual(person.Friends[0].Address.City, jo.ValueOrDefault("Friends", 0, "Address", "City").ReadAs<string>(), "Friends[0].Address.City"); AssertAreEqual(person.Friends[0].Age, (int)jo.ValueOrDefault("Friends", 0, "Age"), "Friends[0].Age2"); int newAge = 42; JsonValue ageValue = jo.Friends[0].Age = newAge; AssertAreEqual(newAge, (int)ageValue, "Friends[0].Age3"); AssertIsTrue(jo.NonExistentProperty is JsonValue, "Expected a JsonValue"); AssertIsTrue(jo.NonExistentProperty.JsonType == JsonType.Default, "Expected default JsonValue"); }
/// <summary> /// Tests for <see cref="JsonValue"/> instances without dynamic references. /// </summary> public void NonDynamicTest() { int seed = GetRandomSeed(); Log.Info("Seed: {0}", seed); Random rndGen = new Random(seed); AssertIsTrue(Assembly.GetExecutingAssembly().IsFullyTrusted == false, "Executing assembly not expected to be fully trusted!"); Person person = new Person(rndGen); Person person2 = new Person(rndGen); person.AddFriends(3, rndGen); person2.AddFriends(3, rndGen); JsonValue jo = JsonValueExtensions.CreateFrom(person); JsonValue jo2 = JsonValueExtensions.CreateFrom(person2); AssertAreEqual(person.Address.City, jo["Address"]["City"].ReadAs <string>(), "Address.City"); AssertAreEqual(person.Friends[1].Age, jo["Friends"][1]["Age"].ReadAs <int>(), "Friends[1].Age"); string newCityName = "Bellevue"; jo["Address"]["City"] = newCityName; AssertAreEqual(newCityName, (string)jo["Address"]["City"], "Address.City2"); jo["Friends"][1] = jo2; AssertAreEqual(person2.Age, (int)jo["Friends"][1]["Age"], "Friends[1].Age2"); AssertAreEqual(person2.Address.City, jo.ValueOrDefault("Friends").ValueOrDefault(1).ValueOrDefault("Address").ValueOrDefault("City").ReadAs <string>(), "Address.City3"); AssertAreEqual(person2.Age, (int)jo.ValueOrDefault("Friends").ValueOrDefault(1).ValueOrDefault("Age"), "Friends[1].Age3"); AssertAreEqual(person2.Address.City, jo.ValueOrDefault("Friends", 1, "Address", "City").ReadAs <string>(), "Address.City3"); AssertAreEqual(person2.Age, (int)jo.ValueOrDefault("Friends", 1, "Age"), "Friends[1].Age3"); int newAge = 42; JsonValue ageValue = jo["Friends"][1]["Age"] = newAge; AssertAreEqual(newAge, (int)ageValue, "Friends[1].Age4"); }
/// <summary> /// Tests for <see cref="JsonValue"/> instances with dynamic references. /// </summary> public void DynamicTest() { int seed = GetRandomSeed(); Log.Info("Seed: {0}", seed); Random rndGen = new Random(seed); AssertIsTrue(Assembly.GetExecutingAssembly().IsFullyTrusted == false, "Executing assembly not expected to be fully trusted!"); Person person = new Person(rndGen); person.AddFriends(1, rndGen); dynamic jo = JsonValueExtensions.CreateFrom(person); AssertAreEqual(person.Friends[0].Name, jo.Friends[0].Name.ReadAs <string>(), "Friends[0].Name"); AssertAreEqual(person.Address.City, jo.Address.City.ReadAs <string>(), "Address.City"); AssertAreEqual(person.Friends[0].Age, (int)jo.Friends[0].Age, "Friends[0].Age"); string newCityName = "Bellevue"; jo.Address.City = newCityName; AssertAreEqual(newCityName, (string)jo.Address.City, "Address.City2"); AssertAreEqual(person.Friends[0].Address.City, jo.ValueOrDefault("Friends").ValueOrDefault(0).ValueOrDefault("Address").ValueOrDefault("City").ReadAs <string>(), "Friends[0].Address.City"); AssertAreEqual(person.Friends[0].Age, (int)jo.ValueOrDefault("Friends").ValueOrDefault(0).ValueOrDefault("Age"), "Friends[0].Age2"); AssertAreEqual(person.Friends[0].Address.City, jo.ValueOrDefault("Friends", 0, "Address", "City").ReadAs <string>(), "Friends[0].Address.City"); AssertAreEqual(person.Friends[0].Age, (int)jo.ValueOrDefault("Friends", 0, "Age"), "Friends[0].Age2"); int newAge = 42; JsonValue ageValue = jo.Friends[0].Age = newAge; AssertAreEqual(newAge, (int)ageValue, "Friends[0].Age3"); AssertIsTrue(jo.NonExistentProperty is JsonValue, "Expected a JsonValue"); AssertIsTrue(jo.NonExistentProperty.JsonType == JsonType.Default, "Expected default JsonValue"); }
/// <summary> /// Tests for <see cref="JsonValue"/> instances without dynamic references. /// </summary> public void NonDynamicTest() { int seed = GetRandomSeed(); Log.Info("Seed: {0}", seed); Random rndGen = new Random(seed); AssertIsTrue(Assembly.GetExecutingAssembly().IsFullyTrusted == false, "Executing assembly not expected to be fully trusted!"); Person person = new Person(rndGen); Person person2 = new Person(rndGen); person.AddFriends(3, rndGen); person2.AddFriends(3, rndGen); JsonValue jo = JsonValueExtensions.CreateFrom(person); JsonValue jo2 = JsonValueExtensions.CreateFrom(person2); AssertAreEqual(person.Address.City, jo["Address"]["City"].ReadAs<string>(), "Address.City"); AssertAreEqual(person.Friends[1].Age, jo["Friends"][1]["Age"].ReadAs<int>(), "Friends[1].Age"); string newCityName = "Bellevue"; jo["Address"]["City"] = newCityName; AssertAreEqual(newCityName, (string)jo["Address"]["City"], "Address.City2"); jo["Friends"][1] = jo2; AssertAreEqual(person2.Age, (int)jo["Friends"][1]["Age"], "Friends[1].Age2"); AssertAreEqual(person2.Address.City, jo.ValueOrDefault("Friends").ValueOrDefault(1).ValueOrDefault("Address").ValueOrDefault("City").ReadAs<string>(), "Address.City3"); AssertAreEqual(person2.Age, (int)jo.ValueOrDefault("Friends").ValueOrDefault(1).ValueOrDefault("Age"), "Friends[1].Age3"); AssertAreEqual(person2.Address.City, jo.ValueOrDefault("Friends", 1, "Address", "City").ReadAs<string>(), "Address.City3"); AssertAreEqual(person2.Age, (int)jo.ValueOrDefault("Friends", 1, "Age"), "Friends[1].Age3"); int newAge = 42; JsonValue ageValue = jo["Friends"][1]["Age"] = newAge; AssertAreEqual(newAge, (int)ageValue, "Friends[1].Age4"); }