aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoripkn <ipknhama@gmail.com>2014-11-12 09:23:21 +0900
committeripkn <ipknhama@gmail.com>2014-11-12 09:23:21 +0900
commit21b027774e4c472d27d8726774aad1aaed95ea42 (patch)
tree07187af58764de4f50fc31cc834a064736b76345
parent79e8fa19f603fc5d408370a3579ab5a67aa87bbc (diff)
parent5120b127b0c167e11c9f7c8495b5b407aea32d76 (diff)
downloadcrow-21b027774e4c472d27d8726774aad1aaed95ea42.tar.gz
crow-21b027774e4c472d27d8726774aad1aaed95ea42.zip
Merge pull request #38 from acron0/json-bool
Added boolean types to json rvalue (.b()) including test
-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)