Add u() for uint64_t: manually applying #129

This commit is contained in:
ipknHama 2016-08-27 18:12:17 +09:00
parent e161da45e0
commit f88b56dff0
2 changed files with 24 additions and 1 deletions

View File

@ -262,6 +262,11 @@ namespace crow
return i(); return i();
} }
explicit operator uint64_t() const
{
return u();
}
explicit operator int() const explicit operator int() const
{ {
return (int)i(); return (int)i();
@ -294,6 +299,20 @@ namespace crow
return boost::lexical_cast<int64_t>(start_, end_-start_); return boost::lexical_cast<int64_t>(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<uint64_t>(start_, end_-start_);
default:
throw std::runtime_error(std::string("expected number, got: ") + get_type_str(t()));
}
#endif
return boost::lexical_cast<uint64_t>(start_, end_-start_);
}
double d() const double d() const
{ {
#ifndef CROW_JSON_NO_ERROR_CHECK #ifndef CROW_JSON_NO_ERROR_CHECK
@ -1289,7 +1308,7 @@ namespace crow
o = std::unique_ptr< o = std::unique_ptr<
std::unordered_map<std::string, wvalue> std::unordered_map<std::string, wvalue>
>( >(
new std::unordered_map<std::string, wvalue>{})); new std::unordered_map<std::string, wvalue>{});
return (*o)[str]; return (*o)[str];
} }

View File

@ -519,6 +519,10 @@ TEST(json_read)
ASSERT_EQUAL(false, z["bools"][1].b()); ASSERT_EQUAL(false, z["bools"][1].b());
ASSERT_EQUAL(1.2, z["doubles"][0].d()); ASSERT_EQUAL(1.2, z["doubles"][0].d());
ASSERT_EQUAL(-3.4, z["doubles"][1].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) TEST(json_read_real)