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