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