diff options
-rw-r--r-- | amalgamate/crow_all.h | 9 | ||||
-rw-r--r-- | include/json.h | 9 | ||||
-rw-r--r-- | tests/unittest.cpp | 8 |
3 files changed, 26 insertions, 0 deletions
diff --git a/amalgamate/crow_all.h b/amalgamate/crow_all.h index 2294438..30129f7 100644 --- a/amalgamate/crow_all.h +++ b/amalgamate/crow_all.h @@ -1048,6 +1048,15 @@ namespace crow return boost::lexical_cast<double>(start_, end_-start_); } + bool b() const + { +#ifndef CROW_JSON_NO_ERROR_CHECK + if (t() != type::True && t() != type::False) + throw std::runtime_error("value is not boolean"); +#endif + return t() == type::True; + } + void unescape() const { if (*(start_-1)) diff --git a/include/json.h b/include/json.h index d79fe88..3fc0911 100644 --- a/include/json.h +++ b/include/json.h @@ -284,6 +284,15 @@ namespace crow return boost::lexical_cast<double>(start_, end_-start_); } + bool b() const + { +#ifndef CROW_JSON_NO_ERROR_CHECK + if (t() != type::True && t() != type::False) + throw std::runtime_error("value is not boolean"); +#endif + return t() == type::True; + } + void unescape() const { if (*(start_-1)) diff --git a/tests/unittest.cpp b/tests/unittest.cpp index 4dec727..2b6a20a 100644 --- a/tests/unittest.cpp +++ b/tests/unittest.cpp @@ -453,6 +453,14 @@ TEST(json_read) q = y["ints"][2].i(); ASSERT_EQUAL(3, q); + std::string s2 = R"({"bools":[true, false], "doubles":[1.2, -3.4]})"; + auto z = json::load(s2); + ASSERT_EQUAL(2, z["bools"].size()); + ASSERT_EQUAL(2, z["doubles"].size()); + ASSERT_EQUAL(true, z["bools"][0].b()); + ASSERT_EQUAL(false, z["bools"][1].b()); + ASSERT_EQUAL(1.2, z["doubles"][0].d()); + ASSERT_EQUAL(-3.4, z["doubles"][1].d()); } TEST(json_read_unescaping) |