mirror of
https://github.com/CrowCpp/Crow.git
synced 2024-06-07 21:10:44 +00:00
Merge pull request #91 from SamuelMarks/json_str_to_i
[Feature] JSON body `i()` now works with strings. Added better error messages also.
This commit is contained in:
commit
78cbae4e2f
@ -1214,6 +1214,18 @@ namespace crow
|
||||
Object,
|
||||
};
|
||||
|
||||
const char* get_type_str(type t) {
|
||||
switch(t){
|
||||
case type::Number: return "Number";
|
||||
case type::False: return "False";
|
||||
case type::True: return "True";
|
||||
case type::List: return "List";
|
||||
case type::String: return "String";
|
||||
case type::Object: return "Object";
|
||||
default: return "Unknown";
|
||||
}
|
||||
};
|
||||
|
||||
class rvalue;
|
||||
rvalue load(const char* data, size_t size);
|
||||
|
||||
@ -1401,8 +1413,13 @@ namespace crow
|
||||
int64_t i() const
|
||||
{
|
||||
#ifndef CROW_JSON_NO_ERROR_CHECK
|
||||
if (t() != type::Number)
|
||||
throw std::runtime_error("value is not number");
|
||||
switch (t()) {
|
||||
case type::Number:
|
||||
case type::String:
|
||||
return boost::lexical_cast<int64_t>(start_, end_-start_);
|
||||
default:
|
||||
throw std::runtime_error(strcat("expected number, got: ", get_type_str(t())));
|
||||
}
|
||||
#endif
|
||||
return boost::lexical_cast<int64_t>(start_, end_-start_);
|
||||
}
|
||||
|
@ -82,6 +82,18 @@ namespace crow
|
||||
Object,
|
||||
};
|
||||
|
||||
const char* get_type_str(type t) {
|
||||
switch(t){
|
||||
case type::Number: return "Number";
|
||||
case type::False: return "False";
|
||||
case type::True: return "True";
|
||||
case type::List: return "List";
|
||||
case type::String: return "String";
|
||||
case type::Object: return "Object";
|
||||
default: return "Unknown";
|
||||
}
|
||||
};
|
||||
|
||||
class rvalue;
|
||||
rvalue load(const char* data, size_t size);
|
||||
|
||||
@ -269,8 +281,13 @@ namespace crow
|
||||
int64_t i() const
|
||||
{
|
||||
#ifndef CROW_JSON_NO_ERROR_CHECK
|
||||
if (t() != type::Number)
|
||||
throw std::runtime_error("value is not number");
|
||||
switch (t()) {
|
||||
case type::Number:
|
||||
case type::String:
|
||||
return boost::lexical_cast<int64_t>(start_, end_-start_);
|
||||
default:
|
||||
throw std::runtime_error(strcat("expected number, got: ", get_type_str(t())));
|
||||
}
|
||||
#endif
|
||||
return boost::lexical_cast<int64_t>(start_, end_-start_);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user