aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--amalgamate/crow_all.h9
-rw-r--r--include/json.h9
-rw-r--r--tests/unittest.cpp8
3 files changed, 26 insertions, 0 deletions
diff --git a/amalgamate/crow_all.h b/amalgamate/crow_all.h
index b84a43e..a2f9cc0 100644
--- a/amalgamate/crow_all.h
+++ b/amalgamate/crow_all.h
@@ -628,6 +628,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 4fe03a7..68f06a2 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 0e23ffd..57a4324 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_real)