diff --git a/include/json.h b/include/json.h index 3706034fd..27e621ebb 100644 --- a/include/json.h +++ b/include/json.h @@ -262,6 +262,11 @@ namespace crow return i(); } + explicit operator uint64_t() const + { + return u(); + } + explicit operator int() const { return (int)i(); @@ -294,6 +299,20 @@ namespace crow return boost::lexical_cast(start_, end_-start_); } + uint64_t u() const + { +#ifndef CROW_JSON_NO_ERROR_CHECK + switch (t()) { + case type::Number: + case type::String: + return boost::lexical_cast(start_, end_-start_); + default: + throw std::runtime_error(std::string("expected number, got: ") + get_type_str(t())); + } +#endif + return boost::lexical_cast(start_, end_-start_); + } + double d() const { #ifndef CROW_JSON_NO_ERROR_CHECK @@ -1289,7 +1308,7 @@ namespace crow o = std::unique_ptr< std::unordered_map >( - new std::unordered_map{})); + new std::unordered_map{}); return (*o)[str]; } diff --git a/tests/unittest.cpp b/tests/unittest.cpp index 9021af5e1..78732b090 100644 --- a/tests/unittest.cpp +++ b/tests/unittest.cpp @@ -519,6 +519,10 @@ TEST(json_read) ASSERT_EQUAL(false, z["bools"][1].b()); ASSERT_EQUAL(1.2, z["doubles"][0].d()); ASSERT_EQUAL(-3.4, z["doubles"][1].d()); + + std::string s3 = R"({"uint64": 18446744073709551615})"; + auto z1 = json::load(s3); + ASSERT_EQUAL(18446744073709551615ull, z1["uint64"].u()); } TEST(json_read_real)