If you are compiling with C++20 or later, you can integrate the simdjson On-Demand API with std::ranges and range adaptors (like std::views::transform) using helper functions. These wrappers are zero-cost and forward directly to the underlying On-Demand iterators without buffering values.
Array Iteration:
Use ondemand::get_range() on an ondemand::array. This produces a std::ranges::view that satisfies std::ranges::input_range.
Object Iteration:
Use ondemand::get_key_value_range() on an ondemand::object. This yields simdjson_result<ondemand::field> elements, allowing you to access both the key and the value.
Error Handling Compatibility:
These helpers work with both exception-based and non-exception-based code patterns.
#include "simdjson.h"
#include <ranges>
#include <string>
#include <vector>
auto json = R"([
{ "name": "Alice", "age": 30 },
{ "name": "Bob", "age": 25 },
{ "name": "Carol", "age": 35 }
])"_padded;
ondemand::parser parser;
auto doc = parser.iterate(json);
auto arr = doc.get_array();
// Use std::views::transform to extract names
auto names = ondemand::get_range(arr)
| std::views::transform([](auto elem) -> std::string {
return std::string(std::string_view(elem["name"]));
});
for (auto name : names) {
std::cout << name << std::endl; // Alice, Bob, Carol
}