public void GetValueOrDefaultTest() { bool boolValue; JsonValue target; JsonValue jsonValue; Person person = AnyInstance.AnyPerson; JsonObject jo = JsonValueExtensions.CreateFrom(person) as JsonObject; Assert.Equal <int>(person.Age, jo.ValueOrDefault("Age").ReadAs <int>()); // JsonPrimitive Assert.Equal <string>(person.Address.ToString(), jo.ValueOrDefault("Address").ReadAsType <Address>().ToString()); // JsonObject Assert.Equal <int>(person.Friends.Count, jo.ValueOrDefault("Friends").Count); // JsonArray target = jo.ValueOrDefault("Address").ValueOrDefault("City"); // JsonPrimitive Assert.NotNull(target); Assert.Equal <string>(person.Address.City, target.ReadAs <string>()); target = jo.ValueOrDefault("Address", "City"); // JsonPrimitive Assert.NotNull(target); Assert.Equal <string>(person.Address.City, target.ReadAs <string>()); target = jo.ValueOrDefault("Address").ValueOrDefault("NonExistentProp").ValueOrDefault("NonExistentProp2"); // JsonObject Assert.Equal(JsonType.Default, target.JsonType); Assert.NotNull(target); Assert.False(target.TryReadAs <bool>(out boolValue)); Assert.True(target.TryReadAs <JsonValue>(out jsonValue)); target = jo.ValueOrDefault("Address", "NonExistentProp", "NonExistentProp2"); // JsonObject Assert.Equal(JsonType.Default, target.JsonType); Assert.NotNull(target); Assert.False(target.TryReadAs <bool>(out boolValue)); Assert.True(target.TryReadAs <JsonValue>(out jsonValue)); Assert.Same(target, jsonValue); }
public void DynamicRelationalOperators() { JsonValue jv = new JsonObject { { "one", 1 }, { "one_point_two", 1.2 }, { "decimal_one_point_one", 1.1m }, { "trueValue", true }, { "str", "hello" } }; dynamic dyn = jv; JsonValue defaultJsonValue = jv.ValueOrDefault(-1); Log.Info("Equality"); Assert.True(dyn.one == 1); Assert.True(dyn.one_point_two == 1.2); Assert.False(dyn.one == 1.2); Assert.False(dyn.one_point_two == 1); Assert.False(dyn.one == 2); Assert.False(dyn.one_point_two == 1.3); Assert.True(dyn.one == 1m); Assert.False(dyn.one == 2m); Assert.True(dyn.decimal_one_point_one == 1.1m); Assert.True(dyn.NotHere == null); Assert.True(dyn.NotHere == dyn.NotHere); Assert.True(dyn.NotHere == defaultJsonValue); // DISABLED, 197375, Assert.False(dyn.NotHere == 1); Assert.False(dyn.NotHere == jv); Log.Info("Inequality"); Assert.False(dyn.one != 1); Assert.False(dyn.one_point_two != 1.2); Assert.True(dyn.one != 1.2); Assert.True(dyn.one_point_two != 1); Assert.True(dyn.one != 2); Assert.True(dyn.one_point_two != 1.3); Assert.False(dyn.one != 1m); Assert.True(dyn.one != 2m); Assert.False(dyn.NotHere != null); Assert.False(dyn.NotHere != dyn.NotHere); Assert.False(dyn.NotHere != defaultJsonValue); // DISABLED, 197375, Assert.True(dyn.NotHere != 1); Assert.True(dyn.NotHere != jv); Log.Info("Less than"); Assert.True(dyn.one < 2); Assert.False(dyn.one < 1); Assert.False(dyn.one < 0); Assert.True(dyn.one_point_two < 1.3); Assert.False(dyn.one_point_two < 1.2); Assert.False(dyn.one_point_two < 1.1); Assert.True(dyn.one < 1.1); Assert.Equal(1 < 1.0, dyn.one < 1.0); Assert.False(dyn.one < 0.9); Assert.True(dyn.one_point_two < 2); Assert.False(dyn.one_point_two < 1); Assert.Equal(1.2 < 1.2f, dyn.one_point_two < 1.2f); Log.Info("Greater than"); Assert.False(dyn.one > 2); Assert.False(dyn.one > 1); Assert.True(dyn.one > 0); Assert.False(dyn.one_point_two > 1.3); Assert.False(dyn.one_point_two > 1.2); Assert.True(dyn.one_point_two > 1.1); Assert.False(dyn.one > 1.1); Assert.Equal(1 > 1.0, dyn.one > 1.0); Assert.True(dyn.one > 0.9); Assert.False(dyn.one_point_two > 2); Assert.True(dyn.one_point_two > 1); Assert.Equal(1.2 > 1.2f, dyn.one_point_two > 1.2f); Log.Info("Less than or equals"); Assert.True(dyn.one <= 2); Assert.True(dyn.one <= 1); Assert.False(dyn.one <= 0); Assert.True(dyn.one_point_two <= 1.3); Assert.True(dyn.one_point_two <= 1.2); Assert.False(dyn.one_point_two <= 1.1); Assert.True(dyn.one <= 1.1); Assert.Equal(1 <= 1.0, dyn.one <= 1.0); Assert.False(dyn.one <= 0.9); Assert.True(dyn.one_point_two <= 2); Assert.False(dyn.one_point_two <= 1); Assert.Equal(1.2 <= 1.2f, dyn.one_point_two <= 1.2f); Log.Info("Greater than or equals"); Assert.False(dyn.one >= 2); Assert.True(dyn.one >= 1); Assert.True(dyn.one >= 0); Assert.False(dyn.one_point_two >= 1.3); Assert.True(dyn.one_point_two >= 1.2); Assert.True(dyn.one_point_two >= 1.1); Assert.False(dyn.one >= 1.1); Assert.Equal(1 >= 1.0, dyn.one >= 1.0); Assert.True(dyn.one >= 0.9); Assert.False(dyn.one_point_two >= 2); Assert.True(dyn.one_point_two >= 1); Assert.Equal(1.2 >= 1.2f, dyn.one_point_two >= 1.2f); Log.Info("Invalid number conversions"); JsonValueTests.ExpectException <InvalidOperationException>(() => { Log.Info(dyn.decimal_one_point_one == 1.1); }); JsonValueTests.ExpectException <InvalidOperationException>(() => { Log.Info(dyn.one != (uint)2); }); Log.Info("Invalid data types for relational operators"); JsonValueTests.ExpectException <InvalidOperationException>(() => { Log.Info(dyn.trueValue >= dyn.trueValue); }); JsonValueTests.ExpectException <InvalidOperationException>(() => { Log.Info(dyn.NotHere < dyn.NotHere); }); JsonValueTests.ExpectException <InvalidOperationException>(() => { Log.Info(dyn.str < "Jello"); }); // DISABLED, 197315 Log.Info("Conversions from string"); jv = new JsonObject { { "one", "1" }, { "twelve_point_two", "1.22e1" } }; dyn = jv; Assert.True(dyn.one == 1); Assert.True(dyn.twelve_point_two == 1.22e1); Assert.True(dyn.one >= 0.5f); Assert.True(dyn.twelve_point_two <= 13); Assert.True(dyn.one < 2); Assert.Equal(dyn.twelve_point_two.ReadAs <int>() > 12, dyn.twelve_point_two > 12); }
public void DefaultConcatTest() { JsonValue jv = JsonValueExtensions.CreateFrom(AnyInstance.AnyPerson); dynamic target = JsonValueExtensions.CreateFrom(AnyInstance.AnyPerson); Person person = AnyInstance.AnyPerson; Assert.Equal(person.Address.City, target.Address.City.ReadAs <string>()); Assert.Equal(person.Friends[0].Age, target.Friends[0].Age.ReadAs <int>()); Assert.Equal(target.ValueOrDefault("Address").ValueOrDefault("City"), target.Address.City); Assert.Equal(target.ValueOrDefault("Address", "City"), target.Address.City); Assert.Equal(target.ValueOrDefault("Friends").ValueOrDefault(0).ValueOrDefault("Age"), target.Friends[0].Age); Assert.Equal(target.ValueOrDefault("Friends", 0, "Age"), target.Friends[0].Age); Assert.Equal(JsonType.Default, AnyInstance.AnyJsonValue1.ValueOrDefault((object[])null).JsonType); Assert.Equal(JsonType.Default, jv.ValueOrDefault("Friends", null).JsonType); Assert.Equal(JsonType.Default, AnyInstance.AnyJsonValue1.ValueOrDefault((string)null).JsonType); Assert.Equal(JsonType.Default, AnyInstance.AnyJsonPrimitive.ValueOrDefault(AnyInstance.AnyString, AnyInstance.AnyShort).JsonType); Assert.Equal(JsonType.Default, AnyInstance.AnyJsonArray.ValueOrDefault((string)null).JsonType); Assert.Equal(JsonType.Default, AnyInstance.AnyJsonObject.ValueOrDefault(AnyInstance.AnyString, null).JsonType); Assert.Equal(JsonType.Default, AnyInstance.AnyJsonArray.ValueOrDefault(-1).JsonType); Assert.Same(AnyInstance.AnyJsonValue1, AnyInstance.AnyJsonValue1.ValueOrDefault()); Assert.Same(AnyInstance.AnyJsonArray.ValueOrDefault(0), AnyInstance.AnyJsonArray.ValueOrDefault((short)0)); Assert.Same(AnyInstance.AnyJsonArray.ValueOrDefault(0), AnyInstance.AnyJsonArray.ValueOrDefault((ushort)0)); Assert.Same(AnyInstance.AnyJsonArray.ValueOrDefault(0), AnyInstance.AnyJsonArray.ValueOrDefault((byte)0)); Assert.Same(AnyInstance.AnyJsonArray.ValueOrDefault(0), AnyInstance.AnyJsonArray.ValueOrDefault((sbyte)0)); Assert.Same(AnyInstance.AnyJsonArray.ValueOrDefault(0), AnyInstance.AnyJsonArray.ValueOrDefault((char)0)); jv = new JsonObject(); jv[AnyInstance.AnyString] = AnyInstance.AnyJsonArray; Assert.Same(jv.ValueOrDefault(AnyInstance.AnyString, 0), jv.ValueOrDefault(AnyInstance.AnyString, (short)0)); Assert.Same(jv.ValueOrDefault(AnyInstance.AnyString, 0), jv.ValueOrDefault(AnyInstance.AnyString, (ushort)0)); Assert.Same(jv.ValueOrDefault(AnyInstance.AnyString, 0), jv.ValueOrDefault(AnyInstance.AnyString, (byte)0)); Assert.Same(jv.ValueOrDefault(AnyInstance.AnyString, 0), jv.ValueOrDefault(AnyInstance.AnyString, (sbyte)0)); Assert.Same(jv.ValueOrDefault(AnyInstance.AnyString, 0), jv.ValueOrDefault(AnyInstance.AnyString, (char)0)); jv = AnyInstance.AnyJsonObject; ExceptionHelper.Throws <ArgumentException>(delegate { var c = jv.ValueOrDefault(AnyInstance.AnyString, AnyInstance.AnyLong); }, String.Format(InvalidIndexType, typeof(long))); ExceptionHelper.Throws <ArgumentException>(delegate { var c = jv.ValueOrDefault(AnyInstance.AnyString, AnyInstance.AnyUInt); }, String.Format(InvalidIndexType, typeof(uint))); ExceptionHelper.Throws <ArgumentException>(delegate { var c = jv.ValueOrDefault(AnyInstance.AnyString, AnyInstance.AnyBool); }, String.Format(InvalidIndexType, typeof(bool))); }
public void DefaultConcatTest() { JsonValue jv = JsonValueExtensions.CreateFrom(AnyInstance.AnyPerson); dynamic target = JsonValueExtensions.CreateFrom(AnyInstance.AnyPerson); Person person = AnyInstance.AnyPerson; Assert.AreEqual(person.Address.City, target.Address.City.ReadAs<string>()); Assert.AreEqual(person.Friends[0].Age, target.Friends[0].Age.ReadAs<int>()); Assert.AreEqual(target.ValueOrDefault("Address").ValueOrDefault("City"), target.Address.City); Assert.AreEqual(target.ValueOrDefault("Address", "City"), target.Address.City); Assert.AreEqual(target.ValueOrDefault("Friends").ValueOrDefault(0).ValueOrDefault("Age"), target.Friends[0].Age); Assert.AreEqual(target.ValueOrDefault("Friends", 0, "Age"), target.Friends[0].Age); Assert.AreEqual(JsonType.Default, AnyInstance.AnyJsonValue1.ValueOrDefault((object[])null).JsonType); Assert.AreEqual(JsonType.Default, jv.ValueOrDefault("Friends", null).JsonType); Assert.AreEqual(JsonType.Default, AnyInstance.AnyJsonValue1.ValueOrDefault((string)null).JsonType); Assert.AreEqual(JsonType.Default, AnyInstance.AnyJsonPrimitive.ValueOrDefault(AnyInstance.AnyString, AnyInstance.AnyShort).JsonType); Assert.AreEqual(JsonType.Default, AnyInstance.AnyJsonArray.ValueOrDefault((string)null).JsonType); Assert.AreEqual(JsonType.Default, AnyInstance.AnyJsonObject.ValueOrDefault(AnyInstance.AnyString, null).JsonType); Assert.AreEqual(JsonType.Default, AnyInstance.AnyJsonArray.ValueOrDefault(-1).JsonType); Assert.AreSame(AnyInstance.AnyJsonValue1, AnyInstance.AnyJsonValue1.ValueOrDefault()); Assert.AreSame(AnyInstance.AnyJsonArray.ValueOrDefault(0), AnyInstance.AnyJsonArray.ValueOrDefault((short)0)); Assert.AreSame(AnyInstance.AnyJsonArray.ValueOrDefault(0), AnyInstance.AnyJsonArray.ValueOrDefault((ushort)0)); Assert.AreSame(AnyInstance.AnyJsonArray.ValueOrDefault(0), AnyInstance.AnyJsonArray.ValueOrDefault((byte)0)); Assert.AreSame(AnyInstance.AnyJsonArray.ValueOrDefault(0), AnyInstance.AnyJsonArray.ValueOrDefault((sbyte)0)); Assert.AreSame(AnyInstance.AnyJsonArray.ValueOrDefault(0), AnyInstance.AnyJsonArray.ValueOrDefault((char)0)); jv = new JsonObject(); jv[AnyInstance.AnyString] = AnyInstance.AnyJsonArray; Assert.AreSame(jv.ValueOrDefault(AnyInstance.AnyString, 0), jv.ValueOrDefault(AnyInstance.AnyString, (short)0)); Assert.AreSame(jv.ValueOrDefault(AnyInstance.AnyString, 0), jv.ValueOrDefault(AnyInstance.AnyString, (ushort)0)); Assert.AreSame(jv.ValueOrDefault(AnyInstance.AnyString, 0), jv.ValueOrDefault(AnyInstance.AnyString, (byte)0)); Assert.AreSame(jv.ValueOrDefault(AnyInstance.AnyString, 0), jv.ValueOrDefault(AnyInstance.AnyString, (sbyte)0)); Assert.AreSame(jv.ValueOrDefault(AnyInstance.AnyString, 0), jv.ValueOrDefault(AnyInstance.AnyString, (char)0)); jv = AnyInstance.AnyJsonObject; ExceptionTestHelper.ExpectException<ArgumentException>(delegate { var c = jv.ValueOrDefault(AnyInstance.AnyString, AnyInstance.AnyLong); }, string.Format(InvalidIndexType, typeof(long))); ExceptionTestHelper.ExpectException<ArgumentException>(delegate { var c = jv.ValueOrDefault(AnyInstance.AnyString, AnyInstance.AnyUInt); }, string.Format(InvalidIndexType, typeof(uint))); ExceptionTestHelper.ExpectException<ArgumentException>(delegate { var c = jv.ValueOrDefault(AnyInstance.AnyString, AnyInstance.AnyBool); }, string.Format(InvalidIndexType, typeof(bool))); }