public void ReadStringOptional() { JObject jobj = JObject.Parse("{'foo':'abc'}"); // Test success case. string val = string.Empty; JsonReadOptionalUtils.ReadStringOptional(jobj, "foo", x => val = x); Assert.AreEqual("abc", val); // Test missing optional. val = string.Empty; JsonReadOptionalUtils.ReadStringOptional(jobj, "bar", x => val = x); Assert.AreEqual(string.Empty, val); }
public void ReadStringOptional() { JsonDocument jdoc = JsonDocument.Parse("{\"foo\":\"abc\"}"); // Test success case. string val = string.Empty; JsonReadOptionalUtils.ReadStringOptional(jdoc.RootElement, "foo", x => val = x); Assert.Equal("abc", val); // Test missing optional. val = string.Empty; JsonReadOptionalUtils.ReadStringOptional(jdoc.RootElement, "bar", x => val = x); Assert.Equal(string.Empty, val); }
public void ReadDoubleOptional() { JsonDocument jdoc = JsonDocument.Parse("{\"foo\":1234.5}"); // Test success case. double val = -1.0; JsonReadOptionalUtils.ReadDoubleOptional(jdoc.RootElement, "foo", x => val = x); Assert.Equal(1234.5, val); // Test missing optional. val = -1.0; JsonReadOptionalUtils.ReadDoubleOptional(jdoc.RootElement, "bar", x => val = x); Assert.Equal(-1.0, val); // Test invalid value. jdoc = JsonDocument.Parse("{\"foo\":\"abc\"}"); Assert.Throws <InvalidOperationException>(() => JsonReadOptionalUtils.ReadDoubleOptional(jdoc.RootElement, "foo", x => val = x)); }
public void ReadBoolOptional() { JsonDocument jdoc = JsonDocument.Parse("{\"foo\":true}"); // Test success case. bool val = false; JsonReadOptionalUtils.ReadBoolOptional(jdoc.RootElement, "foo", x => val = x); Assert.True(val); // Test missing optional. val = false; JsonReadOptionalUtils.ReadBoolOptional(jdoc.RootElement, "bar", x => val = x); Assert.False(val); // Test invalid value. jdoc = JsonDocument.Parse("{\"foo\":\"abc\"}"); Assert.Throws <InvalidOperationException>(() => JsonReadOptionalUtils.ReadBoolOptional(jdoc.RootElement, "foo", x => val = x)); }
public void ReadDoubleOptional() { JObject jobj = JObject.Parse("{'foo':'1234.5'}"); // Test success case. double val = -1.0; JsonReadOptionalUtils.ReadDoubleOptional(jobj, "foo", x => val = x); Assert.AreEqual(1234.5, val); // Test missing optional. val = -1.0; JsonReadOptionalUtils.ReadDoubleOptional(jobj, "bar", x => val = x); Assert.AreEqual(-1.0, val); // Test invalid value. jobj = JObject.Parse("{'foo':'abc'}"); Assert.ThrowsException <FormatException>(() => JsonReadOptionalUtils.ReadDoubleOptional(jobj, "foo", x => val = x)); }
public void ReadBoolOptional() { JObject jobj = JObject.Parse("{'foo':true}"); // Test success case. bool val = false; JsonReadOptionalUtils.ReadBoolOptional(jobj, "foo", x => val = x); Assert.AreEqual(true, val); // Test missing optional. val = false; JsonReadOptionalUtils.ReadBoolOptional(jobj, "bar", x => val = x); Assert.AreEqual(false, val); // Test invalid value. jobj = JObject.Parse("{'foo':'abc'}"); Assert.ThrowsException <FormatException>(() => JsonReadOptionalUtils.ReadBoolOptional(jobj, "foo", x => val = x)); }
public void ReadIntOptional() { JsonDocument jdoc = JsonDocument.Parse("{\"foo\":1234}"); // Test success case. int val = -1; JsonReadOptionalUtils.ReadIntOptional(jdoc.RootElement, "foo", x => val = x); Assert.AreEqual(1234, val); // Test missing optional. val = -1; JsonReadOptionalUtils.ReadIntOptional(jdoc.RootElement, "bar", x => val = x); Assert.AreEqual(-1, val); // Test invalid value. jdoc = JsonDocument.Parse("{\"foo\":\"abc\"}"); Assert.ThrowsException <InvalidOperationException>(() => JsonReadOptionalUtils.ReadIntOptional(jdoc.RootElement, "foo", x => val = x)); }