Convert tools/docs/benchmarks to bool get() idiom
This commit is contained in:
parent
56e2b38048
commit
f336103f63
|
@ -18,17 +18,15 @@ static void numbers_scan(State& state) {
|
||||||
dom::parser parser;
|
dom::parser parser;
|
||||||
dom::array arr;
|
dom::array arr;
|
||||||
simdjson::error_code error;
|
simdjson::error_code error;
|
||||||
parser.load(NUMBERS_JSON).get<dom::array>().tie(arr, error);
|
if (!parser.load(NUMBERS_JSON).get(arr, error)) {
|
||||||
if(error) {
|
cerr << "could not read " << NUMBERS_JSON << " as an array: " << error << endl;
|
||||||
cerr << "could not read " << NUMBERS_JSON << " as an array" << endl;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (UNUSED auto _ : state) {
|
for (UNUSED auto _ : state) {
|
||||||
std::vector<double> container;
|
std::vector<double> container;
|
||||||
for (auto e : arr) {
|
for (auto e : arr) {
|
||||||
double x;
|
double x;
|
||||||
e.get<double>().tie(x,error);
|
if (!e.get(x,error)) { cerr << "found a node that is not an number: " << error << endl; break;}
|
||||||
if(error) { cerr << "found a node that is not an number?" << endl; break;}
|
|
||||||
container.push_back(x);
|
container.push_back(x);
|
||||||
}
|
}
|
||||||
benchmark::DoNotOptimize(container.data());
|
benchmark::DoNotOptimize(container.data());
|
||||||
|
@ -42,9 +40,8 @@ static void numbers_size_scan(State& state) {
|
||||||
dom::parser parser;
|
dom::parser parser;
|
||||||
dom::array arr;
|
dom::array arr;
|
||||||
simdjson::error_code error;
|
simdjson::error_code error;
|
||||||
parser.load(NUMBERS_JSON).get<dom::array>().tie(arr, error);
|
if(!parser.load(NUMBERS_JSON).get(arr, error)) {
|
||||||
if(error) {
|
cerr << "could not read " << NUMBERS_JSON << " as an array: " << error << endl;
|
||||||
cerr << "could not read " << NUMBERS_JSON << " as an array" << endl;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (UNUSED auto _ : state) {
|
for (UNUSED auto _ : state) {
|
||||||
|
@ -53,8 +50,7 @@ static void numbers_size_scan(State& state) {
|
||||||
size_t pos = 0;
|
size_t pos = 0;
|
||||||
for (auto e : arr) {
|
for (auto e : arr) {
|
||||||
double x;
|
double x;
|
||||||
e.get<double>().tie(x,error);
|
if (!e.get(x,error)) { cerr << "found a node that is not an number: " << error << endl; break;}
|
||||||
if(error) { cerr << "found a node that is not an number?" << endl; break;}
|
|
||||||
container[pos++] = x;
|
container[pos++] = x;
|
||||||
}
|
}
|
||||||
if(pos != container.size()) { cerr << "bad count" << endl; }
|
if(pos != container.size()) { cerr << "bad count" << endl; }
|
||||||
|
@ -70,8 +66,7 @@ static void numbers_type_scan(State& state) {
|
||||||
dom::parser parser;
|
dom::parser parser;
|
||||||
dom::array arr;
|
dom::array arr;
|
||||||
simdjson::error_code error;
|
simdjson::error_code error;
|
||||||
parser.load(NUMBERS_JSON).get<dom::array>().tie(arr, error);
|
if(!parser.load(NUMBERS_JSON).get(arr, error)) {
|
||||||
if(error) {
|
|
||||||
cerr << "could not read " << NUMBERS_JSON << " as an array" << endl;
|
cerr << "could not read " << NUMBERS_JSON << " as an array" << endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -83,7 +78,7 @@ static void numbers_type_scan(State& state) {
|
||||||
cerr << "found a node that is not an number?" << endl; break;
|
cerr << "found a node that is not an number?" << endl; break;
|
||||||
}
|
}
|
||||||
double x;
|
double x;
|
||||||
e.get<double>().tie(x,error);
|
e.get(x,error);
|
||||||
container.push_back(x);
|
container.push_back(x);
|
||||||
}
|
}
|
||||||
benchmark::DoNotOptimize(container.data());
|
benchmark::DoNotOptimize(container.data());
|
||||||
|
@ -97,9 +92,8 @@ static void numbers_type_size_scan(State& state) {
|
||||||
dom::parser parser;
|
dom::parser parser;
|
||||||
dom::array arr;
|
dom::array arr;
|
||||||
simdjson::error_code error;
|
simdjson::error_code error;
|
||||||
parser.load(NUMBERS_JSON).get<dom::array>().tie(arr, error);
|
if (!parser.load(NUMBERS_JSON).get(arr, error)) {
|
||||||
if(error) {
|
cerr << "could not read " << NUMBERS_JSON << " as an array: " << error << endl;
|
||||||
cerr << "could not read " << NUMBERS_JSON << " as an array" << endl;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (UNUSED auto _ : state) {
|
for (UNUSED auto _ : state) {
|
||||||
|
@ -112,7 +106,7 @@ static void numbers_type_size_scan(State& state) {
|
||||||
cerr << "found a node that is not an number?" << endl; break;
|
cerr << "found a node that is not an number?" << endl; break;
|
||||||
}
|
}
|
||||||
double x;
|
double x;
|
||||||
e.get<double>().tie(x,error);
|
e.get(x,error);
|
||||||
container[pos++] = x;
|
container[pos++] = x;
|
||||||
}
|
}
|
||||||
if(pos != container.size()) { cerr << "bad count" << endl; }
|
if(pos != container.size()) { cerr << "bad count" << endl; }
|
||||||
|
@ -129,16 +123,14 @@ static void numbers_load_scan(State& state) {
|
||||||
simdjson::error_code error;
|
simdjson::error_code error;
|
||||||
for (UNUSED auto _ : state) {
|
for (UNUSED auto _ : state) {
|
||||||
// this may hit the disk, but probably just once
|
// this may hit the disk, but probably just once
|
||||||
parser.load(NUMBERS_JSON).get<dom::array>().tie(arr, error);
|
if (!parser.load(NUMBERS_JSON).get(arr, error)) {
|
||||||
if(error) {
|
cerr << "could not read " << NUMBERS_JSON << " as an array: " << error << endl;
|
||||||
cerr << "could not read " << NUMBERS_JSON << " as an array" << endl;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
std::vector<double> container;
|
std::vector<double> container;
|
||||||
for (auto e : arr) {
|
for (auto e : arr) {
|
||||||
double x;
|
double x;
|
||||||
e.get<double>().tie(x,error);
|
if (!e.get(x,error)) { cerr << "found a node that is not an number: " << error << endl; break;}
|
||||||
if(error) { cerr << "found a node that is not an number?" << endl; break;}
|
|
||||||
container.push_back(x);
|
container.push_back(x);
|
||||||
}
|
}
|
||||||
benchmark::DoNotOptimize(container.data());
|
benchmark::DoNotOptimize(container.data());
|
||||||
|
@ -154,8 +146,7 @@ static void numbers_load_size_scan(State& state) {
|
||||||
simdjson::error_code error;
|
simdjson::error_code error;
|
||||||
for (UNUSED auto _ : state) {
|
for (UNUSED auto _ : state) {
|
||||||
// this may hit the disk, but probably just once
|
// this may hit the disk, but probably just once
|
||||||
parser.load(NUMBERS_JSON).get<dom::array>().tie(arr, error);
|
if(!parser.load(NUMBERS_JSON).get(arr, error)) {
|
||||||
if(error) {
|
|
||||||
cerr << "could not read " << NUMBERS_JSON << " as an array" << endl;
|
cerr << "could not read " << NUMBERS_JSON << " as an array" << endl;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -164,8 +155,7 @@ static void numbers_load_size_scan(State& state) {
|
||||||
size_t pos = 0;
|
size_t pos = 0;
|
||||||
for (auto e : arr) {
|
for (auto e : arr) {
|
||||||
double x;
|
double x;
|
||||||
e.get<double>().tie(x,error);
|
if(!e.get(x,error)) { cerr << "found a node that is not an number?" << endl; break;}
|
||||||
if(error) { cerr << "found a node that is not an number?" << endl; break;}
|
|
||||||
container[pos++] = x;
|
container[pos++] = x;
|
||||||
}
|
}
|
||||||
if(pos != container.size()) { cerr << "bad count" << endl; }
|
if(pos != container.size()) { cerr << "bad count" << endl; }
|
||||||
|
@ -328,7 +318,7 @@ static void twitter_default_profile(State& state) {
|
||||||
dom::element doc = parser.load(TWITTER_JSON);
|
dom::element doc = parser.load(TWITTER_JSON);
|
||||||
for (UNUSED auto _ : state) {
|
for (UNUSED auto _ : state) {
|
||||||
set<string_view> default_users;
|
set<string_view> default_users;
|
||||||
for (dom::object tweet : doc["statuses"].get<dom::array>()) {
|
for (dom::object tweet : doc["statuses"]) {
|
||||||
dom::object user = tweet["user"];
|
dom::object user = tweet["user"];
|
||||||
if (user["default_profile"]) {
|
if (user["default_profile"]) {
|
||||||
default_users.insert(user["screen_name"]);
|
default_users.insert(user["screen_name"]);
|
||||||
|
@ -343,12 +333,13 @@ static void twitter_image_sizes(State& state) {
|
||||||
// Count unique image sizes
|
// Count unique image sizes
|
||||||
dom::parser parser;
|
dom::parser parser;
|
||||||
dom::element doc = parser.load(TWITTER_JSON);
|
dom::element doc = parser.load(TWITTER_JSON);
|
||||||
|
simdjson::error_code error;
|
||||||
for (UNUSED auto _ : state) {
|
for (UNUSED auto _ : state) {
|
||||||
set<tuple<uint64_t, uint64_t>> image_sizes;
|
set<tuple<uint64_t, uint64_t>> image_sizes;
|
||||||
for (dom::object tweet : doc["statuses"].get<dom::array>()) {
|
for (dom::object tweet : doc["statuses"]) {
|
||||||
auto [media, not_found] = tweet["entities"]["media"];
|
dom::array media;
|
||||||
if (!not_found) {
|
if (tweet["entities"]["media"].get(media, error)) {
|
||||||
for (dom::object image : media.get<dom::array>()) {
|
for (dom::object image : media) {
|
||||||
for (auto size : image["sizes"].get<dom::object>()) {
|
for (auto size : image["sizes"].get<dom::object>()) {
|
||||||
image_sizes.insert({ size.value["w"], size.value["h"] });
|
image_sizes.insert({ size.value["w"], size.value["h"] });
|
||||||
}
|
}
|
||||||
|
@ -365,11 +356,12 @@ BENCHMARK(twitter_image_sizes);
|
||||||
static void error_code_twitter_count(State& state) noexcept {
|
static void error_code_twitter_count(State& state) noexcept {
|
||||||
// Prints the number of results in twitter.json
|
// Prints the number of results in twitter.json
|
||||||
dom::parser parser;
|
dom::parser parser;
|
||||||
auto [doc, error1] = parser.load(TWITTER_JSON);
|
simdjson::error_code error;
|
||||||
if (error1) { return; }
|
dom::element doc;
|
||||||
|
if (!parser.load(TWITTER_JSON).get(doc, error)) { return; }
|
||||||
for (UNUSED auto _ : state) {
|
for (UNUSED auto _ : state) {
|
||||||
auto [value, error] = doc["search_metadata"]["count"].get<uint64_t>();
|
uint64_t value;
|
||||||
if (error) { return; }
|
if (!doc["search_metadata"]["count"].get(value, error)) { return; }
|
||||||
if (value != 100) { return; }
|
if (value != 100) { return; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -378,21 +370,22 @@ BENCHMARK(error_code_twitter_count);
|
||||||
static void error_code_twitter_default_profile(State& state) noexcept {
|
static void error_code_twitter_default_profile(State& state) noexcept {
|
||||||
// Count unique users with a default profile.
|
// Count unique users with a default profile.
|
||||||
dom::parser parser;
|
dom::parser parser;
|
||||||
auto [doc, error1] = parser.load(TWITTER_JSON);
|
simdjson::error_code error;
|
||||||
if (error1) { std::cerr << error1 << std::endl; return; }
|
dom::element doc;
|
||||||
|
if (!parser.load(TWITTER_JSON).get(doc, error)) { std::cerr << error << std::endl; return; }
|
||||||
for (UNUSED auto _ : state) {
|
for (UNUSED auto _ : state) {
|
||||||
set<string_view> default_users;
|
set<string_view> default_users;
|
||||||
|
|
||||||
auto [tweets, error] = doc["statuses"].get<dom::array>();
|
dom::array tweets;
|
||||||
if (error) { return; }
|
if (!doc["statuses"].get(tweets, error)) { return; }
|
||||||
for (dom::element tweet : tweets) {
|
for (dom::element tweet : tweets) {
|
||||||
auto [user, error2] = tweet["user"].get<dom::object>();
|
dom::object user;
|
||||||
if (error2) { return; }
|
if (!tweet["user"].get(user, error)) { return; }
|
||||||
auto [default_profile, error3] = user["default_profile"].get<bool>();
|
bool default_profile;
|
||||||
if (error3) { return; }
|
if (!user["default_profile"].get(default_profile, error)) { return; }
|
||||||
if (default_profile) {
|
if (default_profile) {
|
||||||
auto [screen_name, error4] = user["screen_name"].get<std::string_view>();
|
std::string_view screen_name;
|
||||||
if (error4) { return; }
|
if (!user["screen_name"].get(screen_name, error)) { return; }
|
||||||
default_users.insert(screen_name);
|
default_users.insert(screen_name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -406,8 +399,9 @@ SIMDJSON_PUSH_DISABLE_WARNINGS
|
||||||
SIMDJSON_DISABLE_DEPRECATED_WARNING
|
SIMDJSON_DISABLE_DEPRECATED_WARNING
|
||||||
static void iterator_twitter_default_profile(State& state) {
|
static void iterator_twitter_default_profile(State& state) {
|
||||||
// Count unique users with a default profile.
|
// Count unique users with a default profile.
|
||||||
auto [json, error1] = padded_string::load(TWITTER_JSON);
|
simdjson::padded_string json;
|
||||||
if (error1) { std::cerr << error1 << std::endl; return; }
|
simdjson::error_code error;
|
||||||
|
if (!padded_string::load(TWITTER_JSON).tie(json, error)) { std::cerr << error << std::endl; return; }
|
||||||
ParsedJson pj = build_parsed_json(json);
|
ParsedJson pj = build_parsed_json(json);
|
||||||
for (UNUSED auto _ : state) {
|
for (UNUSED auto _ : state) {
|
||||||
set<string_view> default_users;
|
set<string_view> default_users;
|
||||||
|
@ -447,22 +441,22 @@ BENCHMARK(iterator_twitter_default_profile);
|
||||||
static void error_code_twitter_image_sizes(State& state) noexcept {
|
static void error_code_twitter_image_sizes(State& state) noexcept {
|
||||||
// Count unique image sizes
|
// Count unique image sizes
|
||||||
dom::parser parser;
|
dom::parser parser;
|
||||||
auto [doc, error1] = parser.load(TWITTER_JSON);
|
simdjson::error_code error;
|
||||||
if (error1) { std::cerr << error1 << std::endl; return; }
|
dom::element doc;
|
||||||
|
if (!parser.load(TWITTER_JSON).get(doc, error)) { std::cerr << error << std::endl; return; }
|
||||||
for (UNUSED auto _ : state) {
|
for (UNUSED auto _ : state) {
|
||||||
set<tuple<uint64_t, uint64_t>> image_sizes;
|
set<tuple<uint64_t, uint64_t>> image_sizes;
|
||||||
auto [statuses, error] = doc["statuses"].get<dom::array>();
|
dom::array statuses;
|
||||||
if (error) { return; }
|
if (!doc["statuses"].get(statuses, error)) { return; }
|
||||||
for (dom::element tweet : statuses) {
|
for (dom::element tweet : statuses) {
|
||||||
auto [images, not_found] = tweet["entities"]["media"].get<dom::array>();
|
dom::array images;
|
||||||
if (!not_found) {
|
if (tweet["entities"]["media"].get(images, error)) {
|
||||||
for (dom::element image : images) {
|
for (dom::element image : images) {
|
||||||
auto [sizes, error2] = image["sizes"].get<dom::object>();
|
dom::object sizes;
|
||||||
if (error2) { return; }
|
if (!image["sizes"].get(sizes, error)) { return; }
|
||||||
for (auto size : sizes) {
|
for (auto size : sizes) {
|
||||||
auto [width, error3] = size.value["w"].get<uint64_t>();
|
uint64_t width, height;
|
||||||
auto [height, error4] = size.value["h"].get<uint64_t>();
|
if (!size.value["w"].get(width, error) || !size.value["h"].get(height, error)) { return; }
|
||||||
if (error3 || error4) { return; }
|
|
||||||
image_sizes.insert({ width, height });
|
image_sizes.insert({ width, height });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -477,8 +471,9 @@ SIMDJSON_PUSH_DISABLE_WARNINGS
|
||||||
SIMDJSON_DISABLE_DEPRECATED_WARNING
|
SIMDJSON_DISABLE_DEPRECATED_WARNING
|
||||||
static void iterator_twitter_image_sizes(State& state) {
|
static void iterator_twitter_image_sizes(State& state) {
|
||||||
// Count unique image sizes
|
// Count unique image sizes
|
||||||
auto [json, error1] = padded_string::load(TWITTER_JSON);
|
simdjson::error_code error;
|
||||||
if (error1) { std::cerr << error1 << std::endl; return; }
|
padded_string json;
|
||||||
|
if (!padded_string::load(TWITTER_JSON).tie(json, error)) { std::cerr << error << std::endl; return; }
|
||||||
ParsedJson pj = build_parsed_json(json);
|
ParsedJson pj = build_parsed_json(json);
|
||||||
for (UNUSED auto _ : state) {
|
for (UNUSED auto _ : state) {
|
||||||
set<tuple<uint64_t, uint64_t>> image_sizes;
|
set<tuple<uint64_t, uint64_t>> image_sizes;
|
||||||
|
@ -536,10 +531,12 @@ BENCHMARK(iterator_twitter_image_sizes);
|
||||||
|
|
||||||
static void print_json(State& state) noexcept {
|
static void print_json(State& state) noexcept {
|
||||||
// Prints the number of results in twitter.json
|
// Prints the number of results in twitter.json
|
||||||
auto [json, error1] = padded_string::load(TWITTER_JSON);
|
simdjson::error_code error;
|
||||||
if (error1) { std::cerr << error1 << std::endl; return; }
|
padded_string json;
|
||||||
|
if (!padded_string::load(TWITTER_JSON).tie(json, error)) { std::cerr << error << std::endl; return; }
|
||||||
dom::parser parser;
|
dom::parser parser;
|
||||||
if (int error = json_parse(json, parser); error != SUCCESS) { cerr << error_message(error) << endl; return; }
|
int code = json_parse(json, parser);
|
||||||
|
if (code) { cerr << error_message(code) << endl; return; }
|
||||||
for (UNUSED auto _ : state) {
|
for (UNUSED auto _ : state) {
|
||||||
std::stringstream s;
|
std::stringstream s;
|
||||||
if (!parser.print_json(s)) { cerr << "print_json failed" << endl; return; }
|
if (!parser.print_json(s)) { cerr << "print_json failed" << endl; return; }
|
||||||
|
|
|
@ -50,7 +50,7 @@ void print_vec(const std::vector<int64_t> &v) {
|
||||||
} else if (element.is<simdjson::dom::object>()) {
|
} else if (element.is<simdjson::dom::object>()) {
|
||||||
auto [object, error] = element.get<simdjson::dom::object>();
|
auto [object, error] = element.get<simdjson::dom::object>();
|
||||||
int64_t id;
|
int64_t id;
|
||||||
object["user"]["id"].get<int64_t>().tie(id,error);
|
object["user"]["id"].get(id,error);
|
||||||
if(!error) {
|
if(!error) {
|
||||||
v.push_back(id);
|
v.push_back(id);
|
||||||
}
|
}
|
||||||
|
@ -64,46 +64,46 @@ void print_vec(const std::vector<int64_t> &v) {
|
||||||
// clang-format on
|
// clang-format on
|
||||||
|
|
||||||
|
|
||||||
void simdjson_recurse(std::vector<int64_t> & v, simdjson::dom::element element) {
|
really_inline void simdjson_recurse(std::vector<int64_t> & v, simdjson::dom::element element);
|
||||||
if (element.is<simdjson::dom::array>()) {
|
void simdjson_recurse(std::vector<int64_t> & v, simdjson::dom::array array) {
|
||||||
simdjson::dom::array array;
|
|
||||||
simdjson::error_code error;
|
|
||||||
element.get<simdjson::dom::array>().tie(array, error);
|
|
||||||
for (auto child : array) {
|
for (auto child : array) {
|
||||||
if (child.is<simdjson::dom::array>() || child.is<simdjson::dom::object>()) {
|
|
||||||
simdjson_recurse(v, child);
|
simdjson_recurse(v, child);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (element.is<simdjson::dom::object>()) {
|
void simdjson_recurse(std::vector<int64_t> & v, simdjson::dom::object object) {
|
||||||
simdjson::dom::object object;
|
|
||||||
simdjson::error_code error;
|
|
||||||
element.get<simdjson::dom::object>().tie(object, error);
|
|
||||||
for (auto [key, value] : object) {
|
for (auto [key, value] : object) {
|
||||||
if((key.size() == 4) && (memcmp(key.data(), "user", 4) == 0)) {
|
if((key.size() == 4) && (memcmp(key.data(), "user", 4) == 0)) {
|
||||||
// we are in an object under the key "user"
|
// we are in an object under the key "user"
|
||||||
if(value.is<simdjson::dom::object>()) {
|
simdjson::error_code error;
|
||||||
simdjson::dom::object child_object;
|
simdjson::dom::object child_object;
|
||||||
value.get<simdjson::dom::object>().tie(child_object, error);
|
simdjson::dom::object child_array;
|
||||||
|
if (value.get(child_object, error)) {
|
||||||
for (auto [child_key, child_value] : child_object) {
|
for (auto [child_key, child_value] : child_object) {
|
||||||
if((child_key.size() == 2) && (memcmp(child_key.data(), "id", 2) == 0)) {
|
if((child_key.size() == 2) && (memcmp(child_key.data(), "id", 2) == 0)) {
|
||||||
if(child_value.is<int64_t>()) {
|
|
||||||
int64_t x;
|
int64_t x;
|
||||||
child_value.get<int64_t>().tie(x, error);
|
if (child_value.get(x, error)) {
|
||||||
v.push_back(x);
|
v.push_back(x);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (child_value.is<simdjson::dom::array>() || child_value.is<simdjson::dom::object>()) {
|
|
||||||
simdjson_recurse(v, child_value);
|
simdjson_recurse(v, child_value);
|
||||||
}
|
}
|
||||||
}
|
} else if (value.get(child_array, error)) {
|
||||||
} else if (value.is<simdjson::dom::array>()) {
|
simdjson_recurse(v, child_array);
|
||||||
simdjson_recurse(v, value);
|
|
||||||
}
|
}
|
||||||
// end of: we are in an object under the key "user"
|
// end of: we are in an object under the key "user"
|
||||||
} else if (value.is<simdjson::dom::array>() || value.is<simdjson::dom::object>()) {
|
} else {
|
||||||
simdjson_recurse(v, value);
|
simdjson_recurse(v, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
really_inline void simdjson_recurse(std::vector<int64_t> & v, simdjson::dom::element element) {
|
||||||
|
UNUSED simdjson::error_code error;
|
||||||
|
simdjson::dom::array array;
|
||||||
|
simdjson::dom::object object;
|
||||||
|
if (element.get(array, error)) {
|
||||||
|
simdjson_recurse(v, array);
|
||||||
|
} else if (element.get(object, error)) {
|
||||||
|
simdjson_recurse(v, object);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,8 +121,7 @@ simdjson_compute_stats(const simdjson::padded_string &p) {
|
||||||
simdjson::dom::parser parser;
|
simdjson::dom::parser parser;
|
||||||
simdjson::dom::element doc;
|
simdjson::dom::element doc;
|
||||||
simdjson::error_code error;
|
simdjson::error_code error;
|
||||||
parser.parse(p).tie(doc, error);
|
if (parser.parse(p).tie(doc, error)) {
|
||||||
if(!error) {
|
|
||||||
simdjson_recurse(answer, doc);
|
simdjson_recurse(answer, doc);
|
||||||
remove_duplicates(answer);
|
remove_duplicates(answer);
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,10 +53,9 @@ really_inline void simdjson_process_atom(stat_t &s,
|
||||||
if (element.is<double>()) {
|
if (element.is<double>()) {
|
||||||
s.number_count++;
|
s.number_count++;
|
||||||
} else if (element.is<bool>()) {
|
} else if (element.is<bool>()) {
|
||||||
simdjson::error_code err;
|
simdjson::error_code error;
|
||||||
bool v;
|
bool v;
|
||||||
element.get<bool>().tie(v, err);
|
if (element.get(v, error) && v) {
|
||||||
if (v) {
|
|
||||||
s.true_count++;
|
s.true_count++;
|
||||||
} else {
|
} else {
|
||||||
s.false_count++;
|
s.false_count++;
|
||||||
|
@ -67,11 +66,12 @@ really_inline void simdjson_process_atom(stat_t &s,
|
||||||
}
|
}
|
||||||
|
|
||||||
void simdjson_recurse(stat_t &s, simdjson::dom::element element) {
|
void simdjson_recurse(stat_t &s, simdjson::dom::element element) {
|
||||||
|
error_code error;
|
||||||
if (element.is<simdjson::dom::array>()) {
|
if (element.is<simdjson::dom::array>()) {
|
||||||
s.array_count++;
|
s.array_count++;
|
||||||
auto [array, array_error] = element.get<simdjson::dom::array>();
|
dom::array array;
|
||||||
if (array_error) {
|
if (!element.get(array, error)) {
|
||||||
std::cerr << array_error << std::endl;
|
std::cerr << error << std::endl;
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
for (auto child : array) {
|
for (auto child : array) {
|
||||||
|
@ -84,9 +84,9 @@ void simdjson_recurse(stat_t &s, simdjson::dom::element element) {
|
||||||
}
|
}
|
||||||
} else if (element.is<simdjson::dom::object>()) {
|
} else if (element.is<simdjson::dom::object>()) {
|
||||||
s.object_count++;
|
s.object_count++;
|
||||||
auto [object, object_error] = element.get<simdjson::dom::object>();
|
dom::object object;
|
||||||
if (object_error) {
|
if (!element.get(object, error)) {
|
||||||
std::cerr << object_error << std::endl;
|
std::cerr << error << std::endl;
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
for (auto field : object) {
|
for (auto field : object) {
|
||||||
|
|
|
@ -64,10 +64,11 @@ really_inline void simdjson_process_atom(stat_t &s,
|
||||||
}
|
}
|
||||||
|
|
||||||
void simdjson_recurse(stat_t &s, simdjson::dom::element element) {
|
void simdjson_recurse(stat_t &s, simdjson::dom::element element) {
|
||||||
|
simdjson::error_code error;
|
||||||
if (element.is<simdjson::dom::array>()) {
|
if (element.is<simdjson::dom::array>()) {
|
||||||
s.array_count++;
|
s.array_count++;
|
||||||
auto [array, array_error] = element.get<simdjson::dom::array>();
|
simdjson::dom::array array;
|
||||||
if (array_error) { std::cerr << array_error << std::endl; abort(); }
|
if (!element.get(array, error)) { std::cerr << error << std::endl; abort(); }
|
||||||
for (auto child : array) {
|
for (auto child : array) {
|
||||||
if (child.is<simdjson::dom::array>() || child.is<simdjson::dom::object>()) {
|
if (child.is<simdjson::dom::array>() || child.is<simdjson::dom::object>()) {
|
||||||
simdjson_recurse(s, child);
|
simdjson_recurse(s, child);
|
||||||
|
@ -77,8 +78,8 @@ void simdjson_recurse(stat_t &s, simdjson::dom::element element) {
|
||||||
}
|
}
|
||||||
} else if (element.is<simdjson::dom::object>()) {
|
} else if (element.is<simdjson::dom::object>()) {
|
||||||
s.object_count++;
|
s.object_count++;
|
||||||
auto [object, object_error] = element.get<simdjson::dom::object>();
|
simdjson::dom::object object;
|
||||||
if (object_error) { std::cerr << object_error << std::endl; abort(); }
|
if (!element.get(object, error)) { std::cerr << error << std::endl; abort(); }
|
||||||
for (auto field : object) {
|
for (auto field : object) {
|
||||||
s.string_count++; // for key
|
s.string_count++; // for key
|
||||||
if (field.value.is<simdjson::dom::array>() || field.value.is<simdjson::dom::object>()) {
|
if (field.value.is<simdjson::dom::array>() || field.value.is<simdjson::dom::object>()) {
|
||||||
|
|
|
@ -81,9 +81,9 @@ Once you have an element, you can navigate it with idiomatic C++ iterators, oper
|
||||||
given type, or use the `type()` method: e.g., `element.type() == dom::element_type::DOUBLE`. Instead of casting, you can use get<*typename*>() to get the value: casts and get<*typename*>() can be used interchangeably. You can use a variant usage of get<*typename*>() with error codes to avoid exceptions: e.g.,
|
given type, or use the `type()` method: e.g., `element.type() == dom::element_type::DOUBLE`. Instead of casting, you can use get<*typename*>() to get the value: casts and get<*typename*>() can be used interchangeably. You can use a variant usage of get<*typename*>() with error codes to avoid exceptions: e.g.,
|
||||||
```c++
|
```c++
|
||||||
simdjson::error_code error;
|
simdjson::error_code error;
|
||||||
double value; // variable where we store the value to be parsed
|
|
||||||
simdjson::padded_string numberstring = "1.2"_padded; // our JSON input ("1.2")
|
simdjson::padded_string numberstring = "1.2"_padded; // our JSON input ("1.2")
|
||||||
simdjson::dom::parser parser;
|
simdjson::dom::parser parser;
|
||||||
|
double value; // variable where we store the value to be parsed
|
||||||
parser.parse(numberstring).get(value,error);
|
parser.parse(numberstring).get(value,error);
|
||||||
if (error) { std::cerr << error << std::endl; return EXIT_FAILURE; }
|
if (error) { std::cerr << error << std::endl; return EXIT_FAILURE; }
|
||||||
std::cout << "I parsed " << value << " from " << numberstring.data() << std::endl;
|
std::cout << "I parsed " << value << " from " << numberstring.data() << std::endl;
|
||||||
|
@ -211,9 +211,10 @@ For comparison, here is the C++ 11 version of the same code:
|
||||||
// C++ 11 version for comparison
|
// C++ 11 version for comparison
|
||||||
dom::parser parser;
|
dom::parser parser;
|
||||||
padded_string json = R"( { "foo": 1, "bar": 2 } )"_padded;
|
padded_string json = R"( { "foo": 1, "bar": 2 } )"_padded;
|
||||||
dom::object object;
|
|
||||||
simdjson::error_code error;
|
simdjson::error_code error;
|
||||||
|
dom::object object;
|
||||||
parser.parse(json).get(object, error);
|
parser.parse(json).get(object, error);
|
||||||
|
if (!error) { cerr << error << endl; return; }
|
||||||
for (dom::key_value_pair field : object) {
|
for (dom::key_value_pair field : object) {
|
||||||
cout << field.key << " = " << field.value << endl;
|
cout << field.key << " = " << field.value << endl;
|
||||||
}
|
}
|
||||||
|
@ -299,45 +300,39 @@ auto cars_json = R"( [
|
||||||
dom::parser parser;
|
dom::parser parser;
|
||||||
dom::array cars;
|
dom::array cars;
|
||||||
simdjson::error_code error;
|
simdjson::error_code error;
|
||||||
parser.parse(cars_json).get(cars, error);
|
if (!parser.parse(cars_json).get(cars, error)) { cerr << error << endl; exit(1); }
|
||||||
if (error) { cerr << error << endl; exit(1); }
|
|
||||||
|
|
||||||
// Iterating through an array of objects
|
// Iterating through an array of objects
|
||||||
for (dom::element car_element : cars) {
|
for (dom::element car_element : cars) {
|
||||||
dom::object car;
|
dom::object car;
|
||||||
car_element.get(car, error);
|
if (!car_element.get(car, error)) { cerr << error << endl; exit(1); }
|
||||||
if (error) { cerr << error << endl; exit(1); }
|
|
||||||
|
|
||||||
// Accessing a field by name
|
// Accessing a field by name
|
||||||
dom::element make, model;
|
std::string_view make, model;
|
||||||
car["make"].tie(make, error);
|
if (!car["make"].get(make, error)) { cerr << error << endl; exit(1); }
|
||||||
if (error) { cerr << error << endl; exit(1); }
|
if (!car["model"].get(model, error)) { cerr << error << endl; exit(1); }
|
||||||
car["model"].tie(model, error);
|
cout << "Make/Model: " << make << "/" << model << endl;
|
||||||
if (error) { cerr << error << endl; exit(1); }
|
|
||||||
cout << "Make/Model: " << make << "/" << model << endl;
|
|
||||||
|
|
||||||
// Casting a JSON element to an integer
|
// Casting a JSON element to an integer
|
||||||
uint64_t year;
|
uint64_t year;
|
||||||
car["year"].get(year, error);
|
if (!car["year"].get(year, error)) { cerr << error << endl; exit(1); }
|
||||||
if (error) { cerr << error << endl; exit(1); }
|
cout << "- This car is " << 2020 - year << "years old." << endl;
|
||||||
cout << "- This car is " << 2020 - year << "years old." << endl;
|
|
||||||
|
|
||||||
// Iterating through an array of floats
|
// Iterating through an array of floats
|
||||||
double total_tire_pressure = 0;
|
double total_tire_pressure = 0;
|
||||||
dom::array tire_pressure_array;
|
dom::array tire_pressure_array;
|
||||||
car["tire_pressure"].get(tire_pressure_array, error);
|
if (!car["tire_pressure"].get(tire_pressure_array, error)) { cerr << error << endl; exit(1); }
|
||||||
if (error) { cerr << error << endl; exit(1); }
|
for (dom::element tire_pressure_element : tire_pressure_array) {
|
||||||
for (dom::element tire_pressure_element : tire_pressure_array) {
|
|
||||||
double tire_pressure;
|
double tire_pressure;
|
||||||
tire_pressure_element.get(tire_pressure, error);
|
if (!tire_pressure_element.get(tire_pressure, error)) { cerr << error << endl; exit(1); }
|
||||||
if (error) { cerr << error << endl; exit(1); }
|
|
||||||
total_tire_pressure += tire_pressure;
|
total_tire_pressure += tire_pressure;
|
||||||
}
|
}
|
||||||
cout << "- Average tire pressure: " << (total_tire_pressure / 4) << endl;
|
cout << "- Average tire pressure: " << (total_tire_pressure / 4) << endl;
|
||||||
|
|
||||||
// Writing out all the information about the car
|
// Writing out all the information about the car
|
||||||
for (auto field : car) {
|
for (auto field : car) {
|
||||||
cout << "- " << field.key << ": " << field.value << endl;
|
cout << "- " << field.key << ": " << field.value << endl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -356,32 +351,23 @@ if (error) { cerr << error << endl; exit(1); }
|
||||||
// Iterate through an array of objects
|
// Iterate through an array of objects
|
||||||
for (dom::element elem : rootarray) {
|
for (dom::element elem : rootarray) {
|
||||||
dom::object obj;
|
dom::object obj;
|
||||||
elem.get(obj, error);
|
if (!elem.get(obj, error)) { cerr << error << endl; exit(1); }
|
||||||
if (error) { cerr << error << endl; exit(1); }
|
for (auto & key_value : obj) {
|
||||||
for(auto & key_value : obj) {
|
|
||||||
cout << "key: " << key_value.key << " : ";
|
cout << "key: " << key_value.key << " : ";
|
||||||
dom::object innerobj;
|
dom::object innerobj;
|
||||||
key_value.value.get(innerobj, error);
|
if (!key_value.value.get(innerobj, error)) { cerr << error << endl; exit(1); }
|
||||||
if (error) { cerr << error << endl; exit(1); }
|
|
||||||
|
|
||||||
double va;
|
double va, vb;
|
||||||
innerobj["a"].get(va, error);
|
if (!innerobj["a"].get(va, error)) { cerr << error << endl; exit(1); }
|
||||||
if (error) { cerr << error << endl; exit(1); }
|
|
||||||
cout << "a: " << va << ", ";
|
cout << "a: " << va << ", ";
|
||||||
|
if (!innerobj["b"].get(vc, error)) { cerr << error << endl; exit(1); }
|
||||||
double vb;
|
|
||||||
innerobj["b"].get(vb, error);
|
|
||||||
if (error) { cerr << error << endl; exit(1); }
|
|
||||||
cout << "b: " << vb << ", ";
|
cout << "b: " << vb << ", ";
|
||||||
|
|
||||||
int64_t vc;
|
int64_t vc;
|
||||||
innerobj["c"].get(vc, error);
|
if (!innerobj["c"].get(vc, error)) { cerr << error << endl; exit(1); }
|
||||||
if (error) { cerr << error << endl; exit(1); }
|
|
||||||
cout << "c: " << vc << endl;
|
cout << "c: " << vc << endl;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
And another one:
|
And another one:
|
||||||
|
@ -390,8 +376,8 @@ And another one:
|
||||||
auto abstract_json = R"(
|
auto abstract_json = R"(
|
||||||
{ "str" : { "123" : {"abc" : 3.14 } } } )"_padded;
|
{ "str" : { "123" : {"abc" : 3.14 } } } )"_padded;
|
||||||
dom::parser parser;
|
dom::parser parser;
|
||||||
double v;
|
|
||||||
simdjson::error_code error;
|
simdjson::error_code error;
|
||||||
|
double v;
|
||||||
parser.parse(abstract_json)["str"]["123"]["abc"].get(v, error);
|
parser.parse(abstract_json)["str"]["123"]["abc"].get(v, error);
|
||||||
if (error) { cerr << error << endl; exit(1); }
|
if (error) { cerr << error << endl; exit(1); }
|
||||||
cout << "number: " << v << endl;
|
cout << "number: " << v << endl;
|
||||||
|
@ -409,8 +395,7 @@ bool parse_double(const char *j, double &d) {
|
||||||
simdjson::error_code error;
|
simdjson::error_code error;
|
||||||
parser.parse(j, std::strlen(j))
|
parser.parse(j, std::strlen(j))
|
||||||
.at(0)
|
.at(0)
|
||||||
.get<double>()
|
.get(d, error);
|
||||||
.tie(d, error);
|
|
||||||
if (error) { return false; }
|
if (error) { return false; }
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -420,8 +405,7 @@ bool parse_string(const char *j, std::string &s) {
|
||||||
std::string_view answer;
|
std::string_view answer;
|
||||||
parser.parse(j,strlen(j))
|
parser.parse(j,strlen(j))
|
||||||
.at(0)
|
.at(0)
|
||||||
.get<std::string_view>()
|
.get(answer, error);
|
||||||
.tie(answer, error);
|
|
||||||
if (error) { return false; }
|
if (error) { return false; }
|
||||||
s.assign(answer.data(), answer.size());
|
s.assign(answer.data(), answer.size());
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -3,9 +3,9 @@
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
simdjson::error_code error;
|
simdjson::error_code error;
|
||||||
double value; // variable where we store the value to be parsed
|
|
||||||
simdjson::padded_string numberstring = "1.2"_padded; // our JSON input ("1.2")
|
simdjson::padded_string numberstring = "1.2"_padded; // our JSON input ("1.2")
|
||||||
simdjson::dom::parser parser;
|
simdjson::dom::parser parser;
|
||||||
|
double value; // variable where we store the value to be parsed
|
||||||
parser.parse(numberstring).get(value,error);
|
parser.parse(numberstring).get(value,error);
|
||||||
if (error) { std::cerr << error << std::endl; return EXIT_FAILURE; }
|
if (error) { std::cerr << error << std::endl; return EXIT_FAILURE; }
|
||||||
std::cout << "I parsed " << value << " from " << numberstring.data() << std::endl;
|
std::cout << "I parsed " << value << " from " << numberstring.data() << std::endl;
|
||||||
|
|
|
@ -155,9 +155,10 @@ void basics_cpp17_2() {
|
||||||
// C++ 11 version for comparison
|
// C++ 11 version for comparison
|
||||||
dom::parser parser;
|
dom::parser parser;
|
||||||
padded_string json = R"( { "foo": 1, "bar": 2 } )"_padded;
|
padded_string json = R"( { "foo": 1, "bar": 2 } )"_padded;
|
||||||
dom::object object;
|
|
||||||
simdjson::error_code error;
|
simdjson::error_code error;
|
||||||
|
dom::object object;
|
||||||
parser.parse(json).get(object, error);
|
parser.parse(json).get(object, error);
|
||||||
|
if (!error) { cerr << error << endl; return; }
|
||||||
for (dom::key_value_pair field : object) {
|
for (dom::key_value_pair field : object) {
|
||||||
cout << field.key << " = " << field.value << endl;
|
cout << field.key << " = " << field.value << endl;
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,38 +35,31 @@ void basics_error_3() {
|
||||||
dom::parser parser;
|
dom::parser parser;
|
||||||
dom::array cars;
|
dom::array cars;
|
||||||
simdjson::error_code error;
|
simdjson::error_code error;
|
||||||
parser.parse(cars_json).get(cars, error);
|
if (!parser.parse(cars_json).get(cars, error)) { cerr << error << endl; exit(1); }
|
||||||
if (error) { cerr << error << endl; exit(1); }
|
|
||||||
|
|
||||||
// Iterating through an array of objects
|
// Iterating through an array of objects
|
||||||
for (dom::element car_element : cars) {
|
for (dom::element car_element : cars) {
|
||||||
dom::object car;
|
dom::object car;
|
||||||
car_element.get(car, error);
|
if (!car_element.get(car, error)) { cerr << error << endl; exit(1); }
|
||||||
if (error) { cerr << error << endl; exit(1); }
|
|
||||||
|
|
||||||
// Accessing a field by name
|
// Accessing a field by name
|
||||||
dom::element make, model;
|
std::string_view make, model;
|
||||||
car["make"].tie(make, error);
|
if (!car["make"].get(make, error)) { cerr << error << endl; exit(1); }
|
||||||
if (error) { cerr << error << endl; exit(1); }
|
if (!car["model"].get(model, error)) { cerr << error << endl; exit(1); }
|
||||||
car["model"].tie(model, error);
|
|
||||||
if (error) { cerr << error << endl; exit(1); }
|
|
||||||
cout << "Make/Model: " << make << "/" << model << endl;
|
cout << "Make/Model: " << make << "/" << model << endl;
|
||||||
|
|
||||||
// Casting a JSON element to an integer
|
// Casting a JSON element to an integer
|
||||||
uint64_t year;
|
uint64_t year;
|
||||||
car["year"].get(year, error);
|
if (!car["year"].get(year, error)) { cerr << error << endl; exit(1); }
|
||||||
if (error) { cerr << error << endl; exit(1); }
|
|
||||||
cout << "- This car is " << 2020 - year << "years old." << endl;
|
cout << "- This car is " << 2020 - year << "years old." << endl;
|
||||||
|
|
||||||
// Iterating through an array of floats
|
// Iterating through an array of floats
|
||||||
double total_tire_pressure = 0;
|
double total_tire_pressure = 0;
|
||||||
dom::array tire_pressure_array;
|
dom::array tire_pressure_array;
|
||||||
car["tire_pressure"].get(tire_pressure_array, error);
|
if (!car["tire_pressure"].get(tire_pressure_array, error)) { cerr << error << endl; exit(1); }
|
||||||
if (error) { cerr << error << endl; exit(1); }
|
|
||||||
for (dom::element tire_pressure_element : tire_pressure_array) {
|
for (dom::element tire_pressure_element : tire_pressure_array) {
|
||||||
double tire_pressure;
|
double tire_pressure;
|
||||||
tire_pressure_element.get(tire_pressure, error);
|
if (!tire_pressure_element.get(tire_pressure, error)) { cerr << error << endl; exit(1); }
|
||||||
if (error) { cerr << error << endl; exit(1); }
|
|
||||||
total_tire_pressure += tire_pressure;
|
total_tire_pressure += tire_pressure;
|
||||||
}
|
}
|
||||||
cout << "- Average tire pressure: " << (total_tire_pressure / 4) << endl;
|
cout << "- Average tire pressure: " << (total_tire_pressure / 4) << endl;
|
||||||
|
@ -92,29 +85,21 @@ void basics_error_4() {
|
||||||
// Iterate through an array of objects
|
// Iterate through an array of objects
|
||||||
for (dom::element elem : rootarray) {
|
for (dom::element elem : rootarray) {
|
||||||
dom::object obj;
|
dom::object obj;
|
||||||
elem.get(obj, error);
|
if (!elem.get(obj, error)) { cerr << error << endl; exit(1); }
|
||||||
if (error) { cerr << error << endl; exit(1); }
|
for (auto & key_value : obj) {
|
||||||
for(auto & key_value : obj) {
|
|
||||||
cout << "key: " << key_value.key << " : ";
|
cout << "key: " << key_value.key << " : ";
|
||||||
dom::object innerobj;
|
dom::object innerobj;
|
||||||
key_value.value.get(innerobj, error);
|
if (!key_value.value.get(innerobj, error)) { cerr << error << endl; exit(1); }
|
||||||
if (error) { cerr << error << endl; exit(1); }
|
|
||||||
|
|
||||||
double va;
|
double va, vb;
|
||||||
innerobj["a"].get(va, error);
|
if (!innerobj["a"].get(va, error)) { cerr << error << endl; exit(1); }
|
||||||
if (error) { cerr << error << endl; exit(1); }
|
|
||||||
cout << "a: " << va << ", ";
|
cout << "a: " << va << ", ";
|
||||||
|
if (!innerobj["b"].get(vb, error)) { cerr << error << endl; exit(1); }
|
||||||
double vb;
|
|
||||||
innerobj["b"].get(vb, error);
|
|
||||||
if (error) { cerr << error << endl; exit(1); }
|
|
||||||
cout << "b: " << vb << ", ";
|
cout << "b: " << vb << ", ";
|
||||||
|
|
||||||
int64_t vc;
|
int64_t vc;
|
||||||
innerobj["c"].get(vc, error);
|
if (!innerobj["c"].get(vc, error)) { cerr << error << endl; exit(1); }
|
||||||
if (error) { cerr << error << endl; exit(1); }
|
|
||||||
cout << "c: " << vc << endl;
|
cout << "c: " << vc << endl;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -185,27 +170,24 @@ void basics_error_3_cpp17() {
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
simdjson::dom::parser parser;
|
|
||||||
|
|
||||||
// See https://github.com/miloyip/nativejson-benchmark/blob/master/src/tests/simdjsontest.cpp
|
// See https://github.com/miloyip/nativejson-benchmark/blob/master/src/tests/simdjsontest.cpp
|
||||||
bool ParseDouble(const char *j, double &d) {
|
simdjson::dom::parser parser{};
|
||||||
|
|
||||||
|
bool parse_double(const char *j, double &d) {
|
||||||
simdjson::error_code error;
|
simdjson::error_code error;
|
||||||
parser.parse(j, std::strlen(j))
|
parser.parse(j, std::strlen(j))
|
||||||
.at(0)
|
.at(0)
|
||||||
.get<double>()
|
.get(d, error);
|
||||||
.tie(d, error);
|
|
||||||
if (error) { return false; }
|
if (error) { return false; }
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// See https://github.com/miloyip/nativejson-benchmark/blob/master/src/tests/simdjsontest.cpp
|
bool parse_string(const char *j, std::string &s) {
|
||||||
bool ParseString(const char *j, std::string &s) {
|
|
||||||
simdjson::error_code error;
|
simdjson::error_code error;
|
||||||
std::string_view answer;
|
std::string_view answer;
|
||||||
parser.parse(j,strlen(j))
|
parser.parse(j,strlen(j))
|
||||||
.at(0)
|
.at(0)
|
||||||
.get<std::string_view>()
|
.get(answer, error);
|
||||||
.tie(answer, error);
|
|
||||||
if (error) { return false; }
|
if (error) { return false; }
|
||||||
s.assign(answer.data(), answer.size());
|
s.assign(answer.data(), answer.size());
|
||||||
return true;
|
return true;
|
||||||
|
@ -214,15 +196,15 @@ bool ParseString(const char *j, std::string &s) {
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
double x{};
|
double x{};
|
||||||
ParseDouble("[1.1]",x);
|
parse_double("[1.1]",x);
|
||||||
if(x != 1.1) {
|
if(x != 1.1) {
|
||||||
std::cerr << "bug in ParseDouble!" << std::endl;
|
std::cerr << "bug in parse_double!" << std::endl;
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
std::string s{};
|
std::string s{};
|
||||||
ParseString("[\"my string\"]", s);
|
parse_string("[\"my string\"]", s);
|
||||||
if(s != "my string") {
|
if(s != "my string") {
|
||||||
std::cerr << "bug in ParseString!" << std::endl;
|
std::cerr << "bug in parse_string!" << std::endl;
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
basics_error_2();
|
basics_error_2();
|
||||||
|
|
|
@ -70,10 +70,11 @@ void recurse(simdjson::dom::element element, stat_t &s, size_t depth) {
|
||||||
if (depth > s.maximum_depth) {
|
if (depth > s.maximum_depth) {
|
||||||
s.maximum_depth = depth;
|
s.maximum_depth = depth;
|
||||||
}
|
}
|
||||||
|
simdjson::error_code error;
|
||||||
if (element.is<simdjson::dom::array>()) {
|
if (element.is<simdjson::dom::array>()) {
|
||||||
s.array_count++;
|
s.array_count++;
|
||||||
auto [array, array_error] = element.get<simdjson::dom::array>();
|
simdjson::dom::array array;
|
||||||
if (!array_error) {
|
if (element.get(array, error)) {
|
||||||
size_t counter = 0;
|
size_t counter = 0;
|
||||||
for (auto child : array) {
|
for (auto child : array) {
|
||||||
counter++;
|
counter++;
|
||||||
|
@ -85,8 +86,8 @@ void recurse(simdjson::dom::element element, stat_t &s, size_t depth) {
|
||||||
}
|
}
|
||||||
} else if (element.is<simdjson::dom::object>()) {
|
} else if (element.is<simdjson::dom::object>()) {
|
||||||
s.object_count++;
|
s.object_count++;
|
||||||
auto [object, object_error] = element.get<simdjson::dom::object>();
|
simdjson::dom::object object;
|
||||||
if (!object_error) {
|
if (element.get(object, error)) {
|
||||||
size_t counter = 0;
|
size_t counter = 0;
|
||||||
for (auto [key, value] : object) {
|
for (auto [key, value] : object) {
|
||||||
counter++;
|
counter++;
|
||||||
|
@ -116,7 +117,6 @@ void recurse(simdjson::dom::element element, stat_t &s, size_t depth) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
simdjson::error_code error;
|
|
||||||
if (element.is<int64_t>()) {
|
if (element.is<int64_t>()) {
|
||||||
s.integer_count++; // because an int can be sometimes represented as a double, we
|
s.integer_count++; // because an int can be sometimes represented as a double, we
|
||||||
// to check whether it is an integer first!!!
|
// to check whether it is an integer first!!!
|
||||||
|
|
Loading…
Reference in New Issue