Support cout << error
This commit is contained in:
parent
31e8a12e88
commit
3bdfe167de
|
@ -179,7 +179,7 @@ if (!parser.allocate_capacity(1024*1024)) { exit(1); }
|
|||
for (padded_string json : { string("[1, 2, 3]"), string("true"), string("[ true, false ]") }) {
|
||||
cout << "Parsing " << json.data() << " ..." << endl;
|
||||
auto [doc, error] = parser.parse(json);
|
||||
if (error) { cerr << "Error: " << error_message(error) << endl; exit(1); }
|
||||
if (error) { cerr << "Error: " << error << endl; exit(1); }
|
||||
doc.print_json(cout);
|
||||
cout << endl;
|
||||
}
|
||||
|
|
|
@ -132,7 +132,7 @@ int main(int argc, char *argv[]) {
|
|||
if (error) {
|
||||
std::cout << "parse failed" << std::endl;
|
||||
std::cout << "error code: " << error << std::endl;
|
||||
std::cout << error_message(error) << std::endl;
|
||||
std::cout << error << std::endl;
|
||||
} else {
|
||||
std::cout << "parse valid" << std::endl;
|
||||
}
|
||||
|
@ -150,7 +150,7 @@ int main(int argc, char *argv[]) {
|
|||
if (error) {
|
||||
std::cout << "parse_many failed" << std::endl;
|
||||
std::cout << "error code: " << error << std::endl;
|
||||
std::cout << error_message(error) << std::endl;
|
||||
std::cout << error << std::endl;
|
||||
} else {
|
||||
std::cout << "parse_many valid" << std::endl;
|
||||
}
|
||||
|
|
|
@ -96,7 +96,7 @@ int main (int argc, char *argv[]){
|
|||
batch_size_res[i] = speedinGBs;
|
||||
|
||||
if (error != simdjson::SUCCESS) {
|
||||
std::wcerr << "Parsing failed with: " << simdjson::error_message(error) << std::endl;
|
||||
std::wcerr << "Parsing failed with: " << error << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
@ -132,7 +132,7 @@ int main (int argc, char *argv[]){
|
|||
res.push_back(secs.count());
|
||||
|
||||
if (error != simdjson::SUCCESS) {
|
||||
std::wcerr << "Parsing failed with: " << simdjson::error_message(error) << std::endl;
|
||||
std::wcerr << "Parsing failed with: " << error << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
|
|
@ -949,7 +949,7 @@ public:
|
|||
*
|
||||
* document::parser parser;
|
||||
* for (auto [doc, error] : parser.parse_many(buf, len)) {
|
||||
* if (error) { cerr << error_message(error) << endl; exit(1); }
|
||||
* if (error) { cerr << error << endl; exit(1); }
|
||||
* cout << std::string(doc["title"]) << endl;
|
||||
* }
|
||||
*
|
||||
|
@ -1011,7 +1011,7 @@ public:
|
|||
*
|
||||
* document::parser parser;
|
||||
* for (auto [doc, error] : parser.parse_many(buf, len)) {
|
||||
* if (error) { cerr << error_message(error) << endl; exit(1); }
|
||||
* if (error) { cerr << error << endl; exit(1); }
|
||||
* cout << std::string(doc["title"]) << endl;
|
||||
* }
|
||||
*
|
||||
|
@ -1073,7 +1073,7 @@ public:
|
|||
*
|
||||
* document::parser parser;
|
||||
* for (auto [doc, error] : parser.parse_many(buf, len)) {
|
||||
* if (error) { cerr << error_message(error) << endl; exit(1); }
|
||||
* if (error) { cerr << error << endl; exit(1); }
|
||||
* cout << std::string(doc["title"]) << endl;
|
||||
* }
|
||||
*
|
||||
|
@ -1134,7 +1134,7 @@ public:
|
|||
*
|
||||
* document::parser parser;
|
||||
* for (auto [doc, error] : parser.parse_many(buf, len)) {
|
||||
* if (error) { cerr << error_message(error) << endl; exit(1); }
|
||||
* if (error) { cerr << error << endl; exit(1); }
|
||||
* cout << std::string(doc["title"]) << endl;
|
||||
* }
|
||||
*
|
||||
|
|
|
@ -44,10 +44,26 @@ enum error_code {
|
|||
*/
|
||||
inline const char *error_message(error_code error) noexcept;
|
||||
|
||||
/**
|
||||
* Write the error message to the output stream
|
||||
*/
|
||||
inline std::ostream& operator<<(std::ostream& out, error_code error) noexcept;
|
||||
|
||||
/**
|
||||
* Exception thrown when an exception-supporting simdjson method is called
|
||||
*/
|
||||
struct invalid_json : public std::exception {
|
||||
invalid_json(error_code _error) noexcept : error{_error} { }
|
||||
const char *what() const noexcept { return error_message(error); }
|
||||
error_code error;
|
||||
/**
|
||||
* Create an exception from a simdjson error code.
|
||||
* @param error The error code
|
||||
*/
|
||||
invalid_json(error_code error) noexcept : _error{error} { }
|
||||
/** The error message */
|
||||
const char *what() const noexcept { return error_message(error()); }
|
||||
error_code error() const noexcept { return _error; }
|
||||
private:
|
||||
/** The error code that was used */
|
||||
error_code _error;
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -50,6 +50,10 @@ inline const std::string &error_message(int error) noexcept {
|
|||
return internal::error_codes[error].message;
|
||||
}
|
||||
|
||||
inline std::ostream& operator<<(std::ostream& out, error_code error) noexcept {
|
||||
return out << error_message(error);
|
||||
}
|
||||
|
||||
} // namespace simdjson
|
||||
|
||||
#endif // SIMDJSON_INLINE_ERROR_H
|
||||
|
|
|
@ -13,7 +13,7 @@ int main(int argc, char *argv[]) {
|
|||
if (error) {
|
||||
std::cout << "document::parse failed" << std::endl;
|
||||
std::cout << "error code: " << error << std::endl;
|
||||
std::cout << error_message(error) << std::endl;
|
||||
std::cout << error << std::endl;
|
||||
} else {
|
||||
std::cout << "document::parse valid" << std::endl;
|
||||
}
|
||||
|
|
|
@ -254,7 +254,7 @@ static bool parse_json_message_issue467(char const* message, std::size_t len, si
|
|||
simdjson::padded_string str(message,len);
|
||||
for (auto [doc, error] : parser.parse_many(str, parser.capacity())) {
|
||||
if (error) {
|
||||
std::cerr << "Failed with simdjson error= " << simdjson::error_message(error) << std::endl;
|
||||
std::cerr << "Failed with simdjson error= " << error << std::endl;
|
||||
return false;
|
||||
}
|
||||
count++;
|
||||
|
@ -784,7 +784,7 @@ namespace dom_api {
|
|||
if (uint64_t(doc["a"]) != 1) { cerr << "Expected uint64_t(doc[\"a\"]) to be 1, was " << uint64_t(doc["a"]) << endl; return false; }
|
||||
|
||||
auto [val, error] = doc["d"];
|
||||
if (error != simdjson::NO_SUCH_FIELD) { cerr << "Expected NO_SUCH_FIELD error for uint64_t(doc[\"d\"]), got " << error_message(error) << endl; return false; }
|
||||
if (error != simdjson::NO_SUCH_FIELD) { cerr << "Expected NO_SUCH_FIELD error for uint64_t(doc[\"d\"]), got " << error << endl; return false; }
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -802,7 +802,7 @@ namespace dom_api {
|
|||
if (uint64_t(obj["a"]) != 1) { cerr << "Expected uint64_t(obj[\"a\"]) to be 1, was " << uint64_t(obj["a"]) << endl; return false; }
|
||||
|
||||
auto [val, error] = obj["d"];
|
||||
if (error != simdjson::NO_SUCH_FIELD) { cerr << "Expected NO_SUCH_FIELD error for uint64_t(obj[\"d\"]), got " << error_message(error) << endl; return false; }
|
||||
if (error != simdjson::NO_SUCH_FIELD) { cerr << "Expected NO_SUCH_FIELD error for uint64_t(obj[\"d\"]), got " << error << endl; return false; }
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -99,7 +99,7 @@ bool validate(const char *dirname) {
|
|||
} else if (starts_with("pass", name) and (has_extension(extension1, name) or has_extension(extension2, name)) and error) {
|
||||
is_file_as_expected[i] = false;
|
||||
printf("warning: file %s should pass but it fails. Error is: %s\n",
|
||||
name, simdjson::error_message(error).data());
|
||||
name, error_message(error));
|
||||
printf("size of file in bytes: %zu \n", json.size());
|
||||
everything_fine = false;
|
||||
} else if ( starts_with("fail", name) and (not starts_with("fail10.json", name)) and !error) {
|
||||
|
|
|
@ -8,7 +8,7 @@ void document_parse_error_code() {
|
|||
|
||||
string json("[ 1, 2, 3 ]");
|
||||
auto [doc, error] = document::parse(json);
|
||||
if (error) { cerr << "Error: " << error_message(error) << endl; exit(1); }
|
||||
if (error) { cerr << "Error: " << error << endl; exit(1); }
|
||||
if (!doc.print_json(cout)) { exit(1); }
|
||||
cout << endl;
|
||||
}
|
||||
|
@ -51,7 +51,7 @@ void parser_parse() {
|
|||
for (padded_string json : { string("[1, 2, 3]"), string("true"), string("[ true, false ]") }) {
|
||||
cout << "Parsing " << json.data() << " ..." << endl;
|
||||
auto [doc, error] = parser.parse(json);
|
||||
if (error) { cerr << "Error: " << error_message(error) << endl; exit(1); }
|
||||
if (error) { cerr << "Error: " << error << endl; exit(1); }
|
||||
if (!doc.print_json(cout)) { exit(1); }
|
||||
cout << endl;
|
||||
}
|
||||
|
@ -65,7 +65,7 @@ void parser_parse_many_error_code() {
|
|||
cout << "Parsing " << json.data() << " ..." << endl;
|
||||
document::parser parser;
|
||||
for (auto [doc, error] : parser.parse_many(json)) {
|
||||
if (error) { cerr << "Error: " << error_message(error) << endl; exit(1); }
|
||||
if (error) { cerr << "Error: " << error << endl; exit(1); }
|
||||
if (!doc.print_json(cout)) { exit(1); }
|
||||
cout << endl;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue