mirror of
https://github.com/simdjson/simdjson
synced 2026-06-08 17:27:07 +00:00
More documentation. (#2007)
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user