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:
Jaeseung Ha 2015-11-28 17:28:39 +09:00
commit 78cbae4e2f
2 changed files with 38 additions and 4 deletions

View File

@ -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_);
}

View File

@ -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_);
}