More documentation. (#2007)

This commit is contained in:
Daniel Lemire
2023-05-23 11:15:07 -04:00
committed by GitHub
parent cb735818ec
commit 0998233d58
2 changed files with 38 additions and 0 deletions
+22
View File
@@ -1149,6 +1149,28 @@ int main(void) {
}
```
You can do handle errors gracefully as well...
```C++
#include <iostream>
#include "simdjson.h"
int main(void) {
simdjson::ondemand::parser parser;
simdjson::padded_string json_string;
simdjson::ondemand::document doc;
try {
json_string = padded_string::load("twitter.json");
doc = parser.iterate(json_string);
uint64_t identifier = doc["statuses"].at(0)["id"];
std::cout << identifier << std::endl;
} catch (simdjson::simdjson_error &error) {
std::cerr << "JSON error: " << error.what() << " near "
<< doc.current_location() << " in " << json_string << std::endl;
}
}
```
### Current location in document
Sometimes, it might be helpful to know the current location in the document during iteration. This is especially useful when encountering errors. The `current_location()` method on a
@@ -974,6 +974,22 @@ int load_example_except() {
std::cout << identifier << std::endl;
return EXIT_SUCCESS;
}
int load_example_except_morecomplete(void) {
TEST_START();
simdjson::ondemand::parser parser;
simdjson::padded_string json_string;
simdjson::ondemand::document doc;
try {
json_string = padded_string::load("twitter.json");
doc = parser.iterate(json_string);
uint64_t identifier = doc["statuses"].at(0)["id"];
std::cout << identifier << std::endl;
} catch (simdjson::simdjson_error &error) {
std::cerr << "JSON error: " << error.what() << " near "
<< doc.current_location() << " in " << json_string << std::endl;
}
return EXIT_SUCCESS;
}
#endif
bool test_load_example() {
TEST_START();