From 3ff23e0271c6d7fbe74813a8f8081eeb6bc79cf1 Mon Sep 17 00:00:00 2001 From: nodejs-github-bot <18269663+nodejs-github-bot@users.noreply.github.com> Date: Sun, 2 Nov 2025 00:38:24 +0000 Subject: [PATCH] deps: update simdjson to 4.1.0 --- deps/simdjson/simdjson.cpp | 24 +- deps/simdjson/simdjson.h | 7777 +++++++++++++++++++++++++++++++++++- 2 files changed, 7692 insertions(+), 109 deletions(-) diff --git a/deps/simdjson/simdjson.cpp b/deps/simdjson/simdjson.cpp index 8a73ef4004c360..7ac177b062b36c 100644 --- a/deps/simdjson/simdjson.cpp +++ b/deps/simdjson/simdjson.cpp @@ -1,4 +1,4 @@ -/* auto-generated on 2025-09-29 20:34:35 -0700. version 4.0.7 Do not edit! */ +/* auto-generated on 2025-10-27 16:52:41 -0400. version 4.1.0 Do not edit! */ /* including simdjson.cpp: */ /* begin file simdjson.cpp */ #define SIMDJSON_SRC_SIMDJSON_CPP @@ -3030,6 +3030,25 @@ concept container_but_not_string = std::ranges::input_range && !string_like && !concepts::string_view_keyed_map; + +// Concept: Indexable container that is not a string or associative container +// Accepts: std::vector, std::array, std::deque (have operator[], value_type, not string_like) +// Rejects: std::string (string_like), std::list (no operator[]), std::map (has key_type) +template +concept indexable_container = requires { + typename Container::value_type; + requires !concepts::string_like; + requires !requires { typename Container::key_type; }; // Reject maps/sets + requires requires(Container& c, std::size_t i) { + { c[i] } -> std::convertible_to; + }; +}; + + +// Variable template to use with std::meta::substitute +template +constexpr bool indexable_container_v = indexable_container; + } // namespace concepts @@ -5047,7 +5066,8 @@ namespace internal { { INCOMPLETE_ARRAY_OR_OBJECT, "INCOMPLETE_ARRAY_OR_OBJECT: JSON document ended early in the middle of an object or array. This is a fatal and unrecoverable error." }, { SCALAR_DOCUMENT_AS_VALUE, "SCALAR_DOCUMENT_AS_VALUE: A JSON document made of a scalar (number, Boolean, null or string) is treated as a value. Use get_bool(), get_double(), etc. on the document instead. "}, { OUT_OF_BOUNDS, "OUT_OF_BOUNDS: Attempt to access location outside of document."}, - { TRAILING_CONTENT, "TRAILING_CONTENT: Unexpected trailing content in the JSON input."} + { TRAILING_CONTENT, "TRAILING_CONTENT: Unexpected trailing content in the JSON input."}, + { OUT_OF_CAPACITY, "OUT_OF_CAPACITY: The capacity was exceeded, we cannot allocate enough memory."} }; // error_messages[] } // namespace internal diff --git a/deps/simdjson/simdjson.h b/deps/simdjson/simdjson.h index a8bd86b28acc16..35ff2626f26735 100644 --- a/deps/simdjson/simdjson.h +++ b/deps/simdjson/simdjson.h @@ -1,4 +1,4 @@ -/* auto-generated on 2025-09-29 20:34:35 -0700. version 4.0.7 Do not edit! */ +/* auto-generated on 2025-10-27 16:52:41 -0400. version 4.1.0 Do not edit! */ /* including simdjson.h: */ /* begin file simdjson.h */ #ifndef SIMDJSON_H @@ -2508,7 +2508,7 @@ namespace std { #define SIMDJSON_SIMDJSON_VERSION_H /** The version of simdjson being used (major.minor.revision) */ -#define SIMDJSON_VERSION "4.0.7" +#define SIMDJSON_VERSION "4.1.0" namespace simdjson { enum { @@ -2519,11 +2519,11 @@ enum { /** * The minor version (major.MINOR.revision) of simdjson being used. */ - SIMDJSON_VERSION_MINOR = 0, + SIMDJSON_VERSION_MINOR = 1, /** * The revision (major.minor.REVISION) of simdjson being used. */ - SIMDJSON_VERSION_REVISION = 7 + SIMDJSON_VERSION_REVISION = 0 }; } // namespace simdjson @@ -3093,6 +3093,25 @@ concept container_but_not_string = std::ranges::input_range && !string_like && !concepts::string_view_keyed_map; + +// Concept: Indexable container that is not a string or associative container +// Accepts: std::vector, std::array, std::deque (have operator[], value_type, not string_like) +// Rejects: std::string (string_like), std::list (no operator[]), std::map (has key_type) +template +concept indexable_container = requires { + typename Container::value_type; + requires !concepts::string_like; + requires !requires { typename Container::key_type; }; // Reject maps/sets + requires requires(Container& c, std::size_t i) { + { c[i] } -> std::convertible_to; + }; +}; + + +// Variable template to use with std::meta::substitute +template +constexpr bool indexable_container_v = indexable_container; + } // namespace concepts @@ -4858,7 +4877,7 @@ class array { inline simdjson_result at_pointer(std::string_view json_pointer) const noexcept; /** - * Recursive function which processes the json path of each child element + * Recursive function which processes the JSON path of each child element */ inline void process_json_path_of_child_elements(std::vector::iterator& current, std::vector::iterator& end, const std::string_view& path_suffix, std::vector& accumulator) const noexcept; @@ -4873,7 +4892,7 @@ class array { * JSONPath queries that trivially convertible to JSON Pointer queries: key * names and array indices. * - * https://datatracker.ietf.org/doc/html/draft-normington-jsonpath-00 + * https://www.rfc-editor.org/rfc/rfc9535 (RFC 9535) * * @return The value associated with the given JSONPath expression, or: * - INVALID_JSON_POINTER if the JSONPath to JSON Pointer conversion fails @@ -5812,7 +5831,7 @@ class document_stream { /** * Construct an uninitialized document_stream. * - * ```c++ + * ```cpp * document_stream docs; * error = parser.parse_many(json).get(docs); * ``` @@ -6472,7 +6491,7 @@ class element { * JSONPath queries that trivially convertible to JSON Pointer queries: key * names and array indices. * - * https://datatracker.ietf.org/doc/html/draft-normington-jsonpath-00 + * https://www.rfc-editor.org/rfc/rfc9535 (RFC 9535) * * @return The value associated with the given JSONPath expression, or: * - INVALID_JSON_POINTER if the JSONPath to JSON Pointer conversion fails @@ -6818,7 +6837,7 @@ class object { inline simdjson_result at_pointer(std::string_view json_pointer) const noexcept; /** - * Recursive function which processes the json path of each child element + * Recursive function which processes the JSON path of each child element */ inline void process_json_path_of_child_elements(std::vector::iterator& current, std::vector::iterator& end, const std::string_view& path_suffix, std::vector& accumulator) const noexcept; @@ -6832,7 +6851,7 @@ class object { * JSONPath queries that trivially convertible to JSON Pointer queries: key * names and array indices. * - * https://datatracker.ietf.org/doc/html/draft-normington-jsonpath-00 + * https://www.rfc-editor.org/rfc/rfc9535 (RFC 9535) * * @return The value associated with the given JSONPath expression, or: * - INVALID_JSON_POINTER if the JSONPath to JSON Pointer conversion fails @@ -8036,7 +8055,7 @@ inline simdjson_result> object::at_path_with_wildcard(std:: } if (i >= json_path.size() || (json_path[i] != '.' && json_path[i] != '[')) { - // expect json path to always start with $ but this isn't currently + // expect JSONPath expressions to always start with $ but this isn't currently // expected in jsonpathutil.h. return INVALID_JSON_POINTER; } @@ -34416,7 +34435,7 @@ class value { * The following code reads z, then y, then x, and thus will not retrieve x or y if fed the * JSON `{ "x": 1, "y": 2, "z": 3 }`: * - * ```c++ + * ```cpp * simdjson::ondemand::parser parser; * auto obj = parser.parse(R"( { "x": 1, "y": 2, "z": 3 } )"_padded); * double z = obj.find_field("z"); @@ -34789,7 +34808,7 @@ struct simdjson_result : public arm64::implementation_si * The following code reads z, then y, then x, and thus will not retrieve x or y if fed the * JSON `{ "x": 1, "y": 2, "z": 3 }`: * - * ```c++ + * ```cpp * simdjson::ondemand::parser parser; * auto obj = parser.parse(R"( { "x": 1, "y": 2, "z": 3 } )"_padded); * double z = obj.find_field("z"); @@ -36676,7 +36695,7 @@ class array { * JSONPath queries that trivially convertible to JSON Pointer queries: key * names and array indices. * - * https://datatracker.ietf.org/doc/html/draft-normington-jsonpath-00 + * https://www.rfc-editor.org/rfc/rfc9535 (RFC 9535) * * @return The value associated with the given JSONPath expression, or: * - INVALID_JSON_POINTER if the JSONPath to JSON Pointer conversion fails @@ -37354,7 +37373,7 @@ class document { * The following code reads z, then y, then x, and thus will not retrieve x or y if fed the * JSON `{ "x": 1, "y": 2, "z": 3 }`: * - * ```c++ + * ```cpp * simdjson::ondemand::parser parser; * auto obj = parser.parse(R"( { "x": 1, "y": 2, "z": 3 } )"_padded); * double z = obj.find_field("z"); @@ -37648,7 +37667,7 @@ class document { * JSONPath queries that trivially convertible to JSON Pointer queries: key * names and array indices. * - * https://datatracker.ietf.org/doc/html/draft-normington-jsonpath-00 + * https://www.rfc-editor.org/rfc/rfc9535 (RFC 9535) * * Key values are matched exactly, without unescaping or Unicode normalization. * We do a byte-by-byte comparison. E.g. @@ -37681,7 +37700,7 @@ class document { * potentially improving performance by skipping unwanted fields. * * Example: - * ```c++ + * ```cpp * struct Car { * std::string make; * std::string model; @@ -38145,7 +38164,7 @@ class document_stream { /** * Construct an uninitialized document_stream. * - * ```c++ + * ```cpp * document_stream docs; * auto error = parser.iterate_many(json).get(docs); * ``` @@ -38562,7 +38581,7 @@ class object { * The following code reads z, then y, then x, and thus will not retrieve x or y if fed the * JSON `{ "x": 1, "y": 2, "z": 3 }`: * - * ```c++ + * ```cpp * simdjson::ondemand::parser parser; * auto obj = parser.parse(R"( { "x": 1, "y": 2, "z": 3 } )"_padded); * double z = obj.find_field("z"); @@ -38771,7 +38790,7 @@ class object { * potentially improving performance by skipping unwanted fields. * * Example: - * ```c++ + * ```cpp * struct Car { * std::string make; * std::string model; @@ -45753,9 +45772,9 @@ simdjson_inline void string_builder::append(number_type v) noexcept { pv = 0 - pv; // the 0 is for Microsoft } size_t dc = internal::digit_count(pv); - if (negative) { - buffer.get()[position++] = '-'; - } + // by always writing the minus sign, we avoid the branch. + buffer.get()[position] = '-'; + position += negative ? 1 : 0; char *write_pointer = buffer.get() + position + dc - 1; while (pv >= 100) { memcpy(write_pointer - 1, &internal::decimal_table[(pv % 100) * 2], 2); @@ -46424,6 +46443,949 @@ simdjson_warn_unused simdjson_result extract_from(const T &obj, siz #endif /* end file simdjson/generic/ondemand/json_builder.h for arm64 */ +// JSON path accessor (compile-time) - must be after inline definitions +/* including simdjson/generic/ondemand/compile_time_accessors.h for arm64: #include "simdjson/generic/ondemand/compile_time_accessors.h" */ +/* begin file simdjson/generic/ondemand/compile_time_accessors.h for arm64 */ +/** + * Compile-time JSON Path and JSON Pointer accessors using C++26 reflection (P2996) + * + * This file validates JSON paths/pointers against struct definitions at compile time + * and generates optimized accessor code with zero runtime overhead. + * + * ## How It Works + * + * **Compile Time**: Path is parsed, validated against struct, types are checked + * **Runtime**: Direct navigation with no parsing or validation overhead + * + * Example: + * ```cpp + * struct User { std::string name; std::vector emails; }; + * + * std::string email; + * path_accessor::extract_field(doc, email); + * + * // Compile time validates: + * // 1. User has "emails" field + * // 2. "emails" is array-like + * // 3. Element type is std::string + * // 4. static_assert(^^std::string == ^^std::string) + * + * // Runtime just navigates: + * // doc.get_object().find_field("emails").get_array().at(0).get(email) + * ``` + * + * ## Key Reflection APIs + * + * - `^^Type`: Reflect operator, converts type to std::meta::info + * - `std::meta::nonstatic_data_members_of(type)`: Get all fields of a struct + * - `std::meta::identifier_of(member)`: Get field name as string_view + * - `std::meta::type_of(member)`: Get reflected type of a field + * - `std::meta::is_array_type(type)`: Check if C-style array + * - `std::meta::remove_extent(array)`: Extract element type from array + * - `std::meta::members_of(type)`: Get all members including typedefs + * - `std::meta::is_type(member)`: Check if member is a type (vs field) + * + * All operations execute at compile time in consteval contexts. + */ +#ifndef SIMDJSON_GENERIC_ONDEMAND_COMPILE_TIME_ACCESSORS_H + +/* amalgamation skipped (editor-only): #ifndef SIMDJSON_CONDITIONAL_INCLUDE */ +/* amalgamation skipped (editor-only): #define SIMDJSON_GENERIC_ONDEMAND_COMPILE_TIME_ACCESSORS_H */ +/* amalgamation skipped (editor-only): */ +/* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ + +// Arguably, we should just check SIMDJSON_STATIC_REFLECTION since it +// is unlikely that we will have reflection support without concepts support. +#if SIMDJSON_SUPPORTS_CONCEPTS && SIMDJSON_STATIC_REFLECTION + +#include +#include +#include + +namespace simdjson { +namespace arm64 { +namespace ondemand { +/*** + * JSONPath implementation for compile-time access + * RFC 9535 JSONPath: Query Expressions for JSON, https://www.rfc-editor.org/rfc/rfc9535 + */ +namespace json_path { + +// Note: value type must be fully defined before this header is included +// This is ensured by including this in amalgamated.h after value-inl.h + +using ::simdjson::arm64::ondemand::value; + +// Path step types +enum class step_type { + field, // .field_name or ["field_name"] + array_index // [index] +}; + +// Represents a single step in a JSON path +template +struct path_step { + step_type type; + char key[N]; // Field name (empty for array indices) + std::size_t index; // Array index (0 for field access) + + constexpr path_step(step_type t, const char (&k)[N], std::size_t idx = 0) + : type(t), index(idx) { + for (std::size_t i = 0; i < N; ++i) { + key[i] = k[i]; + } + } + + constexpr std::string_view key_view() const { + return {key, N - 1}; + } +}; + +// Helper to create field step +template +consteval auto make_field_step(const char (&name)[N]) { + return path_step(step_type::field, name, 0); +} + +// Helper to create array index step +consteval auto make_index_step(std::size_t idx) { + return path_step<1>(step_type::array_index, "", idx); +} + +// Parse state for compile-time JSON path parsing +struct parse_result { + bool success; + std::size_t pos; + std::string_view error_msg; +}; + +// Compile-time JSON path parser +// Supports subset: .field, ["field"], [index], nested combinations +template +struct json_path_parser { + static constexpr std::string_view path_str = Path.view(); + + // Skip leading $ if present + static consteval std::size_t skip_root() { + if (!path_str.empty() && path_str[0] == '$') { + return 1; + } + return 0; + } + + // Count the number of steps in the path at compile time + static consteval std::size_t count_steps() { + std::size_t count = 0; + std::size_t i = skip_root(); + + while (i < path_str.size()) { + if (path_str[i] == '.') { + // Field access: .field + ++i; + if (i >= path_str.size()) break; + + // Skip field name + while (i < path_str.size() && path_str[i] != '.' && path_str[i] != '[') { + ++i; + } + ++count; + } else if (path_str[i] == '[') { + // Array or bracket notation + ++i; + if (i >= path_str.size()) break; + + if (path_str[i] == '"' || path_str[i] == '\'') { + // Field access: ["field"] or ['field'] + char quote = path_str[i]; + ++i; + while (i < path_str.size() && path_str[i] != quote) { + ++i; + } + if (i < path_str.size()) ++i; // skip closing quote + if (i < path_str.size() && path_str[i] == ']') ++i; + } else { + // Array index: [0], [123] + while (i < path_str.size() && path_str[i] != ']') { + ++i; + } + if (i < path_str.size()) ++i; // skip ] + } + ++count; + } else { + ++i; + } + } + + return count; + } + + // Parse a field name at compile time + static consteval std::size_t parse_field_name(std::size_t start, char* out, std::size_t max_len) { + std::size_t len = 0; + std::size_t i = start; + + while (i < path_str.size() && path_str[i] != '.' && path_str[i] != '[' && len < max_len - 1) { + out[len++] = path_str[i++]; + } + out[len] = '\0'; + return i; + } + + // Parse an array index at compile time + static consteval std::pair parse_array_index(std::size_t start) { + std::size_t index = 0; + std::size_t i = start; + + while (i < path_str.size() && path_str[i] >= '0' && path_str[i] <= '9') { + index = index * 10 + (path_str[i] - '0'); + ++i; + } + + return {i, index}; + } +}; + +// Compile-time path accessor generator +template +struct path_accessor { + using value = ::simdjson::arm64::ondemand::value; + + static constexpr auto parser = json_path_parser(); + static constexpr std::size_t num_steps = parser.count_steps(); + static constexpr std::string_view path_view = Path.view(); + + // Compile-time accessor generation + // If T is a struct, validates the path at compile time + // If T is void, skips validation + template + static inline simdjson_result access(DocOrValue& doc_or_val) noexcept { + // Validate path at compile time if T is a struct + if constexpr (std::is_class_v) { + constexpr bool path_valid = validate_path(); + static_assert(path_valid, "JSON path does not match struct definition"); + } + + // Parse the path at compile time to build access steps + return access_impl(doc_or_val.get_value()); + } + + // Extract value at path directly into target with compile-time type validation + // Example: std::string name; path_accessor::extract_field(doc, name); + template + static inline error_code extract_field(DocOrValue& doc_or_val, FieldType& target) noexcept { + static_assert(std::is_class_v, "extract_field requires T to be a struct type for validation"); + + // Validate path exists in struct definition + constexpr bool path_valid = validate_path(); + static_assert(path_valid, "JSON path does not match struct definition"); + + // Get the type at the end of the path + constexpr auto final_type = get_final_type(); + + // Verify target type matches the field type + static_assert(final_type == ^^FieldType, "Target type does not match the field type at the path"); + + // All validation done at compile time - just navigate and extract + auto json_value = access_impl(doc_or_val.get_value()); + if (json_value.error()) return json_value.error(); + + return json_value.get(target); + } + +private: + // Get the final type by walking the path through the struct type + template + static consteval std::enable_if_t, std::meta::info> get_final_type() { + auto current_type = ^^T; + std::size_t i = parser.skip_root(); + + while (i < path_view.size()) { + if (path_view[i] == '.') { + // .field syntax + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != '.' && path_view[i] != '[') { + ++i; + } + + std::string_view field_name = path_view.substr(field_start, i - field_start); + + auto members = std::meta::nonstatic_data_members_of( + current_type, std::meta::access_context::unchecked() + ); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == field_name) { + current_type = std::meta::type_of(mem); + break; + } + } + + } else if (path_view[i] == '[') { + ++i; + if (i >= path_view.size()) break; + + if (path_view[i] == '"' || path_view[i] == '\'') { + // ["field"] syntax + char quote = path_view[i]; + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != quote) { + ++i; + } + + std::string_view field_name = path_view.substr(field_start, i - field_start); + if (i < path_view.size()) ++i; // skip quote + if (i < path_view.size() && path_view[i] == ']') ++i; + + auto members = std::meta::nonstatic_data_members_of( + current_type, std::meta::access_context::unchecked() + ); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == field_name) { + current_type = std::meta::type_of(mem); + break; + } + } + + } else { + // [index] syntax - extract element type + while (i < path_view.size() && path_view[i] >= '0' && path_view[i] <= '9') { + ++i; + } + if (i < path_view.size() && path_view[i] == ']') ++i; + + current_type = get_element_type_reflected(current_type); + } + } else { + ++i; + } + } + + return current_type; + } + +private: + // Walk path and extract directly into final field using compile-time reflection + template + static inline error_code extract_with_reflection(simdjson_result current, TargetType& target_ref) noexcept { + if (current.error()) return current.error(); + + // Base case: end of path - extract into target + if constexpr (PathPos >= path_view.size()) { + return current.get(target_ref); + } + // Field access: .field_name + else if constexpr (path_view[PathPos] == '.') { + constexpr auto field_info = parse_next_field(PathPos); + constexpr std::string_view field_name = std::get<0>(field_info); + constexpr std::size_t next_pos = std::get<1>(field_info); + + constexpr auto member_info = find_member_by_name(CurrentType, field_name); + static_assert(member_info != ^^void, "Field not found in struct"); + + constexpr auto member_type = std::meta::type_of(member_info); + + auto obj_result = current.get_object(); + if (obj_result.error()) return obj_result.error(); + auto obj = obj_result.value_unsafe(); + auto field_value = obj.find_field_unordered(field_name); + + if constexpr (next_pos >= path_view.size()) { + return field_value.get(target_ref); + } else { + return extract_with_reflection(field_value, target_ref); + } + } + // Bracket notation: [index] or ["field"] + else if constexpr (path_view[PathPos] == '[') { + constexpr auto bracket_info = parse_bracket(PathPos); + constexpr bool is_field = std::get<0>(bracket_info); + constexpr std::size_t next_pos = std::get<2>(bracket_info); + + if constexpr (is_field) { + constexpr std::string_view field_name = std::get<1>(bracket_info); + constexpr auto member_info = find_member_by_name(CurrentType, field_name); + static_assert(member_info != ^^void, "Field not found in struct"); + constexpr auto member_type = std::meta::type_of(member_info); + + auto obj_result = current.get_object(); + if (obj_result.error()) return obj_result.error(); + auto obj = obj_result.value_unsafe(); + auto field_value = obj.find_field_unordered(field_name); + + if constexpr (next_pos >= path_view.size()) { + return field_value.get(target_ref); + } else { + return extract_with_reflection(field_value, target_ref); + } + } else { + constexpr std::size_t index = std::get<3>(bracket_info); + constexpr auto elem_type = get_element_type_reflected(CurrentType); + static_assert(elem_type != ^^void, "Could not determine array element type"); + + auto arr_result = current.get_array(); + if (arr_result.error()) return arr_result.error(); + auto arr = arr_result.value_unsafe(); + auto elem_value = arr.at(index); + + if constexpr (next_pos >= path_view.size()) { + return elem_value.get(target_ref); + } else { + return extract_with_reflection(elem_value, target_ref); + } + } + } + // Skip unexpected characters and continue + else { + return extract_with_reflection(current, target_ref); + } + } + + // Find member by name in reflected type + static consteval std::meta::info find_member_by_name(std::meta::info type_refl, std::string_view name) { + auto members = std::meta::nonstatic_data_members_of(type_refl, std::meta::access_context::unchecked()); + for (auto mem : members) { + if (std::meta::identifier_of(mem) == name) { + return mem; + } + } + } + + // Generate compile-time accessor code by walking the path + template + static inline simdjson_result access_impl(simdjson_result current) noexcept { + if (current.error()) return current; + + if constexpr (PathPos >= path_view.size()) { + return current; + } else if constexpr (path_view[PathPos] == '.') { + constexpr auto field_info = parse_next_field(PathPos); + constexpr std::string_view field_name = std::get<0>(field_info); + constexpr std::size_t next_pos = std::get<1>(field_info); + + auto obj_result = current.get_object(); + if (obj_result.error()) return obj_result.error(); + + auto obj = obj_result.value_unsafe(); + auto next_value = obj.find_field_unordered(field_name); + + return access_impl(next_value); + + } else if constexpr (path_view[PathPos] == '[') { + constexpr auto bracket_info = parse_bracket(PathPos); + constexpr bool is_field = std::get<0>(bracket_info); + constexpr std::size_t next_pos = std::get<2>(bracket_info); + + if constexpr (is_field) { + constexpr std::string_view field_name = std::get<1>(bracket_info); + + auto obj_result = current.get_object(); + if (obj_result.error()) return obj_result.error(); + + auto obj = obj_result.value_unsafe(); + auto next_value = obj.find_field_unordered(field_name); + + return access_impl(next_value); + + } else { + constexpr std::size_t index = std::get<3>(bracket_info); + + auto arr_result = current.get_array(); + if (arr_result.error()) return arr_result.error(); + + auto arr = arr_result.value_unsafe(); + auto next_value = arr.at(index); + + return access_impl(next_value); + } + } else { + return access_impl(current); + } + } + + // Parse next field name + static consteval auto parse_next_field(std::size_t start) { + std::size_t i = start + 1; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != '.' && path_view[i] != '[') { + ++i; + } + std::string_view field_name = path_view.substr(field_start, i - field_start); + return std::make_tuple(field_name, i); + } + + // Parse bracket notation: returns (is_field, field_name, next_pos, index) + static consteval auto parse_bracket(std::size_t start) { + std::size_t i = start + 1; // skip '[' + + if (i < path_view.size() && (path_view[i] == '"' || path_view[i] == '\'')) { + // Field access + char quote = path_view[i]; + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != quote) { + ++i; + } + std::string_view field_name = path_view.substr(field_start, i - field_start); + if (i < path_view.size()) ++i; // skip closing quote + if (i < path_view.size() && path_view[i] == ']') ++i; + + return std::make_tuple(true, field_name, i, std::size_t(0)); + } else { + // Array index + std::size_t index = 0; + while (i < path_view.size() && path_view[i] >= '0' && path_view[i] <= '9') { + index = index * 10 + (path_view[i] - '0'); + ++i; + } + if (i < path_view.size() && path_view[i] == ']') ++i; + + return std::make_tuple(false, std::string_view{}, i, index); + } + } + +public: + // Check if reflected type is array-like (C-style array or indexable container) + // Uses reflection to test: 1) std::meta::is_array_type() for C arrays + // 2) std::meta::substitute() to test concepts::indexable_container concept + static consteval bool is_array_like_reflected(std::meta::info type_reflection) { + if (std::meta::is_array_type(type_reflection)) { + return true; + } + + if (std::meta::can_substitute(^^concepts::indexable_container_v, {type_reflection})) { + return std::meta::extract(std::meta::substitute(^^concepts::indexable_container_v, {type_reflection})); + } + return false; + } + + // Extract element type from reflected array or container + // For C arrays: uses std::meta::remove_extent() + // For containers: finds value_type member using std::meta::members_of() + static consteval std::meta::info get_element_type_reflected(std::meta::info type_reflection) { + if (std::meta::is_array_type(type_reflection)) { + return std::meta::remove_extent(type_reflection); + } + + auto members = std::meta::members_of(type_reflection, std::meta::access_context::unchecked()); + for (auto mem : members) { + if (std::meta::is_type(mem)) { + auto name = std::meta::identifier_of(mem); + if (name == "value_type") { + return mem; + } + } + } + return ^^void; + } + +private: + // Check if type has member with given name + template + static consteval bool has_member(std::string_view member_name) { + constexpr auto members = std::meta::nonstatic_data_members_of(^^Type, std::meta::access_context::unchecked()); + for (auto mem : members) { + if (std::meta::identifier_of(mem) == member_name) { + return true; + } + } + return false; + } + + // Get type of member by name + template + static consteval auto get_member_type(std::string_view member_name) { + constexpr auto members = std::meta::nonstatic_data_members_of(^^Type, std::meta::access_context::unchecked()); + for (auto mem : members) { + if (std::meta::identifier_of(mem) == member_name) { + return std::meta::type_of(mem); + } + } + return ^^void; + } + + // Check if non-reflected type is array-like + template + static consteval bool is_container_type() { + using BaseType = std::remove_cvref_t; + if constexpr (requires { typename BaseType::value_type; }) { + return true; + } + if constexpr (std::is_array_v) { + return true; + } + return false; + } + + // Extract element type from non-reflected container + template + using extract_element_type = std::conditional_t< + requires { typename std::remove_cvref_t::value_type; }, + typename std::remove_cvref_t::value_type, + std::conditional_t< + std::is_array_v>, + std::remove_extent_t>, + void + > + >; + + // Validate path matches struct definition + static consteval bool validate_path() { + if constexpr (!std::is_class_v) { + return true; + } + + auto current_type = ^^T; + std::size_t i = parser.skip_root(); + + while (i < path_view.size()) { + if (path_view[i] == '.') { + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != '.' && path_view[i] != '[') { + ++i; + } + + std::string_view field_name = path_view.substr(field_start, i - field_start); + + bool found = false; + auto members = std::meta::nonstatic_data_members_of(current_type, std::meta::access_context::unchecked()); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == field_name) { + current_type = std::meta::type_of(mem); + found = true; + break; + } + } + + if (!found) { + return false; + } + + } else if (path_view[i] == '[') { + ++i; + if (i >= path_view.size()) return false; + + if (path_view[i] == '"' || path_view[i] == '\'') { + char quote = path_view[i]; + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != quote) { + ++i; + } + + std::string_view field_name = path_view.substr(field_start, i - field_start); + if (i < path_view.size()) ++i; + if (i < path_view.size() && path_view[i] == ']') ++i; + + bool found = false; + auto members = std::meta::nonstatic_data_members_of(current_type, std::meta::access_context::unchecked()); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == field_name) { + current_type = std::meta::type_of(mem); + found = true; + break; + } + } + + if (!found) { + return false; + } + + } else { + while (i < path_view.size() && path_view[i] >= '0' && path_view[i] <= '9') { + ++i; + } + + if (i < path_view.size() && path_view[i] == ']') ++i; + + if (!is_array_like_reflected(current_type)) { + return false; + } + + auto new_type = get_element_type_reflected(current_type); + + if (new_type == ^^void) { + return false; + } + + current_type = new_type; + } + } else { + ++i; + } + } + + return true; + } +}; + +// Compile-time path accessor with validation +template +inline simdjson_result<::simdjson::arm64::ondemand::value> at_path_compiled(DocOrValue& doc_or_val) noexcept { + using accessor = path_accessor; + return accessor::access(doc_or_val); +} + +// Overload without type parameter (no validation) +template +inline simdjson_result<::simdjson::arm64::ondemand::value> at_path_compiled(DocOrValue& doc_or_val) noexcept { + using accessor = path_accessor; + return accessor::access(doc_or_val); +} + +// ============================================================================ +// JSON Pointer Compile-Time Support (RFC 6901) +// ============================================================================ + +// JSON Pointer parser: /field/0/nested (slash-separated) +template +struct json_pointer_parser { + static constexpr std::string_view pointer_str = Pointer.view(); + + // Unescape token: ~0 -> ~, ~1 -> / + static consteval void unescape_token(std::string_view src, char* dest, std::size_t& out_len) { + out_len = 0; + for (std::size_t i = 0; i < src.size(); ++i) { + if (src[i] == '~' && i + 1 < src.size()) { + if (src[i + 1] == '0') { + dest[out_len++] = '~'; + ++i; + } else if (src[i + 1] == '1') { + dest[out_len++] = '/'; + ++i; + } else { + dest[out_len++] = src[i]; + } + } else { + dest[out_len++] = src[i]; + } + } + } + + // Check if token is numeric + static consteval bool is_numeric(std::string_view token) { + if (token.empty()) return false; + if (token[0] == '0' && token.size() > 1) return false; + for (char c : token) { + if (c < '0' || c > '9') return false; + } + return true; + } + + // Parse numeric token to index + static consteval std::size_t parse_index(std::string_view token) { + std::size_t result = 0; + for (char c : token) { + result = result * 10 + (c - '0'); + } + return result; + } + + // Count tokens in pointer + static consteval std::size_t count_tokens() { + if (pointer_str.empty() || pointer_str == "/") return 0; + + std::size_t count = 0; + std::size_t pos = pointer_str[0] == '/' ? 1 : 0; + + while (pos < pointer_str.size()) { + ++count; + std::size_t next_slash = pointer_str.find('/', pos); + if (next_slash == std::string_view::npos) break; + pos = next_slash + 1; + } + + return count; + } + + // Get Nth token + static consteval std::string_view get_token(std::size_t token_index) { + std::size_t pos = pointer_str[0] == '/' ? 1 : 0; + std::size_t current_token = 0; + + while (current_token < token_index) { + std::size_t next_slash = pointer_str.find('/', pos); + pos = next_slash + 1; + ++current_token; + } + + std::size_t token_end = pointer_str.find('/', pos); + if (token_end == std::string_view::npos) token_end = pointer_str.size(); + + return pointer_str.substr(pos, token_end - pos); + } +}; + +// JSON Pointer accessor +template +struct pointer_accessor { + using parser = json_pointer_parser; + static constexpr std::string_view pointer_view = Pointer.view(); + static constexpr std::size_t token_count = parser::count_tokens(); + + // Validate pointer against struct definition + static consteval bool validate_pointer() { + if constexpr (!std::is_class_v) { + return true; + } + + auto current_type = ^^T; + std::size_t pos = pointer_view[0] == '/' ? 1 : 0; + + while (pos < pointer_view.size()) { + // Extract token up to next / + std::size_t token_end = pointer_view.find('/', pos); + if (token_end == std::string_view::npos) token_end = pointer_view.size(); + + std::string_view token = pointer_view.substr(pos, token_end - pos); + + if (parser::is_numeric(token)) { + if (!path_accessor::is_array_like_reflected(current_type)) { + return false; + } + current_type = path_accessor::get_element_type_reflected(current_type); + } else { + bool found = false; + auto members = std::meta::nonstatic_data_members_of(current_type, std::meta::access_context::unchecked()); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == token) { + current_type = std::meta::type_of(mem); + found = true; + break; + } + } + + if (!found) return false; + } + + pos = token_end + 1; + } + + return true; + } + + // Recursive accessor + template + static inline simdjson_result access_impl(simdjson_result current) noexcept { + if constexpr (TokenIndex >= token_count) { + return current; + } else { + constexpr std::string_view token = parser::get_token(TokenIndex); + + if constexpr (parser::is_numeric(token)) { + constexpr std::size_t index = parser::parse_index(token); + auto arr = current.get_array().value_unsafe(); + auto next_value = arr.at(index); + return access_impl(next_value); + } else { + auto obj = current.get_object().value_unsafe(); + auto next_value = obj.find_field_unordered(token); + return access_impl(next_value); + } + } + } + + // Access JSON value at pointer + template + static inline simdjson_result access(DocOrValue& doc_or_val) noexcept { + if constexpr (std::is_class_v) { + constexpr bool pointer_valid = validate_pointer(); + static_assert(pointer_valid, "JSON Pointer does not match struct definition"); + } + + if (pointer_view.empty() || pointer_view == "/") { + if constexpr (requires { doc_or_val.get_value(); }) { + return doc_or_val.get_value(); + } else { + return doc_or_val; + } + } + + simdjson_result current = doc_or_val.get_value(); + return access_impl<0>(current); + } + + // Extract value at pointer directly into target with type validation + template + static inline error_code extract_field(DocOrValue& doc_or_val, FieldType& target) noexcept { + static_assert(std::is_class_v, "extract_field requires T to be a struct type for validation"); + + constexpr bool pointer_valid = validate_pointer(); + static_assert(pointer_valid, "JSON Pointer does not match struct definition"); + + constexpr auto final_type = get_final_type(); + static_assert(final_type == ^^FieldType, "Target type does not match the field type at the pointer"); + + simdjson_result current_value = doc_or_val.get_value(); + auto json_value = access_impl<0>(current_value); + if (json_value.error()) return json_value.error(); + + return json_value.get(target); + } + +private: + // Get final type by walking pointer through struct + template + static consteval std::enable_if_t, std::meta::info> get_final_type() { + auto current_type = ^^T; + std::size_t pos = pointer_view[0] == '/' ? 1 : 0; + + while (pos < pointer_view.size()) { + std::size_t token_end = pointer_view.find('/', pos); + if (token_end == std::string_view::npos) token_end = pointer_view.size(); + + std::string_view token = pointer_view.substr(pos, token_end - pos); + + if (parser::is_numeric(token)) { + current_type = path_accessor::get_element_type_reflected(current_type); + } else { + auto members = std::meta::nonstatic_data_members_of(current_type, std::meta::access_context::unchecked()); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == token) { + current_type = std::meta::type_of(mem); + break; + } + } + } + + pos = token_end + 1; + } + + return current_type; + } +}; + +// Compile-time JSON Pointer accessor with validation +template +inline simdjson_result<::simdjson::arm64::ondemand::value> at_pointer_compiled(DocOrValue& doc_or_val) noexcept { + using accessor = pointer_accessor; + return accessor::access(doc_or_val); +} + +// Overload without type parameter (no validation) +template +inline simdjson_result<::simdjson::arm64::ondemand::value> at_pointer_compiled(DocOrValue& doc_or_val) noexcept { + using accessor = pointer_accessor; + return accessor::access(doc_or_val); +} + +} // namespace json_path +} // namespace ondemand +} // namespace arm64 +} // namespace simdjson + +#endif // SIMDJSON_SUPPORTS_CONCEPTS && SIMDJSON_STATIC_REFLECTION +#endif // SIMDJSON_GENERIC_ONDEMAND_COMPILE_TIME_ACCESSORS_H + +/* end file simdjson/generic/ondemand/compile_time_accessors.h for arm64 */ + /* end file simdjson/generic/ondemand/amalgamated.h for arm64 */ /* including simdjson/arm64/end.h: #include "simdjson/arm64/end.h" */ /* begin file simdjson/arm64/end.h */ @@ -47722,7 +48684,7 @@ class value { * The following code reads z, then y, then x, and thus will not retrieve x or y if fed the * JSON `{ "x": 1, "y": 2, "z": 3 }`: * - * ```c++ + * ```cpp * simdjson::ondemand::parser parser; * auto obj = parser.parse(R"( { "x": 1, "y": 2, "z": 3 } )"_padded); * double z = obj.find_field("z"); @@ -48095,7 +49057,7 @@ struct simdjson_result : public fallback::implementat * The following code reads z, then y, then x, and thus will not retrieve x or y if fed the * JSON `{ "x": 1, "y": 2, "z": 3 }`: * - * ```c++ + * ```cpp * simdjson::ondemand::parser parser; * auto obj = parser.parse(R"( { "x": 1, "y": 2, "z": 3 } )"_padded); * double z = obj.find_field("z"); @@ -49982,7 +50944,7 @@ class array { * JSONPath queries that trivially convertible to JSON Pointer queries: key * names and array indices. * - * https://datatracker.ietf.org/doc/html/draft-normington-jsonpath-00 + * https://www.rfc-editor.org/rfc/rfc9535 (RFC 9535) * * @return The value associated with the given JSONPath expression, or: * - INVALID_JSON_POINTER if the JSONPath to JSON Pointer conversion fails @@ -50660,7 +51622,7 @@ class document { * The following code reads z, then y, then x, and thus will not retrieve x or y if fed the * JSON `{ "x": 1, "y": 2, "z": 3 }`: * - * ```c++ + * ```cpp * simdjson::ondemand::parser parser; * auto obj = parser.parse(R"( { "x": 1, "y": 2, "z": 3 } )"_padded); * double z = obj.find_field("z"); @@ -50954,7 +51916,7 @@ class document { * JSONPath queries that trivially convertible to JSON Pointer queries: key * names and array indices. * - * https://datatracker.ietf.org/doc/html/draft-normington-jsonpath-00 + * https://www.rfc-editor.org/rfc/rfc9535 (RFC 9535) * * Key values are matched exactly, without unescaping or Unicode normalization. * We do a byte-by-byte comparison. E.g. @@ -50987,7 +51949,7 @@ class document { * potentially improving performance by skipping unwanted fields. * * Example: - * ```c++ + * ```cpp * struct Car { * std::string make; * std::string model; @@ -51451,7 +52413,7 @@ class document_stream { /** * Construct an uninitialized document_stream. * - * ```c++ + * ```cpp * document_stream docs; * auto error = parser.iterate_many(json).get(docs); * ``` @@ -51868,7 +52830,7 @@ class object { * The following code reads z, then y, then x, and thus will not retrieve x or y if fed the * JSON `{ "x": 1, "y": 2, "z": 3 }`: * - * ```c++ + * ```cpp * simdjson::ondemand::parser parser; * auto obj = parser.parse(R"( { "x": 1, "y": 2, "z": 3 } )"_padded); * double z = obj.find_field("z"); @@ -52077,7 +53039,7 @@ class object { * potentially improving performance by skipping unwanted fields. * * Example: - * ```c++ + * ```cpp * struct Car { * std::string make; * std::string model; @@ -59059,9 +60021,9 @@ simdjson_inline void string_builder::append(number_type v) noexcept { pv = 0 - pv; // the 0 is for Microsoft } size_t dc = internal::digit_count(pv); - if (negative) { - buffer.get()[position++] = '-'; - } + // by always writing the minus sign, we avoid the branch. + buffer.get()[position] = '-'; + position += negative ? 1 : 0; char *write_pointer = buffer.get() + position + dc - 1; while (pv >= 100) { memcpy(write_pointer - 1, &internal::decimal_table[(pv % 100) * 2], 2); @@ -59730,6 +60692,949 @@ simdjson_warn_unused simdjson_result extract_from(const T &obj, siz #endif /* end file simdjson/generic/ondemand/json_builder.h for fallback */ +// JSON path accessor (compile-time) - must be after inline definitions +/* including simdjson/generic/ondemand/compile_time_accessors.h for fallback: #include "simdjson/generic/ondemand/compile_time_accessors.h" */ +/* begin file simdjson/generic/ondemand/compile_time_accessors.h for fallback */ +/** + * Compile-time JSON Path and JSON Pointer accessors using C++26 reflection (P2996) + * + * This file validates JSON paths/pointers against struct definitions at compile time + * and generates optimized accessor code with zero runtime overhead. + * + * ## How It Works + * + * **Compile Time**: Path is parsed, validated against struct, types are checked + * **Runtime**: Direct navigation with no parsing or validation overhead + * + * Example: + * ```cpp + * struct User { std::string name; std::vector emails; }; + * + * std::string email; + * path_accessor::extract_field(doc, email); + * + * // Compile time validates: + * // 1. User has "emails" field + * // 2. "emails" is array-like + * // 3. Element type is std::string + * // 4. static_assert(^^std::string == ^^std::string) + * + * // Runtime just navigates: + * // doc.get_object().find_field("emails").get_array().at(0).get(email) + * ``` + * + * ## Key Reflection APIs + * + * - `^^Type`: Reflect operator, converts type to std::meta::info + * - `std::meta::nonstatic_data_members_of(type)`: Get all fields of a struct + * - `std::meta::identifier_of(member)`: Get field name as string_view + * - `std::meta::type_of(member)`: Get reflected type of a field + * - `std::meta::is_array_type(type)`: Check if C-style array + * - `std::meta::remove_extent(array)`: Extract element type from array + * - `std::meta::members_of(type)`: Get all members including typedefs + * - `std::meta::is_type(member)`: Check if member is a type (vs field) + * + * All operations execute at compile time in consteval contexts. + */ +#ifndef SIMDJSON_GENERIC_ONDEMAND_COMPILE_TIME_ACCESSORS_H + +/* amalgamation skipped (editor-only): #ifndef SIMDJSON_CONDITIONAL_INCLUDE */ +/* amalgamation skipped (editor-only): #define SIMDJSON_GENERIC_ONDEMAND_COMPILE_TIME_ACCESSORS_H */ +/* amalgamation skipped (editor-only): */ +/* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ + +// Arguably, we should just check SIMDJSON_STATIC_REFLECTION since it +// is unlikely that we will have reflection support without concepts support. +#if SIMDJSON_SUPPORTS_CONCEPTS && SIMDJSON_STATIC_REFLECTION + +#include +#include +#include + +namespace simdjson { +namespace fallback { +namespace ondemand { +/*** + * JSONPath implementation for compile-time access + * RFC 9535 JSONPath: Query Expressions for JSON, https://www.rfc-editor.org/rfc/rfc9535 + */ +namespace json_path { + +// Note: value type must be fully defined before this header is included +// This is ensured by including this in amalgamated.h after value-inl.h + +using ::simdjson::fallback::ondemand::value; + +// Path step types +enum class step_type { + field, // .field_name or ["field_name"] + array_index // [index] +}; + +// Represents a single step in a JSON path +template +struct path_step { + step_type type; + char key[N]; // Field name (empty for array indices) + std::size_t index; // Array index (0 for field access) + + constexpr path_step(step_type t, const char (&k)[N], std::size_t idx = 0) + : type(t), index(idx) { + for (std::size_t i = 0; i < N; ++i) { + key[i] = k[i]; + } + } + + constexpr std::string_view key_view() const { + return {key, N - 1}; + } +}; + +// Helper to create field step +template +consteval auto make_field_step(const char (&name)[N]) { + return path_step(step_type::field, name, 0); +} + +// Helper to create array index step +consteval auto make_index_step(std::size_t idx) { + return path_step<1>(step_type::array_index, "", idx); +} + +// Parse state for compile-time JSON path parsing +struct parse_result { + bool success; + std::size_t pos; + std::string_view error_msg; +}; + +// Compile-time JSON path parser +// Supports subset: .field, ["field"], [index], nested combinations +template +struct json_path_parser { + static constexpr std::string_view path_str = Path.view(); + + // Skip leading $ if present + static consteval std::size_t skip_root() { + if (!path_str.empty() && path_str[0] == '$') { + return 1; + } + return 0; + } + + // Count the number of steps in the path at compile time + static consteval std::size_t count_steps() { + std::size_t count = 0; + std::size_t i = skip_root(); + + while (i < path_str.size()) { + if (path_str[i] == '.') { + // Field access: .field + ++i; + if (i >= path_str.size()) break; + + // Skip field name + while (i < path_str.size() && path_str[i] != '.' && path_str[i] != '[') { + ++i; + } + ++count; + } else if (path_str[i] == '[') { + // Array or bracket notation + ++i; + if (i >= path_str.size()) break; + + if (path_str[i] == '"' || path_str[i] == '\'') { + // Field access: ["field"] or ['field'] + char quote = path_str[i]; + ++i; + while (i < path_str.size() && path_str[i] != quote) { + ++i; + } + if (i < path_str.size()) ++i; // skip closing quote + if (i < path_str.size() && path_str[i] == ']') ++i; + } else { + // Array index: [0], [123] + while (i < path_str.size() && path_str[i] != ']') { + ++i; + } + if (i < path_str.size()) ++i; // skip ] + } + ++count; + } else { + ++i; + } + } + + return count; + } + + // Parse a field name at compile time + static consteval std::size_t parse_field_name(std::size_t start, char* out, std::size_t max_len) { + std::size_t len = 0; + std::size_t i = start; + + while (i < path_str.size() && path_str[i] != '.' && path_str[i] != '[' && len < max_len - 1) { + out[len++] = path_str[i++]; + } + out[len] = '\0'; + return i; + } + + // Parse an array index at compile time + static consteval std::pair parse_array_index(std::size_t start) { + std::size_t index = 0; + std::size_t i = start; + + while (i < path_str.size() && path_str[i] >= '0' && path_str[i] <= '9') { + index = index * 10 + (path_str[i] - '0'); + ++i; + } + + return {i, index}; + } +}; + +// Compile-time path accessor generator +template +struct path_accessor { + using value = ::simdjson::fallback::ondemand::value; + + static constexpr auto parser = json_path_parser(); + static constexpr std::size_t num_steps = parser.count_steps(); + static constexpr std::string_view path_view = Path.view(); + + // Compile-time accessor generation + // If T is a struct, validates the path at compile time + // If T is void, skips validation + template + static inline simdjson_result access(DocOrValue& doc_or_val) noexcept { + // Validate path at compile time if T is a struct + if constexpr (std::is_class_v) { + constexpr bool path_valid = validate_path(); + static_assert(path_valid, "JSON path does not match struct definition"); + } + + // Parse the path at compile time to build access steps + return access_impl(doc_or_val.get_value()); + } + + // Extract value at path directly into target with compile-time type validation + // Example: std::string name; path_accessor::extract_field(doc, name); + template + static inline error_code extract_field(DocOrValue& doc_or_val, FieldType& target) noexcept { + static_assert(std::is_class_v, "extract_field requires T to be a struct type for validation"); + + // Validate path exists in struct definition + constexpr bool path_valid = validate_path(); + static_assert(path_valid, "JSON path does not match struct definition"); + + // Get the type at the end of the path + constexpr auto final_type = get_final_type(); + + // Verify target type matches the field type + static_assert(final_type == ^^FieldType, "Target type does not match the field type at the path"); + + // All validation done at compile time - just navigate and extract + auto json_value = access_impl(doc_or_val.get_value()); + if (json_value.error()) return json_value.error(); + + return json_value.get(target); + } + +private: + // Get the final type by walking the path through the struct type + template + static consteval std::enable_if_t, std::meta::info> get_final_type() { + auto current_type = ^^T; + std::size_t i = parser.skip_root(); + + while (i < path_view.size()) { + if (path_view[i] == '.') { + // .field syntax + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != '.' && path_view[i] != '[') { + ++i; + } + + std::string_view field_name = path_view.substr(field_start, i - field_start); + + auto members = std::meta::nonstatic_data_members_of( + current_type, std::meta::access_context::unchecked() + ); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == field_name) { + current_type = std::meta::type_of(mem); + break; + } + } + + } else if (path_view[i] == '[') { + ++i; + if (i >= path_view.size()) break; + + if (path_view[i] == '"' || path_view[i] == '\'') { + // ["field"] syntax + char quote = path_view[i]; + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != quote) { + ++i; + } + + std::string_view field_name = path_view.substr(field_start, i - field_start); + if (i < path_view.size()) ++i; // skip quote + if (i < path_view.size() && path_view[i] == ']') ++i; + + auto members = std::meta::nonstatic_data_members_of( + current_type, std::meta::access_context::unchecked() + ); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == field_name) { + current_type = std::meta::type_of(mem); + break; + } + } + + } else { + // [index] syntax - extract element type + while (i < path_view.size() && path_view[i] >= '0' && path_view[i] <= '9') { + ++i; + } + if (i < path_view.size() && path_view[i] == ']') ++i; + + current_type = get_element_type_reflected(current_type); + } + } else { + ++i; + } + } + + return current_type; + } + +private: + // Walk path and extract directly into final field using compile-time reflection + template + static inline error_code extract_with_reflection(simdjson_result current, TargetType& target_ref) noexcept { + if (current.error()) return current.error(); + + // Base case: end of path - extract into target + if constexpr (PathPos >= path_view.size()) { + return current.get(target_ref); + } + // Field access: .field_name + else if constexpr (path_view[PathPos] == '.') { + constexpr auto field_info = parse_next_field(PathPos); + constexpr std::string_view field_name = std::get<0>(field_info); + constexpr std::size_t next_pos = std::get<1>(field_info); + + constexpr auto member_info = find_member_by_name(CurrentType, field_name); + static_assert(member_info != ^^void, "Field not found in struct"); + + constexpr auto member_type = std::meta::type_of(member_info); + + auto obj_result = current.get_object(); + if (obj_result.error()) return obj_result.error(); + auto obj = obj_result.value_unsafe(); + auto field_value = obj.find_field_unordered(field_name); + + if constexpr (next_pos >= path_view.size()) { + return field_value.get(target_ref); + } else { + return extract_with_reflection(field_value, target_ref); + } + } + // Bracket notation: [index] or ["field"] + else if constexpr (path_view[PathPos] == '[') { + constexpr auto bracket_info = parse_bracket(PathPos); + constexpr bool is_field = std::get<0>(bracket_info); + constexpr std::size_t next_pos = std::get<2>(bracket_info); + + if constexpr (is_field) { + constexpr std::string_view field_name = std::get<1>(bracket_info); + constexpr auto member_info = find_member_by_name(CurrentType, field_name); + static_assert(member_info != ^^void, "Field not found in struct"); + constexpr auto member_type = std::meta::type_of(member_info); + + auto obj_result = current.get_object(); + if (obj_result.error()) return obj_result.error(); + auto obj = obj_result.value_unsafe(); + auto field_value = obj.find_field_unordered(field_name); + + if constexpr (next_pos >= path_view.size()) { + return field_value.get(target_ref); + } else { + return extract_with_reflection(field_value, target_ref); + } + } else { + constexpr std::size_t index = std::get<3>(bracket_info); + constexpr auto elem_type = get_element_type_reflected(CurrentType); + static_assert(elem_type != ^^void, "Could not determine array element type"); + + auto arr_result = current.get_array(); + if (arr_result.error()) return arr_result.error(); + auto arr = arr_result.value_unsafe(); + auto elem_value = arr.at(index); + + if constexpr (next_pos >= path_view.size()) { + return elem_value.get(target_ref); + } else { + return extract_with_reflection(elem_value, target_ref); + } + } + } + // Skip unexpected characters and continue + else { + return extract_with_reflection(current, target_ref); + } + } + + // Find member by name in reflected type + static consteval std::meta::info find_member_by_name(std::meta::info type_refl, std::string_view name) { + auto members = std::meta::nonstatic_data_members_of(type_refl, std::meta::access_context::unchecked()); + for (auto mem : members) { + if (std::meta::identifier_of(mem) == name) { + return mem; + } + } + } + + // Generate compile-time accessor code by walking the path + template + static inline simdjson_result access_impl(simdjson_result current) noexcept { + if (current.error()) return current; + + if constexpr (PathPos >= path_view.size()) { + return current; + } else if constexpr (path_view[PathPos] == '.') { + constexpr auto field_info = parse_next_field(PathPos); + constexpr std::string_view field_name = std::get<0>(field_info); + constexpr std::size_t next_pos = std::get<1>(field_info); + + auto obj_result = current.get_object(); + if (obj_result.error()) return obj_result.error(); + + auto obj = obj_result.value_unsafe(); + auto next_value = obj.find_field_unordered(field_name); + + return access_impl(next_value); + + } else if constexpr (path_view[PathPos] == '[') { + constexpr auto bracket_info = parse_bracket(PathPos); + constexpr bool is_field = std::get<0>(bracket_info); + constexpr std::size_t next_pos = std::get<2>(bracket_info); + + if constexpr (is_field) { + constexpr std::string_view field_name = std::get<1>(bracket_info); + + auto obj_result = current.get_object(); + if (obj_result.error()) return obj_result.error(); + + auto obj = obj_result.value_unsafe(); + auto next_value = obj.find_field_unordered(field_name); + + return access_impl(next_value); + + } else { + constexpr std::size_t index = std::get<3>(bracket_info); + + auto arr_result = current.get_array(); + if (arr_result.error()) return arr_result.error(); + + auto arr = arr_result.value_unsafe(); + auto next_value = arr.at(index); + + return access_impl(next_value); + } + } else { + return access_impl(current); + } + } + + // Parse next field name + static consteval auto parse_next_field(std::size_t start) { + std::size_t i = start + 1; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != '.' && path_view[i] != '[') { + ++i; + } + std::string_view field_name = path_view.substr(field_start, i - field_start); + return std::make_tuple(field_name, i); + } + + // Parse bracket notation: returns (is_field, field_name, next_pos, index) + static consteval auto parse_bracket(std::size_t start) { + std::size_t i = start + 1; // skip '[' + + if (i < path_view.size() && (path_view[i] == '"' || path_view[i] == '\'')) { + // Field access + char quote = path_view[i]; + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != quote) { + ++i; + } + std::string_view field_name = path_view.substr(field_start, i - field_start); + if (i < path_view.size()) ++i; // skip closing quote + if (i < path_view.size() && path_view[i] == ']') ++i; + + return std::make_tuple(true, field_name, i, std::size_t(0)); + } else { + // Array index + std::size_t index = 0; + while (i < path_view.size() && path_view[i] >= '0' && path_view[i] <= '9') { + index = index * 10 + (path_view[i] - '0'); + ++i; + } + if (i < path_view.size() && path_view[i] == ']') ++i; + + return std::make_tuple(false, std::string_view{}, i, index); + } + } + +public: + // Check if reflected type is array-like (C-style array or indexable container) + // Uses reflection to test: 1) std::meta::is_array_type() for C arrays + // 2) std::meta::substitute() to test concepts::indexable_container concept + static consteval bool is_array_like_reflected(std::meta::info type_reflection) { + if (std::meta::is_array_type(type_reflection)) { + return true; + } + + if (std::meta::can_substitute(^^concepts::indexable_container_v, {type_reflection})) { + return std::meta::extract(std::meta::substitute(^^concepts::indexable_container_v, {type_reflection})); + } + return false; + } + + // Extract element type from reflected array or container + // For C arrays: uses std::meta::remove_extent() + // For containers: finds value_type member using std::meta::members_of() + static consteval std::meta::info get_element_type_reflected(std::meta::info type_reflection) { + if (std::meta::is_array_type(type_reflection)) { + return std::meta::remove_extent(type_reflection); + } + + auto members = std::meta::members_of(type_reflection, std::meta::access_context::unchecked()); + for (auto mem : members) { + if (std::meta::is_type(mem)) { + auto name = std::meta::identifier_of(mem); + if (name == "value_type") { + return mem; + } + } + } + return ^^void; + } + +private: + // Check if type has member with given name + template + static consteval bool has_member(std::string_view member_name) { + constexpr auto members = std::meta::nonstatic_data_members_of(^^Type, std::meta::access_context::unchecked()); + for (auto mem : members) { + if (std::meta::identifier_of(mem) == member_name) { + return true; + } + } + return false; + } + + // Get type of member by name + template + static consteval auto get_member_type(std::string_view member_name) { + constexpr auto members = std::meta::nonstatic_data_members_of(^^Type, std::meta::access_context::unchecked()); + for (auto mem : members) { + if (std::meta::identifier_of(mem) == member_name) { + return std::meta::type_of(mem); + } + } + return ^^void; + } + + // Check if non-reflected type is array-like + template + static consteval bool is_container_type() { + using BaseType = std::remove_cvref_t; + if constexpr (requires { typename BaseType::value_type; }) { + return true; + } + if constexpr (std::is_array_v) { + return true; + } + return false; + } + + // Extract element type from non-reflected container + template + using extract_element_type = std::conditional_t< + requires { typename std::remove_cvref_t::value_type; }, + typename std::remove_cvref_t::value_type, + std::conditional_t< + std::is_array_v>, + std::remove_extent_t>, + void + > + >; + + // Validate path matches struct definition + static consteval bool validate_path() { + if constexpr (!std::is_class_v) { + return true; + } + + auto current_type = ^^T; + std::size_t i = parser.skip_root(); + + while (i < path_view.size()) { + if (path_view[i] == '.') { + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != '.' && path_view[i] != '[') { + ++i; + } + + std::string_view field_name = path_view.substr(field_start, i - field_start); + + bool found = false; + auto members = std::meta::nonstatic_data_members_of(current_type, std::meta::access_context::unchecked()); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == field_name) { + current_type = std::meta::type_of(mem); + found = true; + break; + } + } + + if (!found) { + return false; + } + + } else if (path_view[i] == '[') { + ++i; + if (i >= path_view.size()) return false; + + if (path_view[i] == '"' || path_view[i] == '\'') { + char quote = path_view[i]; + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != quote) { + ++i; + } + + std::string_view field_name = path_view.substr(field_start, i - field_start); + if (i < path_view.size()) ++i; + if (i < path_view.size() && path_view[i] == ']') ++i; + + bool found = false; + auto members = std::meta::nonstatic_data_members_of(current_type, std::meta::access_context::unchecked()); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == field_name) { + current_type = std::meta::type_of(mem); + found = true; + break; + } + } + + if (!found) { + return false; + } + + } else { + while (i < path_view.size() && path_view[i] >= '0' && path_view[i] <= '9') { + ++i; + } + + if (i < path_view.size() && path_view[i] == ']') ++i; + + if (!is_array_like_reflected(current_type)) { + return false; + } + + auto new_type = get_element_type_reflected(current_type); + + if (new_type == ^^void) { + return false; + } + + current_type = new_type; + } + } else { + ++i; + } + } + + return true; + } +}; + +// Compile-time path accessor with validation +template +inline simdjson_result<::simdjson::fallback::ondemand::value> at_path_compiled(DocOrValue& doc_or_val) noexcept { + using accessor = path_accessor; + return accessor::access(doc_or_val); +} + +// Overload without type parameter (no validation) +template +inline simdjson_result<::simdjson::fallback::ondemand::value> at_path_compiled(DocOrValue& doc_or_val) noexcept { + using accessor = path_accessor; + return accessor::access(doc_or_val); +} + +// ============================================================================ +// JSON Pointer Compile-Time Support (RFC 6901) +// ============================================================================ + +// JSON Pointer parser: /field/0/nested (slash-separated) +template +struct json_pointer_parser { + static constexpr std::string_view pointer_str = Pointer.view(); + + // Unescape token: ~0 -> ~, ~1 -> / + static consteval void unescape_token(std::string_view src, char* dest, std::size_t& out_len) { + out_len = 0; + for (std::size_t i = 0; i < src.size(); ++i) { + if (src[i] == '~' && i + 1 < src.size()) { + if (src[i + 1] == '0') { + dest[out_len++] = '~'; + ++i; + } else if (src[i + 1] == '1') { + dest[out_len++] = '/'; + ++i; + } else { + dest[out_len++] = src[i]; + } + } else { + dest[out_len++] = src[i]; + } + } + } + + // Check if token is numeric + static consteval bool is_numeric(std::string_view token) { + if (token.empty()) return false; + if (token[0] == '0' && token.size() > 1) return false; + for (char c : token) { + if (c < '0' || c > '9') return false; + } + return true; + } + + // Parse numeric token to index + static consteval std::size_t parse_index(std::string_view token) { + std::size_t result = 0; + for (char c : token) { + result = result * 10 + (c - '0'); + } + return result; + } + + // Count tokens in pointer + static consteval std::size_t count_tokens() { + if (pointer_str.empty() || pointer_str == "/") return 0; + + std::size_t count = 0; + std::size_t pos = pointer_str[0] == '/' ? 1 : 0; + + while (pos < pointer_str.size()) { + ++count; + std::size_t next_slash = pointer_str.find('/', pos); + if (next_slash == std::string_view::npos) break; + pos = next_slash + 1; + } + + return count; + } + + // Get Nth token + static consteval std::string_view get_token(std::size_t token_index) { + std::size_t pos = pointer_str[0] == '/' ? 1 : 0; + std::size_t current_token = 0; + + while (current_token < token_index) { + std::size_t next_slash = pointer_str.find('/', pos); + pos = next_slash + 1; + ++current_token; + } + + std::size_t token_end = pointer_str.find('/', pos); + if (token_end == std::string_view::npos) token_end = pointer_str.size(); + + return pointer_str.substr(pos, token_end - pos); + } +}; + +// JSON Pointer accessor +template +struct pointer_accessor { + using parser = json_pointer_parser; + static constexpr std::string_view pointer_view = Pointer.view(); + static constexpr std::size_t token_count = parser::count_tokens(); + + // Validate pointer against struct definition + static consteval bool validate_pointer() { + if constexpr (!std::is_class_v) { + return true; + } + + auto current_type = ^^T; + std::size_t pos = pointer_view[0] == '/' ? 1 : 0; + + while (pos < pointer_view.size()) { + // Extract token up to next / + std::size_t token_end = pointer_view.find('/', pos); + if (token_end == std::string_view::npos) token_end = pointer_view.size(); + + std::string_view token = pointer_view.substr(pos, token_end - pos); + + if (parser::is_numeric(token)) { + if (!path_accessor::is_array_like_reflected(current_type)) { + return false; + } + current_type = path_accessor::get_element_type_reflected(current_type); + } else { + bool found = false; + auto members = std::meta::nonstatic_data_members_of(current_type, std::meta::access_context::unchecked()); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == token) { + current_type = std::meta::type_of(mem); + found = true; + break; + } + } + + if (!found) return false; + } + + pos = token_end + 1; + } + + return true; + } + + // Recursive accessor + template + static inline simdjson_result access_impl(simdjson_result current) noexcept { + if constexpr (TokenIndex >= token_count) { + return current; + } else { + constexpr std::string_view token = parser::get_token(TokenIndex); + + if constexpr (parser::is_numeric(token)) { + constexpr std::size_t index = parser::parse_index(token); + auto arr = current.get_array().value_unsafe(); + auto next_value = arr.at(index); + return access_impl(next_value); + } else { + auto obj = current.get_object().value_unsafe(); + auto next_value = obj.find_field_unordered(token); + return access_impl(next_value); + } + } + } + + // Access JSON value at pointer + template + static inline simdjson_result access(DocOrValue& doc_or_val) noexcept { + if constexpr (std::is_class_v) { + constexpr bool pointer_valid = validate_pointer(); + static_assert(pointer_valid, "JSON Pointer does not match struct definition"); + } + + if (pointer_view.empty() || pointer_view == "/") { + if constexpr (requires { doc_or_val.get_value(); }) { + return doc_or_val.get_value(); + } else { + return doc_or_val; + } + } + + simdjson_result current = doc_or_val.get_value(); + return access_impl<0>(current); + } + + // Extract value at pointer directly into target with type validation + template + static inline error_code extract_field(DocOrValue& doc_or_val, FieldType& target) noexcept { + static_assert(std::is_class_v, "extract_field requires T to be a struct type for validation"); + + constexpr bool pointer_valid = validate_pointer(); + static_assert(pointer_valid, "JSON Pointer does not match struct definition"); + + constexpr auto final_type = get_final_type(); + static_assert(final_type == ^^FieldType, "Target type does not match the field type at the pointer"); + + simdjson_result current_value = doc_or_val.get_value(); + auto json_value = access_impl<0>(current_value); + if (json_value.error()) return json_value.error(); + + return json_value.get(target); + } + +private: + // Get final type by walking pointer through struct + template + static consteval std::enable_if_t, std::meta::info> get_final_type() { + auto current_type = ^^T; + std::size_t pos = pointer_view[0] == '/' ? 1 : 0; + + while (pos < pointer_view.size()) { + std::size_t token_end = pointer_view.find('/', pos); + if (token_end == std::string_view::npos) token_end = pointer_view.size(); + + std::string_view token = pointer_view.substr(pos, token_end - pos); + + if (parser::is_numeric(token)) { + current_type = path_accessor::get_element_type_reflected(current_type); + } else { + auto members = std::meta::nonstatic_data_members_of(current_type, std::meta::access_context::unchecked()); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == token) { + current_type = std::meta::type_of(mem); + break; + } + } + } + + pos = token_end + 1; + } + + return current_type; + } +}; + +// Compile-time JSON Pointer accessor with validation +template +inline simdjson_result<::simdjson::fallback::ondemand::value> at_pointer_compiled(DocOrValue& doc_or_val) noexcept { + using accessor = pointer_accessor; + return accessor::access(doc_or_val); +} + +// Overload without type parameter (no validation) +template +inline simdjson_result<::simdjson::fallback::ondemand::value> at_pointer_compiled(DocOrValue& doc_or_val) noexcept { + using accessor = pointer_accessor; + return accessor::access(doc_or_val); +} + +} // namespace json_path +} // namespace ondemand +} // namespace fallback +} // namespace simdjson + +#endif // SIMDJSON_SUPPORTS_CONCEPTS && SIMDJSON_STATIC_REFLECTION +#endif // SIMDJSON_GENERIC_ONDEMAND_COMPILE_TIME_ACCESSORS_H + +/* end file simdjson/generic/ondemand/compile_time_accessors.h for fallback */ + /* end file simdjson/generic/ondemand/amalgamated.h for fallback */ /* including simdjson/fallback/end.h: #include "simdjson/fallback/end.h" */ /* begin file simdjson/fallback/end.h */ @@ -61527,7 +63432,7 @@ class value { * The following code reads z, then y, then x, and thus will not retrieve x or y if fed the * JSON `{ "x": 1, "y": 2, "z": 3 }`: * - * ```c++ + * ```cpp * simdjson::ondemand::parser parser; * auto obj = parser.parse(R"( { "x": 1, "y": 2, "z": 3 } )"_padded); * double z = obj.find_field("z"); @@ -61900,7 +63805,7 @@ struct simdjson_result : public haswell::implementatio * The following code reads z, then y, then x, and thus will not retrieve x or y if fed the * JSON `{ "x": 1, "y": 2, "z": 3 }`: * - * ```c++ + * ```cpp * simdjson::ondemand::parser parser; * auto obj = parser.parse(R"( { "x": 1, "y": 2, "z": 3 } )"_padded); * double z = obj.find_field("z"); @@ -63787,7 +65692,7 @@ class array { * JSONPath queries that trivially convertible to JSON Pointer queries: key * names and array indices. * - * https://datatracker.ietf.org/doc/html/draft-normington-jsonpath-00 + * https://www.rfc-editor.org/rfc/rfc9535 (RFC 9535) * * @return The value associated with the given JSONPath expression, or: * - INVALID_JSON_POINTER if the JSONPath to JSON Pointer conversion fails @@ -64465,7 +66370,7 @@ class document { * The following code reads z, then y, then x, and thus will not retrieve x or y if fed the * JSON `{ "x": 1, "y": 2, "z": 3 }`: * - * ```c++ + * ```cpp * simdjson::ondemand::parser parser; * auto obj = parser.parse(R"( { "x": 1, "y": 2, "z": 3 } )"_padded); * double z = obj.find_field("z"); @@ -64759,7 +66664,7 @@ class document { * JSONPath queries that trivially convertible to JSON Pointer queries: key * names and array indices. * - * https://datatracker.ietf.org/doc/html/draft-normington-jsonpath-00 + * https://www.rfc-editor.org/rfc/rfc9535 (RFC 9535) * * Key values are matched exactly, without unescaping or Unicode normalization. * We do a byte-by-byte comparison. E.g. @@ -64792,7 +66697,7 @@ class document { * potentially improving performance by skipping unwanted fields. * * Example: - * ```c++ + * ```cpp * struct Car { * std::string make; * std::string model; @@ -65256,7 +67161,7 @@ class document_stream { /** * Construct an uninitialized document_stream. * - * ```c++ + * ```cpp * document_stream docs; * auto error = parser.iterate_many(json).get(docs); * ``` @@ -65673,7 +67578,7 @@ class object { * The following code reads z, then y, then x, and thus will not retrieve x or y if fed the * JSON `{ "x": 1, "y": 2, "z": 3 }`: * - * ```c++ + * ```cpp * simdjson::ondemand::parser parser; * auto obj = parser.parse(R"( { "x": 1, "y": 2, "z": 3 } )"_padded); * double z = obj.find_field("z"); @@ -65882,7 +67787,7 @@ class object { * potentially improving performance by skipping unwanted fields. * * Example: - * ```c++ + * ```cpp * struct Car { * std::string make; * std::string model; @@ -72864,9 +74769,9 @@ simdjson_inline void string_builder::append(number_type v) noexcept { pv = 0 - pv; // the 0 is for Microsoft } size_t dc = internal::digit_count(pv); - if (negative) { - buffer.get()[position++] = '-'; - } + // by always writing the minus sign, we avoid the branch. + buffer.get()[position] = '-'; + position += negative ? 1 : 0; char *write_pointer = buffer.get() + position + dc - 1; while (pv >= 100) { memcpy(write_pointer - 1, &internal::decimal_table[(pv % 100) * 2], 2); @@ -73535,6 +75440,949 @@ simdjson_warn_unused simdjson_result extract_from(const T &obj, siz #endif /* end file simdjson/generic/ondemand/json_builder.h for haswell */ +// JSON path accessor (compile-time) - must be after inline definitions +/* including simdjson/generic/ondemand/compile_time_accessors.h for haswell: #include "simdjson/generic/ondemand/compile_time_accessors.h" */ +/* begin file simdjson/generic/ondemand/compile_time_accessors.h for haswell */ +/** + * Compile-time JSON Path and JSON Pointer accessors using C++26 reflection (P2996) + * + * This file validates JSON paths/pointers against struct definitions at compile time + * and generates optimized accessor code with zero runtime overhead. + * + * ## How It Works + * + * **Compile Time**: Path is parsed, validated against struct, types are checked + * **Runtime**: Direct navigation with no parsing or validation overhead + * + * Example: + * ```cpp + * struct User { std::string name; std::vector emails; }; + * + * std::string email; + * path_accessor::extract_field(doc, email); + * + * // Compile time validates: + * // 1. User has "emails" field + * // 2. "emails" is array-like + * // 3. Element type is std::string + * // 4. static_assert(^^std::string == ^^std::string) + * + * // Runtime just navigates: + * // doc.get_object().find_field("emails").get_array().at(0).get(email) + * ``` + * + * ## Key Reflection APIs + * + * - `^^Type`: Reflect operator, converts type to std::meta::info + * - `std::meta::nonstatic_data_members_of(type)`: Get all fields of a struct + * - `std::meta::identifier_of(member)`: Get field name as string_view + * - `std::meta::type_of(member)`: Get reflected type of a field + * - `std::meta::is_array_type(type)`: Check if C-style array + * - `std::meta::remove_extent(array)`: Extract element type from array + * - `std::meta::members_of(type)`: Get all members including typedefs + * - `std::meta::is_type(member)`: Check if member is a type (vs field) + * + * All operations execute at compile time in consteval contexts. + */ +#ifndef SIMDJSON_GENERIC_ONDEMAND_COMPILE_TIME_ACCESSORS_H + +/* amalgamation skipped (editor-only): #ifndef SIMDJSON_CONDITIONAL_INCLUDE */ +/* amalgamation skipped (editor-only): #define SIMDJSON_GENERIC_ONDEMAND_COMPILE_TIME_ACCESSORS_H */ +/* amalgamation skipped (editor-only): */ +/* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ + +// Arguably, we should just check SIMDJSON_STATIC_REFLECTION since it +// is unlikely that we will have reflection support without concepts support. +#if SIMDJSON_SUPPORTS_CONCEPTS && SIMDJSON_STATIC_REFLECTION + +#include +#include +#include + +namespace simdjson { +namespace haswell { +namespace ondemand { +/*** + * JSONPath implementation for compile-time access + * RFC 9535 JSONPath: Query Expressions for JSON, https://www.rfc-editor.org/rfc/rfc9535 + */ +namespace json_path { + +// Note: value type must be fully defined before this header is included +// This is ensured by including this in amalgamated.h after value-inl.h + +using ::simdjson::haswell::ondemand::value; + +// Path step types +enum class step_type { + field, // .field_name or ["field_name"] + array_index // [index] +}; + +// Represents a single step in a JSON path +template +struct path_step { + step_type type; + char key[N]; // Field name (empty for array indices) + std::size_t index; // Array index (0 for field access) + + constexpr path_step(step_type t, const char (&k)[N], std::size_t idx = 0) + : type(t), index(idx) { + for (std::size_t i = 0; i < N; ++i) { + key[i] = k[i]; + } + } + + constexpr std::string_view key_view() const { + return {key, N - 1}; + } +}; + +// Helper to create field step +template +consteval auto make_field_step(const char (&name)[N]) { + return path_step(step_type::field, name, 0); +} + +// Helper to create array index step +consteval auto make_index_step(std::size_t idx) { + return path_step<1>(step_type::array_index, "", idx); +} + +// Parse state for compile-time JSON path parsing +struct parse_result { + bool success; + std::size_t pos; + std::string_view error_msg; +}; + +// Compile-time JSON path parser +// Supports subset: .field, ["field"], [index], nested combinations +template +struct json_path_parser { + static constexpr std::string_view path_str = Path.view(); + + // Skip leading $ if present + static consteval std::size_t skip_root() { + if (!path_str.empty() && path_str[0] == '$') { + return 1; + } + return 0; + } + + // Count the number of steps in the path at compile time + static consteval std::size_t count_steps() { + std::size_t count = 0; + std::size_t i = skip_root(); + + while (i < path_str.size()) { + if (path_str[i] == '.') { + // Field access: .field + ++i; + if (i >= path_str.size()) break; + + // Skip field name + while (i < path_str.size() && path_str[i] != '.' && path_str[i] != '[') { + ++i; + } + ++count; + } else if (path_str[i] == '[') { + // Array or bracket notation + ++i; + if (i >= path_str.size()) break; + + if (path_str[i] == '"' || path_str[i] == '\'') { + // Field access: ["field"] or ['field'] + char quote = path_str[i]; + ++i; + while (i < path_str.size() && path_str[i] != quote) { + ++i; + } + if (i < path_str.size()) ++i; // skip closing quote + if (i < path_str.size() && path_str[i] == ']') ++i; + } else { + // Array index: [0], [123] + while (i < path_str.size() && path_str[i] != ']') { + ++i; + } + if (i < path_str.size()) ++i; // skip ] + } + ++count; + } else { + ++i; + } + } + + return count; + } + + // Parse a field name at compile time + static consteval std::size_t parse_field_name(std::size_t start, char* out, std::size_t max_len) { + std::size_t len = 0; + std::size_t i = start; + + while (i < path_str.size() && path_str[i] != '.' && path_str[i] != '[' && len < max_len - 1) { + out[len++] = path_str[i++]; + } + out[len] = '\0'; + return i; + } + + // Parse an array index at compile time + static consteval std::pair parse_array_index(std::size_t start) { + std::size_t index = 0; + std::size_t i = start; + + while (i < path_str.size() && path_str[i] >= '0' && path_str[i] <= '9') { + index = index * 10 + (path_str[i] - '0'); + ++i; + } + + return {i, index}; + } +}; + +// Compile-time path accessor generator +template +struct path_accessor { + using value = ::simdjson::haswell::ondemand::value; + + static constexpr auto parser = json_path_parser(); + static constexpr std::size_t num_steps = parser.count_steps(); + static constexpr std::string_view path_view = Path.view(); + + // Compile-time accessor generation + // If T is a struct, validates the path at compile time + // If T is void, skips validation + template + static inline simdjson_result access(DocOrValue& doc_or_val) noexcept { + // Validate path at compile time if T is a struct + if constexpr (std::is_class_v) { + constexpr bool path_valid = validate_path(); + static_assert(path_valid, "JSON path does not match struct definition"); + } + + // Parse the path at compile time to build access steps + return access_impl(doc_or_val.get_value()); + } + + // Extract value at path directly into target with compile-time type validation + // Example: std::string name; path_accessor::extract_field(doc, name); + template + static inline error_code extract_field(DocOrValue& doc_or_val, FieldType& target) noexcept { + static_assert(std::is_class_v, "extract_field requires T to be a struct type for validation"); + + // Validate path exists in struct definition + constexpr bool path_valid = validate_path(); + static_assert(path_valid, "JSON path does not match struct definition"); + + // Get the type at the end of the path + constexpr auto final_type = get_final_type(); + + // Verify target type matches the field type + static_assert(final_type == ^^FieldType, "Target type does not match the field type at the path"); + + // All validation done at compile time - just navigate and extract + auto json_value = access_impl(doc_or_val.get_value()); + if (json_value.error()) return json_value.error(); + + return json_value.get(target); + } + +private: + // Get the final type by walking the path through the struct type + template + static consteval std::enable_if_t, std::meta::info> get_final_type() { + auto current_type = ^^T; + std::size_t i = parser.skip_root(); + + while (i < path_view.size()) { + if (path_view[i] == '.') { + // .field syntax + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != '.' && path_view[i] != '[') { + ++i; + } + + std::string_view field_name = path_view.substr(field_start, i - field_start); + + auto members = std::meta::nonstatic_data_members_of( + current_type, std::meta::access_context::unchecked() + ); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == field_name) { + current_type = std::meta::type_of(mem); + break; + } + } + + } else if (path_view[i] == '[') { + ++i; + if (i >= path_view.size()) break; + + if (path_view[i] == '"' || path_view[i] == '\'') { + // ["field"] syntax + char quote = path_view[i]; + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != quote) { + ++i; + } + + std::string_view field_name = path_view.substr(field_start, i - field_start); + if (i < path_view.size()) ++i; // skip quote + if (i < path_view.size() && path_view[i] == ']') ++i; + + auto members = std::meta::nonstatic_data_members_of( + current_type, std::meta::access_context::unchecked() + ); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == field_name) { + current_type = std::meta::type_of(mem); + break; + } + } + + } else { + // [index] syntax - extract element type + while (i < path_view.size() && path_view[i] >= '0' && path_view[i] <= '9') { + ++i; + } + if (i < path_view.size() && path_view[i] == ']') ++i; + + current_type = get_element_type_reflected(current_type); + } + } else { + ++i; + } + } + + return current_type; + } + +private: + // Walk path and extract directly into final field using compile-time reflection + template + static inline error_code extract_with_reflection(simdjson_result current, TargetType& target_ref) noexcept { + if (current.error()) return current.error(); + + // Base case: end of path - extract into target + if constexpr (PathPos >= path_view.size()) { + return current.get(target_ref); + } + // Field access: .field_name + else if constexpr (path_view[PathPos] == '.') { + constexpr auto field_info = parse_next_field(PathPos); + constexpr std::string_view field_name = std::get<0>(field_info); + constexpr std::size_t next_pos = std::get<1>(field_info); + + constexpr auto member_info = find_member_by_name(CurrentType, field_name); + static_assert(member_info != ^^void, "Field not found in struct"); + + constexpr auto member_type = std::meta::type_of(member_info); + + auto obj_result = current.get_object(); + if (obj_result.error()) return obj_result.error(); + auto obj = obj_result.value_unsafe(); + auto field_value = obj.find_field_unordered(field_name); + + if constexpr (next_pos >= path_view.size()) { + return field_value.get(target_ref); + } else { + return extract_with_reflection(field_value, target_ref); + } + } + // Bracket notation: [index] or ["field"] + else if constexpr (path_view[PathPos] == '[') { + constexpr auto bracket_info = parse_bracket(PathPos); + constexpr bool is_field = std::get<0>(bracket_info); + constexpr std::size_t next_pos = std::get<2>(bracket_info); + + if constexpr (is_field) { + constexpr std::string_view field_name = std::get<1>(bracket_info); + constexpr auto member_info = find_member_by_name(CurrentType, field_name); + static_assert(member_info != ^^void, "Field not found in struct"); + constexpr auto member_type = std::meta::type_of(member_info); + + auto obj_result = current.get_object(); + if (obj_result.error()) return obj_result.error(); + auto obj = obj_result.value_unsafe(); + auto field_value = obj.find_field_unordered(field_name); + + if constexpr (next_pos >= path_view.size()) { + return field_value.get(target_ref); + } else { + return extract_with_reflection(field_value, target_ref); + } + } else { + constexpr std::size_t index = std::get<3>(bracket_info); + constexpr auto elem_type = get_element_type_reflected(CurrentType); + static_assert(elem_type != ^^void, "Could not determine array element type"); + + auto arr_result = current.get_array(); + if (arr_result.error()) return arr_result.error(); + auto arr = arr_result.value_unsafe(); + auto elem_value = arr.at(index); + + if constexpr (next_pos >= path_view.size()) { + return elem_value.get(target_ref); + } else { + return extract_with_reflection(elem_value, target_ref); + } + } + } + // Skip unexpected characters and continue + else { + return extract_with_reflection(current, target_ref); + } + } + + // Find member by name in reflected type + static consteval std::meta::info find_member_by_name(std::meta::info type_refl, std::string_view name) { + auto members = std::meta::nonstatic_data_members_of(type_refl, std::meta::access_context::unchecked()); + for (auto mem : members) { + if (std::meta::identifier_of(mem) == name) { + return mem; + } + } + } + + // Generate compile-time accessor code by walking the path + template + static inline simdjson_result access_impl(simdjson_result current) noexcept { + if (current.error()) return current; + + if constexpr (PathPos >= path_view.size()) { + return current; + } else if constexpr (path_view[PathPos] == '.') { + constexpr auto field_info = parse_next_field(PathPos); + constexpr std::string_view field_name = std::get<0>(field_info); + constexpr std::size_t next_pos = std::get<1>(field_info); + + auto obj_result = current.get_object(); + if (obj_result.error()) return obj_result.error(); + + auto obj = obj_result.value_unsafe(); + auto next_value = obj.find_field_unordered(field_name); + + return access_impl(next_value); + + } else if constexpr (path_view[PathPos] == '[') { + constexpr auto bracket_info = parse_bracket(PathPos); + constexpr bool is_field = std::get<0>(bracket_info); + constexpr std::size_t next_pos = std::get<2>(bracket_info); + + if constexpr (is_field) { + constexpr std::string_view field_name = std::get<1>(bracket_info); + + auto obj_result = current.get_object(); + if (obj_result.error()) return obj_result.error(); + + auto obj = obj_result.value_unsafe(); + auto next_value = obj.find_field_unordered(field_name); + + return access_impl(next_value); + + } else { + constexpr std::size_t index = std::get<3>(bracket_info); + + auto arr_result = current.get_array(); + if (arr_result.error()) return arr_result.error(); + + auto arr = arr_result.value_unsafe(); + auto next_value = arr.at(index); + + return access_impl(next_value); + } + } else { + return access_impl(current); + } + } + + // Parse next field name + static consteval auto parse_next_field(std::size_t start) { + std::size_t i = start + 1; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != '.' && path_view[i] != '[') { + ++i; + } + std::string_view field_name = path_view.substr(field_start, i - field_start); + return std::make_tuple(field_name, i); + } + + // Parse bracket notation: returns (is_field, field_name, next_pos, index) + static consteval auto parse_bracket(std::size_t start) { + std::size_t i = start + 1; // skip '[' + + if (i < path_view.size() && (path_view[i] == '"' || path_view[i] == '\'')) { + // Field access + char quote = path_view[i]; + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != quote) { + ++i; + } + std::string_view field_name = path_view.substr(field_start, i - field_start); + if (i < path_view.size()) ++i; // skip closing quote + if (i < path_view.size() && path_view[i] == ']') ++i; + + return std::make_tuple(true, field_name, i, std::size_t(0)); + } else { + // Array index + std::size_t index = 0; + while (i < path_view.size() && path_view[i] >= '0' && path_view[i] <= '9') { + index = index * 10 + (path_view[i] - '0'); + ++i; + } + if (i < path_view.size() && path_view[i] == ']') ++i; + + return std::make_tuple(false, std::string_view{}, i, index); + } + } + +public: + // Check if reflected type is array-like (C-style array or indexable container) + // Uses reflection to test: 1) std::meta::is_array_type() for C arrays + // 2) std::meta::substitute() to test concepts::indexable_container concept + static consteval bool is_array_like_reflected(std::meta::info type_reflection) { + if (std::meta::is_array_type(type_reflection)) { + return true; + } + + if (std::meta::can_substitute(^^concepts::indexable_container_v, {type_reflection})) { + return std::meta::extract(std::meta::substitute(^^concepts::indexable_container_v, {type_reflection})); + } + return false; + } + + // Extract element type from reflected array or container + // For C arrays: uses std::meta::remove_extent() + // For containers: finds value_type member using std::meta::members_of() + static consteval std::meta::info get_element_type_reflected(std::meta::info type_reflection) { + if (std::meta::is_array_type(type_reflection)) { + return std::meta::remove_extent(type_reflection); + } + + auto members = std::meta::members_of(type_reflection, std::meta::access_context::unchecked()); + for (auto mem : members) { + if (std::meta::is_type(mem)) { + auto name = std::meta::identifier_of(mem); + if (name == "value_type") { + return mem; + } + } + } + return ^^void; + } + +private: + // Check if type has member with given name + template + static consteval bool has_member(std::string_view member_name) { + constexpr auto members = std::meta::nonstatic_data_members_of(^^Type, std::meta::access_context::unchecked()); + for (auto mem : members) { + if (std::meta::identifier_of(mem) == member_name) { + return true; + } + } + return false; + } + + // Get type of member by name + template + static consteval auto get_member_type(std::string_view member_name) { + constexpr auto members = std::meta::nonstatic_data_members_of(^^Type, std::meta::access_context::unchecked()); + for (auto mem : members) { + if (std::meta::identifier_of(mem) == member_name) { + return std::meta::type_of(mem); + } + } + return ^^void; + } + + // Check if non-reflected type is array-like + template + static consteval bool is_container_type() { + using BaseType = std::remove_cvref_t; + if constexpr (requires { typename BaseType::value_type; }) { + return true; + } + if constexpr (std::is_array_v) { + return true; + } + return false; + } + + // Extract element type from non-reflected container + template + using extract_element_type = std::conditional_t< + requires { typename std::remove_cvref_t::value_type; }, + typename std::remove_cvref_t::value_type, + std::conditional_t< + std::is_array_v>, + std::remove_extent_t>, + void + > + >; + + // Validate path matches struct definition + static consteval bool validate_path() { + if constexpr (!std::is_class_v) { + return true; + } + + auto current_type = ^^T; + std::size_t i = parser.skip_root(); + + while (i < path_view.size()) { + if (path_view[i] == '.') { + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != '.' && path_view[i] != '[') { + ++i; + } + + std::string_view field_name = path_view.substr(field_start, i - field_start); + + bool found = false; + auto members = std::meta::nonstatic_data_members_of(current_type, std::meta::access_context::unchecked()); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == field_name) { + current_type = std::meta::type_of(mem); + found = true; + break; + } + } + + if (!found) { + return false; + } + + } else if (path_view[i] == '[') { + ++i; + if (i >= path_view.size()) return false; + + if (path_view[i] == '"' || path_view[i] == '\'') { + char quote = path_view[i]; + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != quote) { + ++i; + } + + std::string_view field_name = path_view.substr(field_start, i - field_start); + if (i < path_view.size()) ++i; + if (i < path_view.size() && path_view[i] == ']') ++i; + + bool found = false; + auto members = std::meta::nonstatic_data_members_of(current_type, std::meta::access_context::unchecked()); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == field_name) { + current_type = std::meta::type_of(mem); + found = true; + break; + } + } + + if (!found) { + return false; + } + + } else { + while (i < path_view.size() && path_view[i] >= '0' && path_view[i] <= '9') { + ++i; + } + + if (i < path_view.size() && path_view[i] == ']') ++i; + + if (!is_array_like_reflected(current_type)) { + return false; + } + + auto new_type = get_element_type_reflected(current_type); + + if (new_type == ^^void) { + return false; + } + + current_type = new_type; + } + } else { + ++i; + } + } + + return true; + } +}; + +// Compile-time path accessor with validation +template +inline simdjson_result<::simdjson::haswell::ondemand::value> at_path_compiled(DocOrValue& doc_or_val) noexcept { + using accessor = path_accessor; + return accessor::access(doc_or_val); +} + +// Overload without type parameter (no validation) +template +inline simdjson_result<::simdjson::haswell::ondemand::value> at_path_compiled(DocOrValue& doc_or_val) noexcept { + using accessor = path_accessor; + return accessor::access(doc_or_val); +} + +// ============================================================================ +// JSON Pointer Compile-Time Support (RFC 6901) +// ============================================================================ + +// JSON Pointer parser: /field/0/nested (slash-separated) +template +struct json_pointer_parser { + static constexpr std::string_view pointer_str = Pointer.view(); + + // Unescape token: ~0 -> ~, ~1 -> / + static consteval void unescape_token(std::string_view src, char* dest, std::size_t& out_len) { + out_len = 0; + for (std::size_t i = 0; i < src.size(); ++i) { + if (src[i] == '~' && i + 1 < src.size()) { + if (src[i + 1] == '0') { + dest[out_len++] = '~'; + ++i; + } else if (src[i + 1] == '1') { + dest[out_len++] = '/'; + ++i; + } else { + dest[out_len++] = src[i]; + } + } else { + dest[out_len++] = src[i]; + } + } + } + + // Check if token is numeric + static consteval bool is_numeric(std::string_view token) { + if (token.empty()) return false; + if (token[0] == '0' && token.size() > 1) return false; + for (char c : token) { + if (c < '0' || c > '9') return false; + } + return true; + } + + // Parse numeric token to index + static consteval std::size_t parse_index(std::string_view token) { + std::size_t result = 0; + for (char c : token) { + result = result * 10 + (c - '0'); + } + return result; + } + + // Count tokens in pointer + static consteval std::size_t count_tokens() { + if (pointer_str.empty() || pointer_str == "/") return 0; + + std::size_t count = 0; + std::size_t pos = pointer_str[0] == '/' ? 1 : 0; + + while (pos < pointer_str.size()) { + ++count; + std::size_t next_slash = pointer_str.find('/', pos); + if (next_slash == std::string_view::npos) break; + pos = next_slash + 1; + } + + return count; + } + + // Get Nth token + static consteval std::string_view get_token(std::size_t token_index) { + std::size_t pos = pointer_str[0] == '/' ? 1 : 0; + std::size_t current_token = 0; + + while (current_token < token_index) { + std::size_t next_slash = pointer_str.find('/', pos); + pos = next_slash + 1; + ++current_token; + } + + std::size_t token_end = pointer_str.find('/', pos); + if (token_end == std::string_view::npos) token_end = pointer_str.size(); + + return pointer_str.substr(pos, token_end - pos); + } +}; + +// JSON Pointer accessor +template +struct pointer_accessor { + using parser = json_pointer_parser; + static constexpr std::string_view pointer_view = Pointer.view(); + static constexpr std::size_t token_count = parser::count_tokens(); + + // Validate pointer against struct definition + static consteval bool validate_pointer() { + if constexpr (!std::is_class_v) { + return true; + } + + auto current_type = ^^T; + std::size_t pos = pointer_view[0] == '/' ? 1 : 0; + + while (pos < pointer_view.size()) { + // Extract token up to next / + std::size_t token_end = pointer_view.find('/', pos); + if (token_end == std::string_view::npos) token_end = pointer_view.size(); + + std::string_view token = pointer_view.substr(pos, token_end - pos); + + if (parser::is_numeric(token)) { + if (!path_accessor::is_array_like_reflected(current_type)) { + return false; + } + current_type = path_accessor::get_element_type_reflected(current_type); + } else { + bool found = false; + auto members = std::meta::nonstatic_data_members_of(current_type, std::meta::access_context::unchecked()); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == token) { + current_type = std::meta::type_of(mem); + found = true; + break; + } + } + + if (!found) return false; + } + + pos = token_end + 1; + } + + return true; + } + + // Recursive accessor + template + static inline simdjson_result access_impl(simdjson_result current) noexcept { + if constexpr (TokenIndex >= token_count) { + return current; + } else { + constexpr std::string_view token = parser::get_token(TokenIndex); + + if constexpr (parser::is_numeric(token)) { + constexpr std::size_t index = parser::parse_index(token); + auto arr = current.get_array().value_unsafe(); + auto next_value = arr.at(index); + return access_impl(next_value); + } else { + auto obj = current.get_object().value_unsafe(); + auto next_value = obj.find_field_unordered(token); + return access_impl(next_value); + } + } + } + + // Access JSON value at pointer + template + static inline simdjson_result access(DocOrValue& doc_or_val) noexcept { + if constexpr (std::is_class_v) { + constexpr bool pointer_valid = validate_pointer(); + static_assert(pointer_valid, "JSON Pointer does not match struct definition"); + } + + if (pointer_view.empty() || pointer_view == "/") { + if constexpr (requires { doc_or_val.get_value(); }) { + return doc_or_val.get_value(); + } else { + return doc_or_val; + } + } + + simdjson_result current = doc_or_val.get_value(); + return access_impl<0>(current); + } + + // Extract value at pointer directly into target with type validation + template + static inline error_code extract_field(DocOrValue& doc_or_val, FieldType& target) noexcept { + static_assert(std::is_class_v, "extract_field requires T to be a struct type for validation"); + + constexpr bool pointer_valid = validate_pointer(); + static_assert(pointer_valid, "JSON Pointer does not match struct definition"); + + constexpr auto final_type = get_final_type(); + static_assert(final_type == ^^FieldType, "Target type does not match the field type at the pointer"); + + simdjson_result current_value = doc_or_val.get_value(); + auto json_value = access_impl<0>(current_value); + if (json_value.error()) return json_value.error(); + + return json_value.get(target); + } + +private: + // Get final type by walking pointer through struct + template + static consteval std::enable_if_t, std::meta::info> get_final_type() { + auto current_type = ^^T; + std::size_t pos = pointer_view[0] == '/' ? 1 : 0; + + while (pos < pointer_view.size()) { + std::size_t token_end = pointer_view.find('/', pos); + if (token_end == std::string_view::npos) token_end = pointer_view.size(); + + std::string_view token = pointer_view.substr(pos, token_end - pos); + + if (parser::is_numeric(token)) { + current_type = path_accessor::get_element_type_reflected(current_type); + } else { + auto members = std::meta::nonstatic_data_members_of(current_type, std::meta::access_context::unchecked()); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == token) { + current_type = std::meta::type_of(mem); + break; + } + } + } + + pos = token_end + 1; + } + + return current_type; + } +}; + +// Compile-time JSON Pointer accessor with validation +template +inline simdjson_result<::simdjson::haswell::ondemand::value> at_pointer_compiled(DocOrValue& doc_or_val) noexcept { + using accessor = pointer_accessor; + return accessor::access(doc_or_val); +} + +// Overload without type parameter (no validation) +template +inline simdjson_result<::simdjson::haswell::ondemand::value> at_pointer_compiled(DocOrValue& doc_or_val) noexcept { + using accessor = pointer_accessor; + return accessor::access(doc_or_val); +} + +} // namespace json_path +} // namespace ondemand +} // namespace haswell +} // namespace simdjson + +#endif // SIMDJSON_SUPPORTS_CONCEPTS && SIMDJSON_STATIC_REFLECTION +#endif // SIMDJSON_GENERIC_ONDEMAND_COMPILE_TIME_ACCESSORS_H + +/* end file simdjson/generic/ondemand/compile_time_accessors.h for haswell */ + /* end file simdjson/generic/ondemand/amalgamated.h for haswell */ /* including simdjson/haswell/end.h: #include "simdjson/haswell/end.h" */ /* begin file simdjson/haswell/end.h */ @@ -75332,7 +78180,7 @@ class value { * The following code reads z, then y, then x, and thus will not retrieve x or y if fed the * JSON `{ "x": 1, "y": 2, "z": 3 }`: * - * ```c++ + * ```cpp * simdjson::ondemand::parser parser; * auto obj = parser.parse(R"( { "x": 1, "y": 2, "z": 3 } )"_padded); * double z = obj.find_field("z"); @@ -75705,7 +78553,7 @@ struct simdjson_result : public icelake::implementatio * The following code reads z, then y, then x, and thus will not retrieve x or y if fed the * JSON `{ "x": 1, "y": 2, "z": 3 }`: * - * ```c++ + * ```cpp * simdjson::ondemand::parser parser; * auto obj = parser.parse(R"( { "x": 1, "y": 2, "z": 3 } )"_padded); * double z = obj.find_field("z"); @@ -77592,7 +80440,7 @@ class array { * JSONPath queries that trivially convertible to JSON Pointer queries: key * names and array indices. * - * https://datatracker.ietf.org/doc/html/draft-normington-jsonpath-00 + * https://www.rfc-editor.org/rfc/rfc9535 (RFC 9535) * * @return The value associated with the given JSONPath expression, or: * - INVALID_JSON_POINTER if the JSONPath to JSON Pointer conversion fails @@ -78270,7 +81118,7 @@ class document { * The following code reads z, then y, then x, and thus will not retrieve x or y if fed the * JSON `{ "x": 1, "y": 2, "z": 3 }`: * - * ```c++ + * ```cpp * simdjson::ondemand::parser parser; * auto obj = parser.parse(R"( { "x": 1, "y": 2, "z": 3 } )"_padded); * double z = obj.find_field("z"); @@ -78564,7 +81412,7 @@ class document { * JSONPath queries that trivially convertible to JSON Pointer queries: key * names and array indices. * - * https://datatracker.ietf.org/doc/html/draft-normington-jsonpath-00 + * https://www.rfc-editor.org/rfc/rfc9535 (RFC 9535) * * Key values are matched exactly, without unescaping or Unicode normalization. * We do a byte-by-byte comparison. E.g. @@ -78597,7 +81445,7 @@ class document { * potentially improving performance by skipping unwanted fields. * * Example: - * ```c++ + * ```cpp * struct Car { * std::string make; * std::string model; @@ -79061,7 +81909,7 @@ class document_stream { /** * Construct an uninitialized document_stream. * - * ```c++ + * ```cpp * document_stream docs; * auto error = parser.iterate_many(json).get(docs); * ``` @@ -79478,7 +82326,7 @@ class object { * The following code reads z, then y, then x, and thus will not retrieve x or y if fed the * JSON `{ "x": 1, "y": 2, "z": 3 }`: * - * ```c++ + * ```cpp * simdjson::ondemand::parser parser; * auto obj = parser.parse(R"( { "x": 1, "y": 2, "z": 3 } )"_padded); * double z = obj.find_field("z"); @@ -79687,7 +82535,7 @@ class object { * potentially improving performance by skipping unwanted fields. * * Example: - * ```c++ + * ```cpp * struct Car { * std::string make; * std::string model; @@ -86669,9 +89517,9 @@ simdjson_inline void string_builder::append(number_type v) noexcept { pv = 0 - pv; // the 0 is for Microsoft } size_t dc = internal::digit_count(pv); - if (negative) { - buffer.get()[position++] = '-'; - } + // by always writing the minus sign, we avoid the branch. + buffer.get()[position] = '-'; + position += negative ? 1 : 0; char *write_pointer = buffer.get() + position + dc - 1; while (pv >= 100) { memcpy(write_pointer - 1, &internal::decimal_table[(pv % 100) * 2], 2); @@ -87340,6 +90188,949 @@ simdjson_warn_unused simdjson_result extract_from(const T &obj, siz #endif /* end file simdjson/generic/ondemand/json_builder.h for icelake */ +// JSON path accessor (compile-time) - must be after inline definitions +/* including simdjson/generic/ondemand/compile_time_accessors.h for icelake: #include "simdjson/generic/ondemand/compile_time_accessors.h" */ +/* begin file simdjson/generic/ondemand/compile_time_accessors.h for icelake */ +/** + * Compile-time JSON Path and JSON Pointer accessors using C++26 reflection (P2996) + * + * This file validates JSON paths/pointers against struct definitions at compile time + * and generates optimized accessor code with zero runtime overhead. + * + * ## How It Works + * + * **Compile Time**: Path is parsed, validated against struct, types are checked + * **Runtime**: Direct navigation with no parsing or validation overhead + * + * Example: + * ```cpp + * struct User { std::string name; std::vector emails; }; + * + * std::string email; + * path_accessor::extract_field(doc, email); + * + * // Compile time validates: + * // 1. User has "emails" field + * // 2. "emails" is array-like + * // 3. Element type is std::string + * // 4. static_assert(^^std::string == ^^std::string) + * + * // Runtime just navigates: + * // doc.get_object().find_field("emails").get_array().at(0).get(email) + * ``` + * + * ## Key Reflection APIs + * + * - `^^Type`: Reflect operator, converts type to std::meta::info + * - `std::meta::nonstatic_data_members_of(type)`: Get all fields of a struct + * - `std::meta::identifier_of(member)`: Get field name as string_view + * - `std::meta::type_of(member)`: Get reflected type of a field + * - `std::meta::is_array_type(type)`: Check if C-style array + * - `std::meta::remove_extent(array)`: Extract element type from array + * - `std::meta::members_of(type)`: Get all members including typedefs + * - `std::meta::is_type(member)`: Check if member is a type (vs field) + * + * All operations execute at compile time in consteval contexts. + */ +#ifndef SIMDJSON_GENERIC_ONDEMAND_COMPILE_TIME_ACCESSORS_H + +/* amalgamation skipped (editor-only): #ifndef SIMDJSON_CONDITIONAL_INCLUDE */ +/* amalgamation skipped (editor-only): #define SIMDJSON_GENERIC_ONDEMAND_COMPILE_TIME_ACCESSORS_H */ +/* amalgamation skipped (editor-only): */ +/* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ + +// Arguably, we should just check SIMDJSON_STATIC_REFLECTION since it +// is unlikely that we will have reflection support without concepts support. +#if SIMDJSON_SUPPORTS_CONCEPTS && SIMDJSON_STATIC_REFLECTION + +#include +#include +#include + +namespace simdjson { +namespace icelake { +namespace ondemand { +/*** + * JSONPath implementation for compile-time access + * RFC 9535 JSONPath: Query Expressions for JSON, https://www.rfc-editor.org/rfc/rfc9535 + */ +namespace json_path { + +// Note: value type must be fully defined before this header is included +// This is ensured by including this in amalgamated.h after value-inl.h + +using ::simdjson::icelake::ondemand::value; + +// Path step types +enum class step_type { + field, // .field_name or ["field_name"] + array_index // [index] +}; + +// Represents a single step in a JSON path +template +struct path_step { + step_type type; + char key[N]; // Field name (empty for array indices) + std::size_t index; // Array index (0 for field access) + + constexpr path_step(step_type t, const char (&k)[N], std::size_t idx = 0) + : type(t), index(idx) { + for (std::size_t i = 0; i < N; ++i) { + key[i] = k[i]; + } + } + + constexpr std::string_view key_view() const { + return {key, N - 1}; + } +}; + +// Helper to create field step +template +consteval auto make_field_step(const char (&name)[N]) { + return path_step(step_type::field, name, 0); +} + +// Helper to create array index step +consteval auto make_index_step(std::size_t idx) { + return path_step<1>(step_type::array_index, "", idx); +} + +// Parse state for compile-time JSON path parsing +struct parse_result { + bool success; + std::size_t pos; + std::string_view error_msg; +}; + +// Compile-time JSON path parser +// Supports subset: .field, ["field"], [index], nested combinations +template +struct json_path_parser { + static constexpr std::string_view path_str = Path.view(); + + // Skip leading $ if present + static consteval std::size_t skip_root() { + if (!path_str.empty() && path_str[0] == '$') { + return 1; + } + return 0; + } + + // Count the number of steps in the path at compile time + static consteval std::size_t count_steps() { + std::size_t count = 0; + std::size_t i = skip_root(); + + while (i < path_str.size()) { + if (path_str[i] == '.') { + // Field access: .field + ++i; + if (i >= path_str.size()) break; + + // Skip field name + while (i < path_str.size() && path_str[i] != '.' && path_str[i] != '[') { + ++i; + } + ++count; + } else if (path_str[i] == '[') { + // Array or bracket notation + ++i; + if (i >= path_str.size()) break; + + if (path_str[i] == '"' || path_str[i] == '\'') { + // Field access: ["field"] or ['field'] + char quote = path_str[i]; + ++i; + while (i < path_str.size() && path_str[i] != quote) { + ++i; + } + if (i < path_str.size()) ++i; // skip closing quote + if (i < path_str.size() && path_str[i] == ']') ++i; + } else { + // Array index: [0], [123] + while (i < path_str.size() && path_str[i] != ']') { + ++i; + } + if (i < path_str.size()) ++i; // skip ] + } + ++count; + } else { + ++i; + } + } + + return count; + } + + // Parse a field name at compile time + static consteval std::size_t parse_field_name(std::size_t start, char* out, std::size_t max_len) { + std::size_t len = 0; + std::size_t i = start; + + while (i < path_str.size() && path_str[i] != '.' && path_str[i] != '[' && len < max_len - 1) { + out[len++] = path_str[i++]; + } + out[len] = '\0'; + return i; + } + + // Parse an array index at compile time + static consteval std::pair parse_array_index(std::size_t start) { + std::size_t index = 0; + std::size_t i = start; + + while (i < path_str.size() && path_str[i] >= '0' && path_str[i] <= '9') { + index = index * 10 + (path_str[i] - '0'); + ++i; + } + + return {i, index}; + } +}; + +// Compile-time path accessor generator +template +struct path_accessor { + using value = ::simdjson::icelake::ondemand::value; + + static constexpr auto parser = json_path_parser(); + static constexpr std::size_t num_steps = parser.count_steps(); + static constexpr std::string_view path_view = Path.view(); + + // Compile-time accessor generation + // If T is a struct, validates the path at compile time + // If T is void, skips validation + template + static inline simdjson_result access(DocOrValue& doc_or_val) noexcept { + // Validate path at compile time if T is a struct + if constexpr (std::is_class_v) { + constexpr bool path_valid = validate_path(); + static_assert(path_valid, "JSON path does not match struct definition"); + } + + // Parse the path at compile time to build access steps + return access_impl(doc_or_val.get_value()); + } + + // Extract value at path directly into target with compile-time type validation + // Example: std::string name; path_accessor::extract_field(doc, name); + template + static inline error_code extract_field(DocOrValue& doc_or_val, FieldType& target) noexcept { + static_assert(std::is_class_v, "extract_field requires T to be a struct type for validation"); + + // Validate path exists in struct definition + constexpr bool path_valid = validate_path(); + static_assert(path_valid, "JSON path does not match struct definition"); + + // Get the type at the end of the path + constexpr auto final_type = get_final_type(); + + // Verify target type matches the field type + static_assert(final_type == ^^FieldType, "Target type does not match the field type at the path"); + + // All validation done at compile time - just navigate and extract + auto json_value = access_impl(doc_or_val.get_value()); + if (json_value.error()) return json_value.error(); + + return json_value.get(target); + } + +private: + // Get the final type by walking the path through the struct type + template + static consteval std::enable_if_t, std::meta::info> get_final_type() { + auto current_type = ^^T; + std::size_t i = parser.skip_root(); + + while (i < path_view.size()) { + if (path_view[i] == '.') { + // .field syntax + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != '.' && path_view[i] != '[') { + ++i; + } + + std::string_view field_name = path_view.substr(field_start, i - field_start); + + auto members = std::meta::nonstatic_data_members_of( + current_type, std::meta::access_context::unchecked() + ); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == field_name) { + current_type = std::meta::type_of(mem); + break; + } + } + + } else if (path_view[i] == '[') { + ++i; + if (i >= path_view.size()) break; + + if (path_view[i] == '"' || path_view[i] == '\'') { + // ["field"] syntax + char quote = path_view[i]; + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != quote) { + ++i; + } + + std::string_view field_name = path_view.substr(field_start, i - field_start); + if (i < path_view.size()) ++i; // skip quote + if (i < path_view.size() && path_view[i] == ']') ++i; + + auto members = std::meta::nonstatic_data_members_of( + current_type, std::meta::access_context::unchecked() + ); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == field_name) { + current_type = std::meta::type_of(mem); + break; + } + } + + } else { + // [index] syntax - extract element type + while (i < path_view.size() && path_view[i] >= '0' && path_view[i] <= '9') { + ++i; + } + if (i < path_view.size() && path_view[i] == ']') ++i; + + current_type = get_element_type_reflected(current_type); + } + } else { + ++i; + } + } + + return current_type; + } + +private: + // Walk path and extract directly into final field using compile-time reflection + template + static inline error_code extract_with_reflection(simdjson_result current, TargetType& target_ref) noexcept { + if (current.error()) return current.error(); + + // Base case: end of path - extract into target + if constexpr (PathPos >= path_view.size()) { + return current.get(target_ref); + } + // Field access: .field_name + else if constexpr (path_view[PathPos] == '.') { + constexpr auto field_info = parse_next_field(PathPos); + constexpr std::string_view field_name = std::get<0>(field_info); + constexpr std::size_t next_pos = std::get<1>(field_info); + + constexpr auto member_info = find_member_by_name(CurrentType, field_name); + static_assert(member_info != ^^void, "Field not found in struct"); + + constexpr auto member_type = std::meta::type_of(member_info); + + auto obj_result = current.get_object(); + if (obj_result.error()) return obj_result.error(); + auto obj = obj_result.value_unsafe(); + auto field_value = obj.find_field_unordered(field_name); + + if constexpr (next_pos >= path_view.size()) { + return field_value.get(target_ref); + } else { + return extract_with_reflection(field_value, target_ref); + } + } + // Bracket notation: [index] or ["field"] + else if constexpr (path_view[PathPos] == '[') { + constexpr auto bracket_info = parse_bracket(PathPos); + constexpr bool is_field = std::get<0>(bracket_info); + constexpr std::size_t next_pos = std::get<2>(bracket_info); + + if constexpr (is_field) { + constexpr std::string_view field_name = std::get<1>(bracket_info); + constexpr auto member_info = find_member_by_name(CurrentType, field_name); + static_assert(member_info != ^^void, "Field not found in struct"); + constexpr auto member_type = std::meta::type_of(member_info); + + auto obj_result = current.get_object(); + if (obj_result.error()) return obj_result.error(); + auto obj = obj_result.value_unsafe(); + auto field_value = obj.find_field_unordered(field_name); + + if constexpr (next_pos >= path_view.size()) { + return field_value.get(target_ref); + } else { + return extract_with_reflection(field_value, target_ref); + } + } else { + constexpr std::size_t index = std::get<3>(bracket_info); + constexpr auto elem_type = get_element_type_reflected(CurrentType); + static_assert(elem_type != ^^void, "Could not determine array element type"); + + auto arr_result = current.get_array(); + if (arr_result.error()) return arr_result.error(); + auto arr = arr_result.value_unsafe(); + auto elem_value = arr.at(index); + + if constexpr (next_pos >= path_view.size()) { + return elem_value.get(target_ref); + } else { + return extract_with_reflection(elem_value, target_ref); + } + } + } + // Skip unexpected characters and continue + else { + return extract_with_reflection(current, target_ref); + } + } + + // Find member by name in reflected type + static consteval std::meta::info find_member_by_name(std::meta::info type_refl, std::string_view name) { + auto members = std::meta::nonstatic_data_members_of(type_refl, std::meta::access_context::unchecked()); + for (auto mem : members) { + if (std::meta::identifier_of(mem) == name) { + return mem; + } + } + } + + // Generate compile-time accessor code by walking the path + template + static inline simdjson_result access_impl(simdjson_result current) noexcept { + if (current.error()) return current; + + if constexpr (PathPos >= path_view.size()) { + return current; + } else if constexpr (path_view[PathPos] == '.') { + constexpr auto field_info = parse_next_field(PathPos); + constexpr std::string_view field_name = std::get<0>(field_info); + constexpr std::size_t next_pos = std::get<1>(field_info); + + auto obj_result = current.get_object(); + if (obj_result.error()) return obj_result.error(); + + auto obj = obj_result.value_unsafe(); + auto next_value = obj.find_field_unordered(field_name); + + return access_impl(next_value); + + } else if constexpr (path_view[PathPos] == '[') { + constexpr auto bracket_info = parse_bracket(PathPos); + constexpr bool is_field = std::get<0>(bracket_info); + constexpr std::size_t next_pos = std::get<2>(bracket_info); + + if constexpr (is_field) { + constexpr std::string_view field_name = std::get<1>(bracket_info); + + auto obj_result = current.get_object(); + if (obj_result.error()) return obj_result.error(); + + auto obj = obj_result.value_unsafe(); + auto next_value = obj.find_field_unordered(field_name); + + return access_impl(next_value); + + } else { + constexpr std::size_t index = std::get<3>(bracket_info); + + auto arr_result = current.get_array(); + if (arr_result.error()) return arr_result.error(); + + auto arr = arr_result.value_unsafe(); + auto next_value = arr.at(index); + + return access_impl(next_value); + } + } else { + return access_impl(current); + } + } + + // Parse next field name + static consteval auto parse_next_field(std::size_t start) { + std::size_t i = start + 1; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != '.' && path_view[i] != '[') { + ++i; + } + std::string_view field_name = path_view.substr(field_start, i - field_start); + return std::make_tuple(field_name, i); + } + + // Parse bracket notation: returns (is_field, field_name, next_pos, index) + static consteval auto parse_bracket(std::size_t start) { + std::size_t i = start + 1; // skip '[' + + if (i < path_view.size() && (path_view[i] == '"' || path_view[i] == '\'')) { + // Field access + char quote = path_view[i]; + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != quote) { + ++i; + } + std::string_view field_name = path_view.substr(field_start, i - field_start); + if (i < path_view.size()) ++i; // skip closing quote + if (i < path_view.size() && path_view[i] == ']') ++i; + + return std::make_tuple(true, field_name, i, std::size_t(0)); + } else { + // Array index + std::size_t index = 0; + while (i < path_view.size() && path_view[i] >= '0' && path_view[i] <= '9') { + index = index * 10 + (path_view[i] - '0'); + ++i; + } + if (i < path_view.size() && path_view[i] == ']') ++i; + + return std::make_tuple(false, std::string_view{}, i, index); + } + } + +public: + // Check if reflected type is array-like (C-style array or indexable container) + // Uses reflection to test: 1) std::meta::is_array_type() for C arrays + // 2) std::meta::substitute() to test concepts::indexable_container concept + static consteval bool is_array_like_reflected(std::meta::info type_reflection) { + if (std::meta::is_array_type(type_reflection)) { + return true; + } + + if (std::meta::can_substitute(^^concepts::indexable_container_v, {type_reflection})) { + return std::meta::extract(std::meta::substitute(^^concepts::indexable_container_v, {type_reflection})); + } + return false; + } + + // Extract element type from reflected array or container + // For C arrays: uses std::meta::remove_extent() + // For containers: finds value_type member using std::meta::members_of() + static consteval std::meta::info get_element_type_reflected(std::meta::info type_reflection) { + if (std::meta::is_array_type(type_reflection)) { + return std::meta::remove_extent(type_reflection); + } + + auto members = std::meta::members_of(type_reflection, std::meta::access_context::unchecked()); + for (auto mem : members) { + if (std::meta::is_type(mem)) { + auto name = std::meta::identifier_of(mem); + if (name == "value_type") { + return mem; + } + } + } + return ^^void; + } + +private: + // Check if type has member with given name + template + static consteval bool has_member(std::string_view member_name) { + constexpr auto members = std::meta::nonstatic_data_members_of(^^Type, std::meta::access_context::unchecked()); + for (auto mem : members) { + if (std::meta::identifier_of(mem) == member_name) { + return true; + } + } + return false; + } + + // Get type of member by name + template + static consteval auto get_member_type(std::string_view member_name) { + constexpr auto members = std::meta::nonstatic_data_members_of(^^Type, std::meta::access_context::unchecked()); + for (auto mem : members) { + if (std::meta::identifier_of(mem) == member_name) { + return std::meta::type_of(mem); + } + } + return ^^void; + } + + // Check if non-reflected type is array-like + template + static consteval bool is_container_type() { + using BaseType = std::remove_cvref_t; + if constexpr (requires { typename BaseType::value_type; }) { + return true; + } + if constexpr (std::is_array_v) { + return true; + } + return false; + } + + // Extract element type from non-reflected container + template + using extract_element_type = std::conditional_t< + requires { typename std::remove_cvref_t::value_type; }, + typename std::remove_cvref_t::value_type, + std::conditional_t< + std::is_array_v>, + std::remove_extent_t>, + void + > + >; + + // Validate path matches struct definition + static consteval bool validate_path() { + if constexpr (!std::is_class_v) { + return true; + } + + auto current_type = ^^T; + std::size_t i = parser.skip_root(); + + while (i < path_view.size()) { + if (path_view[i] == '.') { + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != '.' && path_view[i] != '[') { + ++i; + } + + std::string_view field_name = path_view.substr(field_start, i - field_start); + + bool found = false; + auto members = std::meta::nonstatic_data_members_of(current_type, std::meta::access_context::unchecked()); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == field_name) { + current_type = std::meta::type_of(mem); + found = true; + break; + } + } + + if (!found) { + return false; + } + + } else if (path_view[i] == '[') { + ++i; + if (i >= path_view.size()) return false; + + if (path_view[i] == '"' || path_view[i] == '\'') { + char quote = path_view[i]; + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != quote) { + ++i; + } + + std::string_view field_name = path_view.substr(field_start, i - field_start); + if (i < path_view.size()) ++i; + if (i < path_view.size() && path_view[i] == ']') ++i; + + bool found = false; + auto members = std::meta::nonstatic_data_members_of(current_type, std::meta::access_context::unchecked()); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == field_name) { + current_type = std::meta::type_of(mem); + found = true; + break; + } + } + + if (!found) { + return false; + } + + } else { + while (i < path_view.size() && path_view[i] >= '0' && path_view[i] <= '9') { + ++i; + } + + if (i < path_view.size() && path_view[i] == ']') ++i; + + if (!is_array_like_reflected(current_type)) { + return false; + } + + auto new_type = get_element_type_reflected(current_type); + + if (new_type == ^^void) { + return false; + } + + current_type = new_type; + } + } else { + ++i; + } + } + + return true; + } +}; + +// Compile-time path accessor with validation +template +inline simdjson_result<::simdjson::icelake::ondemand::value> at_path_compiled(DocOrValue& doc_or_val) noexcept { + using accessor = path_accessor; + return accessor::access(doc_or_val); +} + +// Overload without type parameter (no validation) +template +inline simdjson_result<::simdjson::icelake::ondemand::value> at_path_compiled(DocOrValue& doc_or_val) noexcept { + using accessor = path_accessor; + return accessor::access(doc_or_val); +} + +// ============================================================================ +// JSON Pointer Compile-Time Support (RFC 6901) +// ============================================================================ + +// JSON Pointer parser: /field/0/nested (slash-separated) +template +struct json_pointer_parser { + static constexpr std::string_view pointer_str = Pointer.view(); + + // Unescape token: ~0 -> ~, ~1 -> / + static consteval void unescape_token(std::string_view src, char* dest, std::size_t& out_len) { + out_len = 0; + for (std::size_t i = 0; i < src.size(); ++i) { + if (src[i] == '~' && i + 1 < src.size()) { + if (src[i + 1] == '0') { + dest[out_len++] = '~'; + ++i; + } else if (src[i + 1] == '1') { + dest[out_len++] = '/'; + ++i; + } else { + dest[out_len++] = src[i]; + } + } else { + dest[out_len++] = src[i]; + } + } + } + + // Check if token is numeric + static consteval bool is_numeric(std::string_view token) { + if (token.empty()) return false; + if (token[0] == '0' && token.size() > 1) return false; + for (char c : token) { + if (c < '0' || c > '9') return false; + } + return true; + } + + // Parse numeric token to index + static consteval std::size_t parse_index(std::string_view token) { + std::size_t result = 0; + for (char c : token) { + result = result * 10 + (c - '0'); + } + return result; + } + + // Count tokens in pointer + static consteval std::size_t count_tokens() { + if (pointer_str.empty() || pointer_str == "/") return 0; + + std::size_t count = 0; + std::size_t pos = pointer_str[0] == '/' ? 1 : 0; + + while (pos < pointer_str.size()) { + ++count; + std::size_t next_slash = pointer_str.find('/', pos); + if (next_slash == std::string_view::npos) break; + pos = next_slash + 1; + } + + return count; + } + + // Get Nth token + static consteval std::string_view get_token(std::size_t token_index) { + std::size_t pos = pointer_str[0] == '/' ? 1 : 0; + std::size_t current_token = 0; + + while (current_token < token_index) { + std::size_t next_slash = pointer_str.find('/', pos); + pos = next_slash + 1; + ++current_token; + } + + std::size_t token_end = pointer_str.find('/', pos); + if (token_end == std::string_view::npos) token_end = pointer_str.size(); + + return pointer_str.substr(pos, token_end - pos); + } +}; + +// JSON Pointer accessor +template +struct pointer_accessor { + using parser = json_pointer_parser; + static constexpr std::string_view pointer_view = Pointer.view(); + static constexpr std::size_t token_count = parser::count_tokens(); + + // Validate pointer against struct definition + static consteval bool validate_pointer() { + if constexpr (!std::is_class_v) { + return true; + } + + auto current_type = ^^T; + std::size_t pos = pointer_view[0] == '/' ? 1 : 0; + + while (pos < pointer_view.size()) { + // Extract token up to next / + std::size_t token_end = pointer_view.find('/', pos); + if (token_end == std::string_view::npos) token_end = pointer_view.size(); + + std::string_view token = pointer_view.substr(pos, token_end - pos); + + if (parser::is_numeric(token)) { + if (!path_accessor::is_array_like_reflected(current_type)) { + return false; + } + current_type = path_accessor::get_element_type_reflected(current_type); + } else { + bool found = false; + auto members = std::meta::nonstatic_data_members_of(current_type, std::meta::access_context::unchecked()); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == token) { + current_type = std::meta::type_of(mem); + found = true; + break; + } + } + + if (!found) return false; + } + + pos = token_end + 1; + } + + return true; + } + + // Recursive accessor + template + static inline simdjson_result access_impl(simdjson_result current) noexcept { + if constexpr (TokenIndex >= token_count) { + return current; + } else { + constexpr std::string_view token = parser::get_token(TokenIndex); + + if constexpr (parser::is_numeric(token)) { + constexpr std::size_t index = parser::parse_index(token); + auto arr = current.get_array().value_unsafe(); + auto next_value = arr.at(index); + return access_impl(next_value); + } else { + auto obj = current.get_object().value_unsafe(); + auto next_value = obj.find_field_unordered(token); + return access_impl(next_value); + } + } + } + + // Access JSON value at pointer + template + static inline simdjson_result access(DocOrValue& doc_or_val) noexcept { + if constexpr (std::is_class_v) { + constexpr bool pointer_valid = validate_pointer(); + static_assert(pointer_valid, "JSON Pointer does not match struct definition"); + } + + if (pointer_view.empty() || pointer_view == "/") { + if constexpr (requires { doc_or_val.get_value(); }) { + return doc_or_val.get_value(); + } else { + return doc_or_val; + } + } + + simdjson_result current = doc_or_val.get_value(); + return access_impl<0>(current); + } + + // Extract value at pointer directly into target with type validation + template + static inline error_code extract_field(DocOrValue& doc_or_val, FieldType& target) noexcept { + static_assert(std::is_class_v, "extract_field requires T to be a struct type for validation"); + + constexpr bool pointer_valid = validate_pointer(); + static_assert(pointer_valid, "JSON Pointer does not match struct definition"); + + constexpr auto final_type = get_final_type(); + static_assert(final_type == ^^FieldType, "Target type does not match the field type at the pointer"); + + simdjson_result current_value = doc_or_val.get_value(); + auto json_value = access_impl<0>(current_value); + if (json_value.error()) return json_value.error(); + + return json_value.get(target); + } + +private: + // Get final type by walking pointer through struct + template + static consteval std::enable_if_t, std::meta::info> get_final_type() { + auto current_type = ^^T; + std::size_t pos = pointer_view[0] == '/' ? 1 : 0; + + while (pos < pointer_view.size()) { + std::size_t token_end = pointer_view.find('/', pos); + if (token_end == std::string_view::npos) token_end = pointer_view.size(); + + std::string_view token = pointer_view.substr(pos, token_end - pos); + + if (parser::is_numeric(token)) { + current_type = path_accessor::get_element_type_reflected(current_type); + } else { + auto members = std::meta::nonstatic_data_members_of(current_type, std::meta::access_context::unchecked()); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == token) { + current_type = std::meta::type_of(mem); + break; + } + } + } + + pos = token_end + 1; + } + + return current_type; + } +}; + +// Compile-time JSON Pointer accessor with validation +template +inline simdjson_result<::simdjson::icelake::ondemand::value> at_pointer_compiled(DocOrValue& doc_or_val) noexcept { + using accessor = pointer_accessor; + return accessor::access(doc_or_val); +} + +// Overload without type parameter (no validation) +template +inline simdjson_result<::simdjson::icelake::ondemand::value> at_pointer_compiled(DocOrValue& doc_or_val) noexcept { + using accessor = pointer_accessor; + return accessor::access(doc_or_val); +} + +} // namespace json_path +} // namespace ondemand +} // namespace icelake +} // namespace simdjson + +#endif // SIMDJSON_SUPPORTS_CONCEPTS && SIMDJSON_STATIC_REFLECTION +#endif // SIMDJSON_GENERIC_ONDEMAND_COMPILE_TIME_ACCESSORS_H + +/* end file simdjson/generic/ondemand/compile_time_accessors.h for icelake */ + /* end file simdjson/generic/ondemand/amalgamated.h for icelake */ /* including simdjson/icelake/end.h: #include "simdjson/icelake/end.h" */ /* begin file simdjson/icelake/end.h */ @@ -89252,7 +93043,7 @@ class value { * The following code reads z, then y, then x, and thus will not retrieve x or y if fed the * JSON `{ "x": 1, "y": 2, "z": 3 }`: * - * ```c++ + * ```cpp * simdjson::ondemand::parser parser; * auto obj = parser.parse(R"( { "x": 1, "y": 2, "z": 3 } )"_padded); * double z = obj.find_field("z"); @@ -89625,7 +93416,7 @@ struct simdjson_result : public ppc64::implementation_si * The following code reads z, then y, then x, and thus will not retrieve x or y if fed the * JSON `{ "x": 1, "y": 2, "z": 3 }`: * - * ```c++ + * ```cpp * simdjson::ondemand::parser parser; * auto obj = parser.parse(R"( { "x": 1, "y": 2, "z": 3 } )"_padded); * double z = obj.find_field("z"); @@ -91512,7 +95303,7 @@ class array { * JSONPath queries that trivially convertible to JSON Pointer queries: key * names and array indices. * - * https://datatracker.ietf.org/doc/html/draft-normington-jsonpath-00 + * https://www.rfc-editor.org/rfc/rfc9535 (RFC 9535) * * @return The value associated with the given JSONPath expression, or: * - INVALID_JSON_POINTER if the JSONPath to JSON Pointer conversion fails @@ -92190,7 +95981,7 @@ class document { * The following code reads z, then y, then x, and thus will not retrieve x or y if fed the * JSON `{ "x": 1, "y": 2, "z": 3 }`: * - * ```c++ + * ```cpp * simdjson::ondemand::parser parser; * auto obj = parser.parse(R"( { "x": 1, "y": 2, "z": 3 } )"_padded); * double z = obj.find_field("z"); @@ -92484,7 +96275,7 @@ class document { * JSONPath queries that trivially convertible to JSON Pointer queries: key * names and array indices. * - * https://datatracker.ietf.org/doc/html/draft-normington-jsonpath-00 + * https://www.rfc-editor.org/rfc/rfc9535 (RFC 9535) * * Key values are matched exactly, without unescaping or Unicode normalization. * We do a byte-by-byte comparison. E.g. @@ -92517,7 +96308,7 @@ class document { * potentially improving performance by skipping unwanted fields. * * Example: - * ```c++ + * ```cpp * struct Car { * std::string make; * std::string model; @@ -92981,7 +96772,7 @@ class document_stream { /** * Construct an uninitialized document_stream. * - * ```c++ + * ```cpp * document_stream docs; * auto error = parser.iterate_many(json).get(docs); * ``` @@ -93398,7 +97189,7 @@ class object { * The following code reads z, then y, then x, and thus will not retrieve x or y if fed the * JSON `{ "x": 1, "y": 2, "z": 3 }`: * - * ```c++ + * ```cpp * simdjson::ondemand::parser parser; * auto obj = parser.parse(R"( { "x": 1, "y": 2, "z": 3 } )"_padded); * double z = obj.find_field("z"); @@ -93607,7 +97398,7 @@ class object { * potentially improving performance by skipping unwanted fields. * * Example: - * ```c++ + * ```cpp * struct Car { * std::string make; * std::string model; @@ -100589,9 +104380,9 @@ simdjson_inline void string_builder::append(number_type v) noexcept { pv = 0 - pv; // the 0 is for Microsoft } size_t dc = internal::digit_count(pv); - if (negative) { - buffer.get()[position++] = '-'; - } + // by always writing the minus sign, we avoid the branch. + buffer.get()[position] = '-'; + position += negative ? 1 : 0; char *write_pointer = buffer.get() + position + dc - 1; while (pv >= 100) { memcpy(write_pointer - 1, &internal::decimal_table[(pv % 100) * 2], 2); @@ -101260,6 +105051,949 @@ simdjson_warn_unused simdjson_result extract_from(const T &obj, siz #endif /* end file simdjson/generic/ondemand/json_builder.h for ppc64 */ +// JSON path accessor (compile-time) - must be after inline definitions +/* including simdjson/generic/ondemand/compile_time_accessors.h for ppc64: #include "simdjson/generic/ondemand/compile_time_accessors.h" */ +/* begin file simdjson/generic/ondemand/compile_time_accessors.h for ppc64 */ +/** + * Compile-time JSON Path and JSON Pointer accessors using C++26 reflection (P2996) + * + * This file validates JSON paths/pointers against struct definitions at compile time + * and generates optimized accessor code with zero runtime overhead. + * + * ## How It Works + * + * **Compile Time**: Path is parsed, validated against struct, types are checked + * **Runtime**: Direct navigation with no parsing or validation overhead + * + * Example: + * ```cpp + * struct User { std::string name; std::vector emails; }; + * + * std::string email; + * path_accessor::extract_field(doc, email); + * + * // Compile time validates: + * // 1. User has "emails" field + * // 2. "emails" is array-like + * // 3. Element type is std::string + * // 4. static_assert(^^std::string == ^^std::string) + * + * // Runtime just navigates: + * // doc.get_object().find_field("emails").get_array().at(0).get(email) + * ``` + * + * ## Key Reflection APIs + * + * - `^^Type`: Reflect operator, converts type to std::meta::info + * - `std::meta::nonstatic_data_members_of(type)`: Get all fields of a struct + * - `std::meta::identifier_of(member)`: Get field name as string_view + * - `std::meta::type_of(member)`: Get reflected type of a field + * - `std::meta::is_array_type(type)`: Check if C-style array + * - `std::meta::remove_extent(array)`: Extract element type from array + * - `std::meta::members_of(type)`: Get all members including typedefs + * - `std::meta::is_type(member)`: Check if member is a type (vs field) + * + * All operations execute at compile time in consteval contexts. + */ +#ifndef SIMDJSON_GENERIC_ONDEMAND_COMPILE_TIME_ACCESSORS_H + +/* amalgamation skipped (editor-only): #ifndef SIMDJSON_CONDITIONAL_INCLUDE */ +/* amalgamation skipped (editor-only): #define SIMDJSON_GENERIC_ONDEMAND_COMPILE_TIME_ACCESSORS_H */ +/* amalgamation skipped (editor-only): */ +/* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ + +// Arguably, we should just check SIMDJSON_STATIC_REFLECTION since it +// is unlikely that we will have reflection support without concepts support. +#if SIMDJSON_SUPPORTS_CONCEPTS && SIMDJSON_STATIC_REFLECTION + +#include +#include +#include + +namespace simdjson { +namespace ppc64 { +namespace ondemand { +/*** + * JSONPath implementation for compile-time access + * RFC 9535 JSONPath: Query Expressions for JSON, https://www.rfc-editor.org/rfc/rfc9535 + */ +namespace json_path { + +// Note: value type must be fully defined before this header is included +// This is ensured by including this in amalgamated.h after value-inl.h + +using ::simdjson::ppc64::ondemand::value; + +// Path step types +enum class step_type { + field, // .field_name or ["field_name"] + array_index // [index] +}; + +// Represents a single step in a JSON path +template +struct path_step { + step_type type; + char key[N]; // Field name (empty for array indices) + std::size_t index; // Array index (0 for field access) + + constexpr path_step(step_type t, const char (&k)[N], std::size_t idx = 0) + : type(t), index(idx) { + for (std::size_t i = 0; i < N; ++i) { + key[i] = k[i]; + } + } + + constexpr std::string_view key_view() const { + return {key, N - 1}; + } +}; + +// Helper to create field step +template +consteval auto make_field_step(const char (&name)[N]) { + return path_step(step_type::field, name, 0); +} + +// Helper to create array index step +consteval auto make_index_step(std::size_t idx) { + return path_step<1>(step_type::array_index, "", idx); +} + +// Parse state for compile-time JSON path parsing +struct parse_result { + bool success; + std::size_t pos; + std::string_view error_msg; +}; + +// Compile-time JSON path parser +// Supports subset: .field, ["field"], [index], nested combinations +template +struct json_path_parser { + static constexpr std::string_view path_str = Path.view(); + + // Skip leading $ if present + static consteval std::size_t skip_root() { + if (!path_str.empty() && path_str[0] == '$') { + return 1; + } + return 0; + } + + // Count the number of steps in the path at compile time + static consteval std::size_t count_steps() { + std::size_t count = 0; + std::size_t i = skip_root(); + + while (i < path_str.size()) { + if (path_str[i] == '.') { + // Field access: .field + ++i; + if (i >= path_str.size()) break; + + // Skip field name + while (i < path_str.size() && path_str[i] != '.' && path_str[i] != '[') { + ++i; + } + ++count; + } else if (path_str[i] == '[') { + // Array or bracket notation + ++i; + if (i >= path_str.size()) break; + + if (path_str[i] == '"' || path_str[i] == '\'') { + // Field access: ["field"] or ['field'] + char quote = path_str[i]; + ++i; + while (i < path_str.size() && path_str[i] != quote) { + ++i; + } + if (i < path_str.size()) ++i; // skip closing quote + if (i < path_str.size() && path_str[i] == ']') ++i; + } else { + // Array index: [0], [123] + while (i < path_str.size() && path_str[i] != ']') { + ++i; + } + if (i < path_str.size()) ++i; // skip ] + } + ++count; + } else { + ++i; + } + } + + return count; + } + + // Parse a field name at compile time + static consteval std::size_t parse_field_name(std::size_t start, char* out, std::size_t max_len) { + std::size_t len = 0; + std::size_t i = start; + + while (i < path_str.size() && path_str[i] != '.' && path_str[i] != '[' && len < max_len - 1) { + out[len++] = path_str[i++]; + } + out[len] = '\0'; + return i; + } + + // Parse an array index at compile time + static consteval std::pair parse_array_index(std::size_t start) { + std::size_t index = 0; + std::size_t i = start; + + while (i < path_str.size() && path_str[i] >= '0' && path_str[i] <= '9') { + index = index * 10 + (path_str[i] - '0'); + ++i; + } + + return {i, index}; + } +}; + +// Compile-time path accessor generator +template +struct path_accessor { + using value = ::simdjson::ppc64::ondemand::value; + + static constexpr auto parser = json_path_parser(); + static constexpr std::size_t num_steps = parser.count_steps(); + static constexpr std::string_view path_view = Path.view(); + + // Compile-time accessor generation + // If T is a struct, validates the path at compile time + // If T is void, skips validation + template + static inline simdjson_result access(DocOrValue& doc_or_val) noexcept { + // Validate path at compile time if T is a struct + if constexpr (std::is_class_v) { + constexpr bool path_valid = validate_path(); + static_assert(path_valid, "JSON path does not match struct definition"); + } + + // Parse the path at compile time to build access steps + return access_impl(doc_or_val.get_value()); + } + + // Extract value at path directly into target with compile-time type validation + // Example: std::string name; path_accessor::extract_field(doc, name); + template + static inline error_code extract_field(DocOrValue& doc_or_val, FieldType& target) noexcept { + static_assert(std::is_class_v, "extract_field requires T to be a struct type for validation"); + + // Validate path exists in struct definition + constexpr bool path_valid = validate_path(); + static_assert(path_valid, "JSON path does not match struct definition"); + + // Get the type at the end of the path + constexpr auto final_type = get_final_type(); + + // Verify target type matches the field type + static_assert(final_type == ^^FieldType, "Target type does not match the field type at the path"); + + // All validation done at compile time - just navigate and extract + auto json_value = access_impl(doc_or_val.get_value()); + if (json_value.error()) return json_value.error(); + + return json_value.get(target); + } + +private: + // Get the final type by walking the path through the struct type + template + static consteval std::enable_if_t, std::meta::info> get_final_type() { + auto current_type = ^^T; + std::size_t i = parser.skip_root(); + + while (i < path_view.size()) { + if (path_view[i] == '.') { + // .field syntax + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != '.' && path_view[i] != '[') { + ++i; + } + + std::string_view field_name = path_view.substr(field_start, i - field_start); + + auto members = std::meta::nonstatic_data_members_of( + current_type, std::meta::access_context::unchecked() + ); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == field_name) { + current_type = std::meta::type_of(mem); + break; + } + } + + } else if (path_view[i] == '[') { + ++i; + if (i >= path_view.size()) break; + + if (path_view[i] == '"' || path_view[i] == '\'') { + // ["field"] syntax + char quote = path_view[i]; + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != quote) { + ++i; + } + + std::string_view field_name = path_view.substr(field_start, i - field_start); + if (i < path_view.size()) ++i; // skip quote + if (i < path_view.size() && path_view[i] == ']') ++i; + + auto members = std::meta::nonstatic_data_members_of( + current_type, std::meta::access_context::unchecked() + ); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == field_name) { + current_type = std::meta::type_of(mem); + break; + } + } + + } else { + // [index] syntax - extract element type + while (i < path_view.size() && path_view[i] >= '0' && path_view[i] <= '9') { + ++i; + } + if (i < path_view.size() && path_view[i] == ']') ++i; + + current_type = get_element_type_reflected(current_type); + } + } else { + ++i; + } + } + + return current_type; + } + +private: + // Walk path and extract directly into final field using compile-time reflection + template + static inline error_code extract_with_reflection(simdjson_result current, TargetType& target_ref) noexcept { + if (current.error()) return current.error(); + + // Base case: end of path - extract into target + if constexpr (PathPos >= path_view.size()) { + return current.get(target_ref); + } + // Field access: .field_name + else if constexpr (path_view[PathPos] == '.') { + constexpr auto field_info = parse_next_field(PathPos); + constexpr std::string_view field_name = std::get<0>(field_info); + constexpr std::size_t next_pos = std::get<1>(field_info); + + constexpr auto member_info = find_member_by_name(CurrentType, field_name); + static_assert(member_info != ^^void, "Field not found in struct"); + + constexpr auto member_type = std::meta::type_of(member_info); + + auto obj_result = current.get_object(); + if (obj_result.error()) return obj_result.error(); + auto obj = obj_result.value_unsafe(); + auto field_value = obj.find_field_unordered(field_name); + + if constexpr (next_pos >= path_view.size()) { + return field_value.get(target_ref); + } else { + return extract_with_reflection(field_value, target_ref); + } + } + // Bracket notation: [index] or ["field"] + else if constexpr (path_view[PathPos] == '[') { + constexpr auto bracket_info = parse_bracket(PathPos); + constexpr bool is_field = std::get<0>(bracket_info); + constexpr std::size_t next_pos = std::get<2>(bracket_info); + + if constexpr (is_field) { + constexpr std::string_view field_name = std::get<1>(bracket_info); + constexpr auto member_info = find_member_by_name(CurrentType, field_name); + static_assert(member_info != ^^void, "Field not found in struct"); + constexpr auto member_type = std::meta::type_of(member_info); + + auto obj_result = current.get_object(); + if (obj_result.error()) return obj_result.error(); + auto obj = obj_result.value_unsafe(); + auto field_value = obj.find_field_unordered(field_name); + + if constexpr (next_pos >= path_view.size()) { + return field_value.get(target_ref); + } else { + return extract_with_reflection(field_value, target_ref); + } + } else { + constexpr std::size_t index = std::get<3>(bracket_info); + constexpr auto elem_type = get_element_type_reflected(CurrentType); + static_assert(elem_type != ^^void, "Could not determine array element type"); + + auto arr_result = current.get_array(); + if (arr_result.error()) return arr_result.error(); + auto arr = arr_result.value_unsafe(); + auto elem_value = arr.at(index); + + if constexpr (next_pos >= path_view.size()) { + return elem_value.get(target_ref); + } else { + return extract_with_reflection(elem_value, target_ref); + } + } + } + // Skip unexpected characters and continue + else { + return extract_with_reflection(current, target_ref); + } + } + + // Find member by name in reflected type + static consteval std::meta::info find_member_by_name(std::meta::info type_refl, std::string_view name) { + auto members = std::meta::nonstatic_data_members_of(type_refl, std::meta::access_context::unchecked()); + for (auto mem : members) { + if (std::meta::identifier_of(mem) == name) { + return mem; + } + } + } + + // Generate compile-time accessor code by walking the path + template + static inline simdjson_result access_impl(simdjson_result current) noexcept { + if (current.error()) return current; + + if constexpr (PathPos >= path_view.size()) { + return current; + } else if constexpr (path_view[PathPos] == '.') { + constexpr auto field_info = parse_next_field(PathPos); + constexpr std::string_view field_name = std::get<0>(field_info); + constexpr std::size_t next_pos = std::get<1>(field_info); + + auto obj_result = current.get_object(); + if (obj_result.error()) return obj_result.error(); + + auto obj = obj_result.value_unsafe(); + auto next_value = obj.find_field_unordered(field_name); + + return access_impl(next_value); + + } else if constexpr (path_view[PathPos] == '[') { + constexpr auto bracket_info = parse_bracket(PathPos); + constexpr bool is_field = std::get<0>(bracket_info); + constexpr std::size_t next_pos = std::get<2>(bracket_info); + + if constexpr (is_field) { + constexpr std::string_view field_name = std::get<1>(bracket_info); + + auto obj_result = current.get_object(); + if (obj_result.error()) return obj_result.error(); + + auto obj = obj_result.value_unsafe(); + auto next_value = obj.find_field_unordered(field_name); + + return access_impl(next_value); + + } else { + constexpr std::size_t index = std::get<3>(bracket_info); + + auto arr_result = current.get_array(); + if (arr_result.error()) return arr_result.error(); + + auto arr = arr_result.value_unsafe(); + auto next_value = arr.at(index); + + return access_impl(next_value); + } + } else { + return access_impl(current); + } + } + + // Parse next field name + static consteval auto parse_next_field(std::size_t start) { + std::size_t i = start + 1; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != '.' && path_view[i] != '[') { + ++i; + } + std::string_view field_name = path_view.substr(field_start, i - field_start); + return std::make_tuple(field_name, i); + } + + // Parse bracket notation: returns (is_field, field_name, next_pos, index) + static consteval auto parse_bracket(std::size_t start) { + std::size_t i = start + 1; // skip '[' + + if (i < path_view.size() && (path_view[i] == '"' || path_view[i] == '\'')) { + // Field access + char quote = path_view[i]; + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != quote) { + ++i; + } + std::string_view field_name = path_view.substr(field_start, i - field_start); + if (i < path_view.size()) ++i; // skip closing quote + if (i < path_view.size() && path_view[i] == ']') ++i; + + return std::make_tuple(true, field_name, i, std::size_t(0)); + } else { + // Array index + std::size_t index = 0; + while (i < path_view.size() && path_view[i] >= '0' && path_view[i] <= '9') { + index = index * 10 + (path_view[i] - '0'); + ++i; + } + if (i < path_view.size() && path_view[i] == ']') ++i; + + return std::make_tuple(false, std::string_view{}, i, index); + } + } + +public: + // Check if reflected type is array-like (C-style array or indexable container) + // Uses reflection to test: 1) std::meta::is_array_type() for C arrays + // 2) std::meta::substitute() to test concepts::indexable_container concept + static consteval bool is_array_like_reflected(std::meta::info type_reflection) { + if (std::meta::is_array_type(type_reflection)) { + return true; + } + + if (std::meta::can_substitute(^^concepts::indexable_container_v, {type_reflection})) { + return std::meta::extract(std::meta::substitute(^^concepts::indexable_container_v, {type_reflection})); + } + return false; + } + + // Extract element type from reflected array or container + // For C arrays: uses std::meta::remove_extent() + // For containers: finds value_type member using std::meta::members_of() + static consteval std::meta::info get_element_type_reflected(std::meta::info type_reflection) { + if (std::meta::is_array_type(type_reflection)) { + return std::meta::remove_extent(type_reflection); + } + + auto members = std::meta::members_of(type_reflection, std::meta::access_context::unchecked()); + for (auto mem : members) { + if (std::meta::is_type(mem)) { + auto name = std::meta::identifier_of(mem); + if (name == "value_type") { + return mem; + } + } + } + return ^^void; + } + +private: + // Check if type has member with given name + template + static consteval bool has_member(std::string_view member_name) { + constexpr auto members = std::meta::nonstatic_data_members_of(^^Type, std::meta::access_context::unchecked()); + for (auto mem : members) { + if (std::meta::identifier_of(mem) == member_name) { + return true; + } + } + return false; + } + + // Get type of member by name + template + static consteval auto get_member_type(std::string_view member_name) { + constexpr auto members = std::meta::nonstatic_data_members_of(^^Type, std::meta::access_context::unchecked()); + for (auto mem : members) { + if (std::meta::identifier_of(mem) == member_name) { + return std::meta::type_of(mem); + } + } + return ^^void; + } + + // Check if non-reflected type is array-like + template + static consteval bool is_container_type() { + using BaseType = std::remove_cvref_t; + if constexpr (requires { typename BaseType::value_type; }) { + return true; + } + if constexpr (std::is_array_v) { + return true; + } + return false; + } + + // Extract element type from non-reflected container + template + using extract_element_type = std::conditional_t< + requires { typename std::remove_cvref_t::value_type; }, + typename std::remove_cvref_t::value_type, + std::conditional_t< + std::is_array_v>, + std::remove_extent_t>, + void + > + >; + + // Validate path matches struct definition + static consteval bool validate_path() { + if constexpr (!std::is_class_v) { + return true; + } + + auto current_type = ^^T; + std::size_t i = parser.skip_root(); + + while (i < path_view.size()) { + if (path_view[i] == '.') { + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != '.' && path_view[i] != '[') { + ++i; + } + + std::string_view field_name = path_view.substr(field_start, i - field_start); + + bool found = false; + auto members = std::meta::nonstatic_data_members_of(current_type, std::meta::access_context::unchecked()); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == field_name) { + current_type = std::meta::type_of(mem); + found = true; + break; + } + } + + if (!found) { + return false; + } + + } else if (path_view[i] == '[') { + ++i; + if (i >= path_view.size()) return false; + + if (path_view[i] == '"' || path_view[i] == '\'') { + char quote = path_view[i]; + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != quote) { + ++i; + } + + std::string_view field_name = path_view.substr(field_start, i - field_start); + if (i < path_view.size()) ++i; + if (i < path_view.size() && path_view[i] == ']') ++i; + + bool found = false; + auto members = std::meta::nonstatic_data_members_of(current_type, std::meta::access_context::unchecked()); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == field_name) { + current_type = std::meta::type_of(mem); + found = true; + break; + } + } + + if (!found) { + return false; + } + + } else { + while (i < path_view.size() && path_view[i] >= '0' && path_view[i] <= '9') { + ++i; + } + + if (i < path_view.size() && path_view[i] == ']') ++i; + + if (!is_array_like_reflected(current_type)) { + return false; + } + + auto new_type = get_element_type_reflected(current_type); + + if (new_type == ^^void) { + return false; + } + + current_type = new_type; + } + } else { + ++i; + } + } + + return true; + } +}; + +// Compile-time path accessor with validation +template +inline simdjson_result<::simdjson::ppc64::ondemand::value> at_path_compiled(DocOrValue& doc_or_val) noexcept { + using accessor = path_accessor; + return accessor::access(doc_or_val); +} + +// Overload without type parameter (no validation) +template +inline simdjson_result<::simdjson::ppc64::ondemand::value> at_path_compiled(DocOrValue& doc_or_val) noexcept { + using accessor = path_accessor; + return accessor::access(doc_or_val); +} + +// ============================================================================ +// JSON Pointer Compile-Time Support (RFC 6901) +// ============================================================================ + +// JSON Pointer parser: /field/0/nested (slash-separated) +template +struct json_pointer_parser { + static constexpr std::string_view pointer_str = Pointer.view(); + + // Unescape token: ~0 -> ~, ~1 -> / + static consteval void unescape_token(std::string_view src, char* dest, std::size_t& out_len) { + out_len = 0; + for (std::size_t i = 0; i < src.size(); ++i) { + if (src[i] == '~' && i + 1 < src.size()) { + if (src[i + 1] == '0') { + dest[out_len++] = '~'; + ++i; + } else if (src[i + 1] == '1') { + dest[out_len++] = '/'; + ++i; + } else { + dest[out_len++] = src[i]; + } + } else { + dest[out_len++] = src[i]; + } + } + } + + // Check if token is numeric + static consteval bool is_numeric(std::string_view token) { + if (token.empty()) return false; + if (token[0] == '0' && token.size() > 1) return false; + for (char c : token) { + if (c < '0' || c > '9') return false; + } + return true; + } + + // Parse numeric token to index + static consteval std::size_t parse_index(std::string_view token) { + std::size_t result = 0; + for (char c : token) { + result = result * 10 + (c - '0'); + } + return result; + } + + // Count tokens in pointer + static consteval std::size_t count_tokens() { + if (pointer_str.empty() || pointer_str == "/") return 0; + + std::size_t count = 0; + std::size_t pos = pointer_str[0] == '/' ? 1 : 0; + + while (pos < pointer_str.size()) { + ++count; + std::size_t next_slash = pointer_str.find('/', pos); + if (next_slash == std::string_view::npos) break; + pos = next_slash + 1; + } + + return count; + } + + // Get Nth token + static consteval std::string_view get_token(std::size_t token_index) { + std::size_t pos = pointer_str[0] == '/' ? 1 : 0; + std::size_t current_token = 0; + + while (current_token < token_index) { + std::size_t next_slash = pointer_str.find('/', pos); + pos = next_slash + 1; + ++current_token; + } + + std::size_t token_end = pointer_str.find('/', pos); + if (token_end == std::string_view::npos) token_end = pointer_str.size(); + + return pointer_str.substr(pos, token_end - pos); + } +}; + +// JSON Pointer accessor +template +struct pointer_accessor { + using parser = json_pointer_parser; + static constexpr std::string_view pointer_view = Pointer.view(); + static constexpr std::size_t token_count = parser::count_tokens(); + + // Validate pointer against struct definition + static consteval bool validate_pointer() { + if constexpr (!std::is_class_v) { + return true; + } + + auto current_type = ^^T; + std::size_t pos = pointer_view[0] == '/' ? 1 : 0; + + while (pos < pointer_view.size()) { + // Extract token up to next / + std::size_t token_end = pointer_view.find('/', pos); + if (token_end == std::string_view::npos) token_end = pointer_view.size(); + + std::string_view token = pointer_view.substr(pos, token_end - pos); + + if (parser::is_numeric(token)) { + if (!path_accessor::is_array_like_reflected(current_type)) { + return false; + } + current_type = path_accessor::get_element_type_reflected(current_type); + } else { + bool found = false; + auto members = std::meta::nonstatic_data_members_of(current_type, std::meta::access_context::unchecked()); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == token) { + current_type = std::meta::type_of(mem); + found = true; + break; + } + } + + if (!found) return false; + } + + pos = token_end + 1; + } + + return true; + } + + // Recursive accessor + template + static inline simdjson_result access_impl(simdjson_result current) noexcept { + if constexpr (TokenIndex >= token_count) { + return current; + } else { + constexpr std::string_view token = parser::get_token(TokenIndex); + + if constexpr (parser::is_numeric(token)) { + constexpr std::size_t index = parser::parse_index(token); + auto arr = current.get_array().value_unsafe(); + auto next_value = arr.at(index); + return access_impl(next_value); + } else { + auto obj = current.get_object().value_unsafe(); + auto next_value = obj.find_field_unordered(token); + return access_impl(next_value); + } + } + } + + // Access JSON value at pointer + template + static inline simdjson_result access(DocOrValue& doc_or_val) noexcept { + if constexpr (std::is_class_v) { + constexpr bool pointer_valid = validate_pointer(); + static_assert(pointer_valid, "JSON Pointer does not match struct definition"); + } + + if (pointer_view.empty() || pointer_view == "/") { + if constexpr (requires { doc_or_val.get_value(); }) { + return doc_or_val.get_value(); + } else { + return doc_or_val; + } + } + + simdjson_result current = doc_or_val.get_value(); + return access_impl<0>(current); + } + + // Extract value at pointer directly into target with type validation + template + static inline error_code extract_field(DocOrValue& doc_or_val, FieldType& target) noexcept { + static_assert(std::is_class_v, "extract_field requires T to be a struct type for validation"); + + constexpr bool pointer_valid = validate_pointer(); + static_assert(pointer_valid, "JSON Pointer does not match struct definition"); + + constexpr auto final_type = get_final_type(); + static_assert(final_type == ^^FieldType, "Target type does not match the field type at the pointer"); + + simdjson_result current_value = doc_or_val.get_value(); + auto json_value = access_impl<0>(current_value); + if (json_value.error()) return json_value.error(); + + return json_value.get(target); + } + +private: + // Get final type by walking pointer through struct + template + static consteval std::enable_if_t, std::meta::info> get_final_type() { + auto current_type = ^^T; + std::size_t pos = pointer_view[0] == '/' ? 1 : 0; + + while (pos < pointer_view.size()) { + std::size_t token_end = pointer_view.find('/', pos); + if (token_end == std::string_view::npos) token_end = pointer_view.size(); + + std::string_view token = pointer_view.substr(pos, token_end - pos); + + if (parser::is_numeric(token)) { + current_type = path_accessor::get_element_type_reflected(current_type); + } else { + auto members = std::meta::nonstatic_data_members_of(current_type, std::meta::access_context::unchecked()); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == token) { + current_type = std::meta::type_of(mem); + break; + } + } + } + + pos = token_end + 1; + } + + return current_type; + } +}; + +// Compile-time JSON Pointer accessor with validation +template +inline simdjson_result<::simdjson::ppc64::ondemand::value> at_pointer_compiled(DocOrValue& doc_or_val) noexcept { + using accessor = pointer_accessor; + return accessor::access(doc_or_val); +} + +// Overload without type parameter (no validation) +template +inline simdjson_result<::simdjson::ppc64::ondemand::value> at_pointer_compiled(DocOrValue& doc_or_val) noexcept { + using accessor = pointer_accessor; + return accessor::access(doc_or_val); +} + +} // namespace json_path +} // namespace ondemand +} // namespace ppc64 +} // namespace simdjson + +#endif // SIMDJSON_SUPPORTS_CONCEPTS && SIMDJSON_STATIC_REFLECTION +#endif // SIMDJSON_GENERIC_ONDEMAND_COMPILE_TIME_ACCESSORS_H + +/* end file simdjson/generic/ondemand/compile_time_accessors.h for ppc64 */ + /* end file simdjson/generic/ondemand/amalgamated.h for ppc64 */ /* including simdjson/ppc64/end.h: #include "simdjson/ppc64/end.h" */ /* begin file simdjson/ppc64/end.h */ @@ -103488,7 +108222,7 @@ class value { * The following code reads z, then y, then x, and thus will not retrieve x or y if fed the * JSON `{ "x": 1, "y": 2, "z": 3 }`: * - * ```c++ + * ```cpp * simdjson::ondemand::parser parser; * auto obj = parser.parse(R"( { "x": 1, "y": 2, "z": 3 } )"_padded); * double z = obj.find_field("z"); @@ -103861,7 +108595,7 @@ struct simdjson_result : public westmere::implementat * The following code reads z, then y, then x, and thus will not retrieve x or y if fed the * JSON `{ "x": 1, "y": 2, "z": 3 }`: * - * ```c++ + * ```cpp * simdjson::ondemand::parser parser; * auto obj = parser.parse(R"( { "x": 1, "y": 2, "z": 3 } )"_padded); * double z = obj.find_field("z"); @@ -105748,7 +110482,7 @@ class array { * JSONPath queries that trivially convertible to JSON Pointer queries: key * names and array indices. * - * https://datatracker.ietf.org/doc/html/draft-normington-jsonpath-00 + * https://www.rfc-editor.org/rfc/rfc9535 (RFC 9535) * * @return The value associated with the given JSONPath expression, or: * - INVALID_JSON_POINTER if the JSONPath to JSON Pointer conversion fails @@ -106426,7 +111160,7 @@ class document { * The following code reads z, then y, then x, and thus will not retrieve x or y if fed the * JSON `{ "x": 1, "y": 2, "z": 3 }`: * - * ```c++ + * ```cpp * simdjson::ondemand::parser parser; * auto obj = parser.parse(R"( { "x": 1, "y": 2, "z": 3 } )"_padded); * double z = obj.find_field("z"); @@ -106720,7 +111454,7 @@ class document { * JSONPath queries that trivially convertible to JSON Pointer queries: key * names and array indices. * - * https://datatracker.ietf.org/doc/html/draft-normington-jsonpath-00 + * https://www.rfc-editor.org/rfc/rfc9535 (RFC 9535) * * Key values are matched exactly, without unescaping or Unicode normalization. * We do a byte-by-byte comparison. E.g. @@ -106753,7 +111487,7 @@ class document { * potentially improving performance by skipping unwanted fields. * * Example: - * ```c++ + * ```cpp * struct Car { * std::string make; * std::string model; @@ -107217,7 +111951,7 @@ class document_stream { /** * Construct an uninitialized document_stream. * - * ```c++ + * ```cpp * document_stream docs; * auto error = parser.iterate_many(json).get(docs); * ``` @@ -107634,7 +112368,7 @@ class object { * The following code reads z, then y, then x, and thus will not retrieve x or y if fed the * JSON `{ "x": 1, "y": 2, "z": 3 }`: * - * ```c++ + * ```cpp * simdjson::ondemand::parser parser; * auto obj = parser.parse(R"( { "x": 1, "y": 2, "z": 3 } )"_padded); * double z = obj.find_field("z"); @@ -107843,7 +112577,7 @@ class object { * potentially improving performance by skipping unwanted fields. * * Example: - * ```c++ + * ```cpp * struct Car { * std::string make; * std::string model; @@ -114825,9 +119559,9 @@ simdjson_inline void string_builder::append(number_type v) noexcept { pv = 0 - pv; // the 0 is for Microsoft } size_t dc = internal::digit_count(pv); - if (negative) { - buffer.get()[position++] = '-'; - } + // by always writing the minus sign, we avoid the branch. + buffer.get()[position] = '-'; + position += negative ? 1 : 0; char *write_pointer = buffer.get() + position + dc - 1; while (pv >= 100) { memcpy(write_pointer - 1, &internal::decimal_table[(pv % 100) * 2], 2); @@ -115496,6 +120230,949 @@ simdjson_warn_unused simdjson_result extract_from(const T &obj, siz #endif /* end file simdjson/generic/ondemand/json_builder.h for westmere */ +// JSON path accessor (compile-time) - must be after inline definitions +/* including simdjson/generic/ondemand/compile_time_accessors.h for westmere: #include "simdjson/generic/ondemand/compile_time_accessors.h" */ +/* begin file simdjson/generic/ondemand/compile_time_accessors.h for westmere */ +/** + * Compile-time JSON Path and JSON Pointer accessors using C++26 reflection (P2996) + * + * This file validates JSON paths/pointers against struct definitions at compile time + * and generates optimized accessor code with zero runtime overhead. + * + * ## How It Works + * + * **Compile Time**: Path is parsed, validated against struct, types are checked + * **Runtime**: Direct navigation with no parsing or validation overhead + * + * Example: + * ```cpp + * struct User { std::string name; std::vector emails; }; + * + * std::string email; + * path_accessor::extract_field(doc, email); + * + * // Compile time validates: + * // 1. User has "emails" field + * // 2. "emails" is array-like + * // 3. Element type is std::string + * // 4. static_assert(^^std::string == ^^std::string) + * + * // Runtime just navigates: + * // doc.get_object().find_field("emails").get_array().at(0).get(email) + * ``` + * + * ## Key Reflection APIs + * + * - `^^Type`: Reflect operator, converts type to std::meta::info + * - `std::meta::nonstatic_data_members_of(type)`: Get all fields of a struct + * - `std::meta::identifier_of(member)`: Get field name as string_view + * - `std::meta::type_of(member)`: Get reflected type of a field + * - `std::meta::is_array_type(type)`: Check if C-style array + * - `std::meta::remove_extent(array)`: Extract element type from array + * - `std::meta::members_of(type)`: Get all members including typedefs + * - `std::meta::is_type(member)`: Check if member is a type (vs field) + * + * All operations execute at compile time in consteval contexts. + */ +#ifndef SIMDJSON_GENERIC_ONDEMAND_COMPILE_TIME_ACCESSORS_H + +/* amalgamation skipped (editor-only): #ifndef SIMDJSON_CONDITIONAL_INCLUDE */ +/* amalgamation skipped (editor-only): #define SIMDJSON_GENERIC_ONDEMAND_COMPILE_TIME_ACCESSORS_H */ +/* amalgamation skipped (editor-only): */ +/* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ + +// Arguably, we should just check SIMDJSON_STATIC_REFLECTION since it +// is unlikely that we will have reflection support without concepts support. +#if SIMDJSON_SUPPORTS_CONCEPTS && SIMDJSON_STATIC_REFLECTION + +#include +#include +#include + +namespace simdjson { +namespace westmere { +namespace ondemand { +/*** + * JSONPath implementation for compile-time access + * RFC 9535 JSONPath: Query Expressions for JSON, https://www.rfc-editor.org/rfc/rfc9535 + */ +namespace json_path { + +// Note: value type must be fully defined before this header is included +// This is ensured by including this in amalgamated.h after value-inl.h + +using ::simdjson::westmere::ondemand::value; + +// Path step types +enum class step_type { + field, // .field_name or ["field_name"] + array_index // [index] +}; + +// Represents a single step in a JSON path +template +struct path_step { + step_type type; + char key[N]; // Field name (empty for array indices) + std::size_t index; // Array index (0 for field access) + + constexpr path_step(step_type t, const char (&k)[N], std::size_t idx = 0) + : type(t), index(idx) { + for (std::size_t i = 0; i < N; ++i) { + key[i] = k[i]; + } + } + + constexpr std::string_view key_view() const { + return {key, N - 1}; + } +}; + +// Helper to create field step +template +consteval auto make_field_step(const char (&name)[N]) { + return path_step(step_type::field, name, 0); +} + +// Helper to create array index step +consteval auto make_index_step(std::size_t idx) { + return path_step<1>(step_type::array_index, "", idx); +} + +// Parse state for compile-time JSON path parsing +struct parse_result { + bool success; + std::size_t pos; + std::string_view error_msg; +}; + +// Compile-time JSON path parser +// Supports subset: .field, ["field"], [index], nested combinations +template +struct json_path_parser { + static constexpr std::string_view path_str = Path.view(); + + // Skip leading $ if present + static consteval std::size_t skip_root() { + if (!path_str.empty() && path_str[0] == '$') { + return 1; + } + return 0; + } + + // Count the number of steps in the path at compile time + static consteval std::size_t count_steps() { + std::size_t count = 0; + std::size_t i = skip_root(); + + while (i < path_str.size()) { + if (path_str[i] == '.') { + // Field access: .field + ++i; + if (i >= path_str.size()) break; + + // Skip field name + while (i < path_str.size() && path_str[i] != '.' && path_str[i] != '[') { + ++i; + } + ++count; + } else if (path_str[i] == '[') { + // Array or bracket notation + ++i; + if (i >= path_str.size()) break; + + if (path_str[i] == '"' || path_str[i] == '\'') { + // Field access: ["field"] or ['field'] + char quote = path_str[i]; + ++i; + while (i < path_str.size() && path_str[i] != quote) { + ++i; + } + if (i < path_str.size()) ++i; // skip closing quote + if (i < path_str.size() && path_str[i] == ']') ++i; + } else { + // Array index: [0], [123] + while (i < path_str.size() && path_str[i] != ']') { + ++i; + } + if (i < path_str.size()) ++i; // skip ] + } + ++count; + } else { + ++i; + } + } + + return count; + } + + // Parse a field name at compile time + static consteval std::size_t parse_field_name(std::size_t start, char* out, std::size_t max_len) { + std::size_t len = 0; + std::size_t i = start; + + while (i < path_str.size() && path_str[i] != '.' && path_str[i] != '[' && len < max_len - 1) { + out[len++] = path_str[i++]; + } + out[len] = '\0'; + return i; + } + + // Parse an array index at compile time + static consteval std::pair parse_array_index(std::size_t start) { + std::size_t index = 0; + std::size_t i = start; + + while (i < path_str.size() && path_str[i] >= '0' && path_str[i] <= '9') { + index = index * 10 + (path_str[i] - '0'); + ++i; + } + + return {i, index}; + } +}; + +// Compile-time path accessor generator +template +struct path_accessor { + using value = ::simdjson::westmere::ondemand::value; + + static constexpr auto parser = json_path_parser(); + static constexpr std::size_t num_steps = parser.count_steps(); + static constexpr std::string_view path_view = Path.view(); + + // Compile-time accessor generation + // If T is a struct, validates the path at compile time + // If T is void, skips validation + template + static inline simdjson_result access(DocOrValue& doc_or_val) noexcept { + // Validate path at compile time if T is a struct + if constexpr (std::is_class_v) { + constexpr bool path_valid = validate_path(); + static_assert(path_valid, "JSON path does not match struct definition"); + } + + // Parse the path at compile time to build access steps + return access_impl(doc_or_val.get_value()); + } + + // Extract value at path directly into target with compile-time type validation + // Example: std::string name; path_accessor::extract_field(doc, name); + template + static inline error_code extract_field(DocOrValue& doc_or_val, FieldType& target) noexcept { + static_assert(std::is_class_v, "extract_field requires T to be a struct type for validation"); + + // Validate path exists in struct definition + constexpr bool path_valid = validate_path(); + static_assert(path_valid, "JSON path does not match struct definition"); + + // Get the type at the end of the path + constexpr auto final_type = get_final_type(); + + // Verify target type matches the field type + static_assert(final_type == ^^FieldType, "Target type does not match the field type at the path"); + + // All validation done at compile time - just navigate and extract + auto json_value = access_impl(doc_or_val.get_value()); + if (json_value.error()) return json_value.error(); + + return json_value.get(target); + } + +private: + // Get the final type by walking the path through the struct type + template + static consteval std::enable_if_t, std::meta::info> get_final_type() { + auto current_type = ^^T; + std::size_t i = parser.skip_root(); + + while (i < path_view.size()) { + if (path_view[i] == '.') { + // .field syntax + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != '.' && path_view[i] != '[') { + ++i; + } + + std::string_view field_name = path_view.substr(field_start, i - field_start); + + auto members = std::meta::nonstatic_data_members_of( + current_type, std::meta::access_context::unchecked() + ); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == field_name) { + current_type = std::meta::type_of(mem); + break; + } + } + + } else if (path_view[i] == '[') { + ++i; + if (i >= path_view.size()) break; + + if (path_view[i] == '"' || path_view[i] == '\'') { + // ["field"] syntax + char quote = path_view[i]; + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != quote) { + ++i; + } + + std::string_view field_name = path_view.substr(field_start, i - field_start); + if (i < path_view.size()) ++i; // skip quote + if (i < path_view.size() && path_view[i] == ']') ++i; + + auto members = std::meta::nonstatic_data_members_of( + current_type, std::meta::access_context::unchecked() + ); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == field_name) { + current_type = std::meta::type_of(mem); + break; + } + } + + } else { + // [index] syntax - extract element type + while (i < path_view.size() && path_view[i] >= '0' && path_view[i] <= '9') { + ++i; + } + if (i < path_view.size() && path_view[i] == ']') ++i; + + current_type = get_element_type_reflected(current_type); + } + } else { + ++i; + } + } + + return current_type; + } + +private: + // Walk path and extract directly into final field using compile-time reflection + template + static inline error_code extract_with_reflection(simdjson_result current, TargetType& target_ref) noexcept { + if (current.error()) return current.error(); + + // Base case: end of path - extract into target + if constexpr (PathPos >= path_view.size()) { + return current.get(target_ref); + } + // Field access: .field_name + else if constexpr (path_view[PathPos] == '.') { + constexpr auto field_info = parse_next_field(PathPos); + constexpr std::string_view field_name = std::get<0>(field_info); + constexpr std::size_t next_pos = std::get<1>(field_info); + + constexpr auto member_info = find_member_by_name(CurrentType, field_name); + static_assert(member_info != ^^void, "Field not found in struct"); + + constexpr auto member_type = std::meta::type_of(member_info); + + auto obj_result = current.get_object(); + if (obj_result.error()) return obj_result.error(); + auto obj = obj_result.value_unsafe(); + auto field_value = obj.find_field_unordered(field_name); + + if constexpr (next_pos >= path_view.size()) { + return field_value.get(target_ref); + } else { + return extract_with_reflection(field_value, target_ref); + } + } + // Bracket notation: [index] or ["field"] + else if constexpr (path_view[PathPos] == '[') { + constexpr auto bracket_info = parse_bracket(PathPos); + constexpr bool is_field = std::get<0>(bracket_info); + constexpr std::size_t next_pos = std::get<2>(bracket_info); + + if constexpr (is_field) { + constexpr std::string_view field_name = std::get<1>(bracket_info); + constexpr auto member_info = find_member_by_name(CurrentType, field_name); + static_assert(member_info != ^^void, "Field not found in struct"); + constexpr auto member_type = std::meta::type_of(member_info); + + auto obj_result = current.get_object(); + if (obj_result.error()) return obj_result.error(); + auto obj = obj_result.value_unsafe(); + auto field_value = obj.find_field_unordered(field_name); + + if constexpr (next_pos >= path_view.size()) { + return field_value.get(target_ref); + } else { + return extract_with_reflection(field_value, target_ref); + } + } else { + constexpr std::size_t index = std::get<3>(bracket_info); + constexpr auto elem_type = get_element_type_reflected(CurrentType); + static_assert(elem_type != ^^void, "Could not determine array element type"); + + auto arr_result = current.get_array(); + if (arr_result.error()) return arr_result.error(); + auto arr = arr_result.value_unsafe(); + auto elem_value = arr.at(index); + + if constexpr (next_pos >= path_view.size()) { + return elem_value.get(target_ref); + } else { + return extract_with_reflection(elem_value, target_ref); + } + } + } + // Skip unexpected characters and continue + else { + return extract_with_reflection(current, target_ref); + } + } + + // Find member by name in reflected type + static consteval std::meta::info find_member_by_name(std::meta::info type_refl, std::string_view name) { + auto members = std::meta::nonstatic_data_members_of(type_refl, std::meta::access_context::unchecked()); + for (auto mem : members) { + if (std::meta::identifier_of(mem) == name) { + return mem; + } + } + } + + // Generate compile-time accessor code by walking the path + template + static inline simdjson_result access_impl(simdjson_result current) noexcept { + if (current.error()) return current; + + if constexpr (PathPos >= path_view.size()) { + return current; + } else if constexpr (path_view[PathPos] == '.') { + constexpr auto field_info = parse_next_field(PathPos); + constexpr std::string_view field_name = std::get<0>(field_info); + constexpr std::size_t next_pos = std::get<1>(field_info); + + auto obj_result = current.get_object(); + if (obj_result.error()) return obj_result.error(); + + auto obj = obj_result.value_unsafe(); + auto next_value = obj.find_field_unordered(field_name); + + return access_impl(next_value); + + } else if constexpr (path_view[PathPos] == '[') { + constexpr auto bracket_info = parse_bracket(PathPos); + constexpr bool is_field = std::get<0>(bracket_info); + constexpr std::size_t next_pos = std::get<2>(bracket_info); + + if constexpr (is_field) { + constexpr std::string_view field_name = std::get<1>(bracket_info); + + auto obj_result = current.get_object(); + if (obj_result.error()) return obj_result.error(); + + auto obj = obj_result.value_unsafe(); + auto next_value = obj.find_field_unordered(field_name); + + return access_impl(next_value); + + } else { + constexpr std::size_t index = std::get<3>(bracket_info); + + auto arr_result = current.get_array(); + if (arr_result.error()) return arr_result.error(); + + auto arr = arr_result.value_unsafe(); + auto next_value = arr.at(index); + + return access_impl(next_value); + } + } else { + return access_impl(current); + } + } + + // Parse next field name + static consteval auto parse_next_field(std::size_t start) { + std::size_t i = start + 1; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != '.' && path_view[i] != '[') { + ++i; + } + std::string_view field_name = path_view.substr(field_start, i - field_start); + return std::make_tuple(field_name, i); + } + + // Parse bracket notation: returns (is_field, field_name, next_pos, index) + static consteval auto parse_bracket(std::size_t start) { + std::size_t i = start + 1; // skip '[' + + if (i < path_view.size() && (path_view[i] == '"' || path_view[i] == '\'')) { + // Field access + char quote = path_view[i]; + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != quote) { + ++i; + } + std::string_view field_name = path_view.substr(field_start, i - field_start); + if (i < path_view.size()) ++i; // skip closing quote + if (i < path_view.size() && path_view[i] == ']') ++i; + + return std::make_tuple(true, field_name, i, std::size_t(0)); + } else { + // Array index + std::size_t index = 0; + while (i < path_view.size() && path_view[i] >= '0' && path_view[i] <= '9') { + index = index * 10 + (path_view[i] - '0'); + ++i; + } + if (i < path_view.size() && path_view[i] == ']') ++i; + + return std::make_tuple(false, std::string_view{}, i, index); + } + } + +public: + // Check if reflected type is array-like (C-style array or indexable container) + // Uses reflection to test: 1) std::meta::is_array_type() for C arrays + // 2) std::meta::substitute() to test concepts::indexable_container concept + static consteval bool is_array_like_reflected(std::meta::info type_reflection) { + if (std::meta::is_array_type(type_reflection)) { + return true; + } + + if (std::meta::can_substitute(^^concepts::indexable_container_v, {type_reflection})) { + return std::meta::extract(std::meta::substitute(^^concepts::indexable_container_v, {type_reflection})); + } + return false; + } + + // Extract element type from reflected array or container + // For C arrays: uses std::meta::remove_extent() + // For containers: finds value_type member using std::meta::members_of() + static consteval std::meta::info get_element_type_reflected(std::meta::info type_reflection) { + if (std::meta::is_array_type(type_reflection)) { + return std::meta::remove_extent(type_reflection); + } + + auto members = std::meta::members_of(type_reflection, std::meta::access_context::unchecked()); + for (auto mem : members) { + if (std::meta::is_type(mem)) { + auto name = std::meta::identifier_of(mem); + if (name == "value_type") { + return mem; + } + } + } + return ^^void; + } + +private: + // Check if type has member with given name + template + static consteval bool has_member(std::string_view member_name) { + constexpr auto members = std::meta::nonstatic_data_members_of(^^Type, std::meta::access_context::unchecked()); + for (auto mem : members) { + if (std::meta::identifier_of(mem) == member_name) { + return true; + } + } + return false; + } + + // Get type of member by name + template + static consteval auto get_member_type(std::string_view member_name) { + constexpr auto members = std::meta::nonstatic_data_members_of(^^Type, std::meta::access_context::unchecked()); + for (auto mem : members) { + if (std::meta::identifier_of(mem) == member_name) { + return std::meta::type_of(mem); + } + } + return ^^void; + } + + // Check if non-reflected type is array-like + template + static consteval bool is_container_type() { + using BaseType = std::remove_cvref_t; + if constexpr (requires { typename BaseType::value_type; }) { + return true; + } + if constexpr (std::is_array_v) { + return true; + } + return false; + } + + // Extract element type from non-reflected container + template + using extract_element_type = std::conditional_t< + requires { typename std::remove_cvref_t::value_type; }, + typename std::remove_cvref_t::value_type, + std::conditional_t< + std::is_array_v>, + std::remove_extent_t>, + void + > + >; + + // Validate path matches struct definition + static consteval bool validate_path() { + if constexpr (!std::is_class_v) { + return true; + } + + auto current_type = ^^T; + std::size_t i = parser.skip_root(); + + while (i < path_view.size()) { + if (path_view[i] == '.') { + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != '.' && path_view[i] != '[') { + ++i; + } + + std::string_view field_name = path_view.substr(field_start, i - field_start); + + bool found = false; + auto members = std::meta::nonstatic_data_members_of(current_type, std::meta::access_context::unchecked()); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == field_name) { + current_type = std::meta::type_of(mem); + found = true; + break; + } + } + + if (!found) { + return false; + } + + } else if (path_view[i] == '[') { + ++i; + if (i >= path_view.size()) return false; + + if (path_view[i] == '"' || path_view[i] == '\'') { + char quote = path_view[i]; + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != quote) { + ++i; + } + + std::string_view field_name = path_view.substr(field_start, i - field_start); + if (i < path_view.size()) ++i; + if (i < path_view.size() && path_view[i] == ']') ++i; + + bool found = false; + auto members = std::meta::nonstatic_data_members_of(current_type, std::meta::access_context::unchecked()); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == field_name) { + current_type = std::meta::type_of(mem); + found = true; + break; + } + } + + if (!found) { + return false; + } + + } else { + while (i < path_view.size() && path_view[i] >= '0' && path_view[i] <= '9') { + ++i; + } + + if (i < path_view.size() && path_view[i] == ']') ++i; + + if (!is_array_like_reflected(current_type)) { + return false; + } + + auto new_type = get_element_type_reflected(current_type); + + if (new_type == ^^void) { + return false; + } + + current_type = new_type; + } + } else { + ++i; + } + } + + return true; + } +}; + +// Compile-time path accessor with validation +template +inline simdjson_result<::simdjson::westmere::ondemand::value> at_path_compiled(DocOrValue& doc_or_val) noexcept { + using accessor = path_accessor; + return accessor::access(doc_or_val); +} + +// Overload without type parameter (no validation) +template +inline simdjson_result<::simdjson::westmere::ondemand::value> at_path_compiled(DocOrValue& doc_or_val) noexcept { + using accessor = path_accessor; + return accessor::access(doc_or_val); +} + +// ============================================================================ +// JSON Pointer Compile-Time Support (RFC 6901) +// ============================================================================ + +// JSON Pointer parser: /field/0/nested (slash-separated) +template +struct json_pointer_parser { + static constexpr std::string_view pointer_str = Pointer.view(); + + // Unescape token: ~0 -> ~, ~1 -> / + static consteval void unescape_token(std::string_view src, char* dest, std::size_t& out_len) { + out_len = 0; + for (std::size_t i = 0; i < src.size(); ++i) { + if (src[i] == '~' && i + 1 < src.size()) { + if (src[i + 1] == '0') { + dest[out_len++] = '~'; + ++i; + } else if (src[i + 1] == '1') { + dest[out_len++] = '/'; + ++i; + } else { + dest[out_len++] = src[i]; + } + } else { + dest[out_len++] = src[i]; + } + } + } + + // Check if token is numeric + static consteval bool is_numeric(std::string_view token) { + if (token.empty()) return false; + if (token[0] == '0' && token.size() > 1) return false; + for (char c : token) { + if (c < '0' || c > '9') return false; + } + return true; + } + + // Parse numeric token to index + static consteval std::size_t parse_index(std::string_view token) { + std::size_t result = 0; + for (char c : token) { + result = result * 10 + (c - '0'); + } + return result; + } + + // Count tokens in pointer + static consteval std::size_t count_tokens() { + if (pointer_str.empty() || pointer_str == "/") return 0; + + std::size_t count = 0; + std::size_t pos = pointer_str[0] == '/' ? 1 : 0; + + while (pos < pointer_str.size()) { + ++count; + std::size_t next_slash = pointer_str.find('/', pos); + if (next_slash == std::string_view::npos) break; + pos = next_slash + 1; + } + + return count; + } + + // Get Nth token + static consteval std::string_view get_token(std::size_t token_index) { + std::size_t pos = pointer_str[0] == '/' ? 1 : 0; + std::size_t current_token = 0; + + while (current_token < token_index) { + std::size_t next_slash = pointer_str.find('/', pos); + pos = next_slash + 1; + ++current_token; + } + + std::size_t token_end = pointer_str.find('/', pos); + if (token_end == std::string_view::npos) token_end = pointer_str.size(); + + return pointer_str.substr(pos, token_end - pos); + } +}; + +// JSON Pointer accessor +template +struct pointer_accessor { + using parser = json_pointer_parser; + static constexpr std::string_view pointer_view = Pointer.view(); + static constexpr std::size_t token_count = parser::count_tokens(); + + // Validate pointer against struct definition + static consteval bool validate_pointer() { + if constexpr (!std::is_class_v) { + return true; + } + + auto current_type = ^^T; + std::size_t pos = pointer_view[0] == '/' ? 1 : 0; + + while (pos < pointer_view.size()) { + // Extract token up to next / + std::size_t token_end = pointer_view.find('/', pos); + if (token_end == std::string_view::npos) token_end = pointer_view.size(); + + std::string_view token = pointer_view.substr(pos, token_end - pos); + + if (parser::is_numeric(token)) { + if (!path_accessor::is_array_like_reflected(current_type)) { + return false; + } + current_type = path_accessor::get_element_type_reflected(current_type); + } else { + bool found = false; + auto members = std::meta::nonstatic_data_members_of(current_type, std::meta::access_context::unchecked()); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == token) { + current_type = std::meta::type_of(mem); + found = true; + break; + } + } + + if (!found) return false; + } + + pos = token_end + 1; + } + + return true; + } + + // Recursive accessor + template + static inline simdjson_result access_impl(simdjson_result current) noexcept { + if constexpr (TokenIndex >= token_count) { + return current; + } else { + constexpr std::string_view token = parser::get_token(TokenIndex); + + if constexpr (parser::is_numeric(token)) { + constexpr std::size_t index = parser::parse_index(token); + auto arr = current.get_array().value_unsafe(); + auto next_value = arr.at(index); + return access_impl(next_value); + } else { + auto obj = current.get_object().value_unsafe(); + auto next_value = obj.find_field_unordered(token); + return access_impl(next_value); + } + } + } + + // Access JSON value at pointer + template + static inline simdjson_result access(DocOrValue& doc_or_val) noexcept { + if constexpr (std::is_class_v) { + constexpr bool pointer_valid = validate_pointer(); + static_assert(pointer_valid, "JSON Pointer does not match struct definition"); + } + + if (pointer_view.empty() || pointer_view == "/") { + if constexpr (requires { doc_or_val.get_value(); }) { + return doc_or_val.get_value(); + } else { + return doc_or_val; + } + } + + simdjson_result current = doc_or_val.get_value(); + return access_impl<0>(current); + } + + // Extract value at pointer directly into target with type validation + template + static inline error_code extract_field(DocOrValue& doc_or_val, FieldType& target) noexcept { + static_assert(std::is_class_v, "extract_field requires T to be a struct type for validation"); + + constexpr bool pointer_valid = validate_pointer(); + static_assert(pointer_valid, "JSON Pointer does not match struct definition"); + + constexpr auto final_type = get_final_type(); + static_assert(final_type == ^^FieldType, "Target type does not match the field type at the pointer"); + + simdjson_result current_value = doc_or_val.get_value(); + auto json_value = access_impl<0>(current_value); + if (json_value.error()) return json_value.error(); + + return json_value.get(target); + } + +private: + // Get final type by walking pointer through struct + template + static consteval std::enable_if_t, std::meta::info> get_final_type() { + auto current_type = ^^T; + std::size_t pos = pointer_view[0] == '/' ? 1 : 0; + + while (pos < pointer_view.size()) { + std::size_t token_end = pointer_view.find('/', pos); + if (token_end == std::string_view::npos) token_end = pointer_view.size(); + + std::string_view token = pointer_view.substr(pos, token_end - pos); + + if (parser::is_numeric(token)) { + current_type = path_accessor::get_element_type_reflected(current_type); + } else { + auto members = std::meta::nonstatic_data_members_of(current_type, std::meta::access_context::unchecked()); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == token) { + current_type = std::meta::type_of(mem); + break; + } + } + } + + pos = token_end + 1; + } + + return current_type; + } +}; + +// Compile-time JSON Pointer accessor with validation +template +inline simdjson_result<::simdjson::westmere::ondemand::value> at_pointer_compiled(DocOrValue& doc_or_val) noexcept { + using accessor = pointer_accessor; + return accessor::access(doc_or_val); +} + +// Overload without type parameter (no validation) +template +inline simdjson_result<::simdjson::westmere::ondemand::value> at_pointer_compiled(DocOrValue& doc_or_val) noexcept { + using accessor = pointer_accessor; + return accessor::access(doc_or_val); +} + +} // namespace json_path +} // namespace ondemand +} // namespace westmere +} // namespace simdjson + +#endif // SIMDJSON_SUPPORTS_CONCEPTS && SIMDJSON_STATIC_REFLECTION +#endif // SIMDJSON_GENERIC_ONDEMAND_COMPILE_TIME_ACCESSORS_H + +/* end file simdjson/generic/ondemand/compile_time_accessors.h for westmere */ + /* end file simdjson/generic/ondemand/amalgamated.h for westmere */ /* including simdjson/westmere/end.h: #include "simdjson/westmere/end.h" */ /* begin file simdjson/westmere/end.h */ @@ -117201,7 +122878,7 @@ class value { * The following code reads z, then y, then x, and thus will not retrieve x or y if fed the * JSON `{ "x": 1, "y": 2, "z": 3 }`: * - * ```c++ + * ```cpp * simdjson::ondemand::parser parser; * auto obj = parser.parse(R"( { "x": 1, "y": 2, "z": 3 } )"_padded); * double z = obj.find_field("z"); @@ -117574,7 +123251,7 @@ struct simdjson_result : public lsx::implementation_simdjs * The following code reads z, then y, then x, and thus will not retrieve x or y if fed the * JSON `{ "x": 1, "y": 2, "z": 3 }`: * - * ```c++ + * ```cpp * simdjson::ondemand::parser parser; * auto obj = parser.parse(R"( { "x": 1, "y": 2, "z": 3 } )"_padded); * double z = obj.find_field("z"); @@ -119461,7 +125138,7 @@ class array { * JSONPath queries that trivially convertible to JSON Pointer queries: key * names and array indices. * - * https://datatracker.ietf.org/doc/html/draft-normington-jsonpath-00 + * https://www.rfc-editor.org/rfc/rfc9535 (RFC 9535) * * @return The value associated with the given JSONPath expression, or: * - INVALID_JSON_POINTER if the JSONPath to JSON Pointer conversion fails @@ -120139,7 +125816,7 @@ class document { * The following code reads z, then y, then x, and thus will not retrieve x or y if fed the * JSON `{ "x": 1, "y": 2, "z": 3 }`: * - * ```c++ + * ```cpp * simdjson::ondemand::parser parser; * auto obj = parser.parse(R"( { "x": 1, "y": 2, "z": 3 } )"_padded); * double z = obj.find_field("z"); @@ -120433,7 +126110,7 @@ class document { * JSONPath queries that trivially convertible to JSON Pointer queries: key * names and array indices. * - * https://datatracker.ietf.org/doc/html/draft-normington-jsonpath-00 + * https://www.rfc-editor.org/rfc/rfc9535 (RFC 9535) * * Key values are matched exactly, without unescaping or Unicode normalization. * We do a byte-by-byte comparison. E.g. @@ -120466,7 +126143,7 @@ class document { * potentially improving performance by skipping unwanted fields. * * Example: - * ```c++ + * ```cpp * struct Car { * std::string make; * std::string model; @@ -120930,7 +126607,7 @@ class document_stream { /** * Construct an uninitialized document_stream. * - * ```c++ + * ```cpp * document_stream docs; * auto error = parser.iterate_many(json).get(docs); * ``` @@ -121347,7 +127024,7 @@ class object { * The following code reads z, then y, then x, and thus will not retrieve x or y if fed the * JSON `{ "x": 1, "y": 2, "z": 3 }`: * - * ```c++ + * ```cpp * simdjson::ondemand::parser parser; * auto obj = parser.parse(R"( { "x": 1, "y": 2, "z": 3 } )"_padded); * double z = obj.find_field("z"); @@ -121556,7 +127233,7 @@ class object { * potentially improving performance by skipping unwanted fields. * * Example: - * ```c++ + * ```cpp * struct Car { * std::string make; * std::string model; @@ -128538,9 +134215,9 @@ simdjson_inline void string_builder::append(number_type v) noexcept { pv = 0 - pv; // the 0 is for Microsoft } size_t dc = internal::digit_count(pv); - if (negative) { - buffer.get()[position++] = '-'; - } + // by always writing the minus sign, we avoid the branch. + buffer.get()[position] = '-'; + position += negative ? 1 : 0; char *write_pointer = buffer.get() + position + dc - 1; while (pv >= 100) { memcpy(write_pointer - 1, &internal::decimal_table[(pv % 100) * 2], 2); @@ -129209,6 +134886,949 @@ simdjson_warn_unused simdjson_result extract_from(const T &obj, siz #endif /* end file simdjson/generic/ondemand/json_builder.h for lsx */ +// JSON path accessor (compile-time) - must be after inline definitions +/* including simdjson/generic/ondemand/compile_time_accessors.h for lsx: #include "simdjson/generic/ondemand/compile_time_accessors.h" */ +/* begin file simdjson/generic/ondemand/compile_time_accessors.h for lsx */ +/** + * Compile-time JSON Path and JSON Pointer accessors using C++26 reflection (P2996) + * + * This file validates JSON paths/pointers against struct definitions at compile time + * and generates optimized accessor code with zero runtime overhead. + * + * ## How It Works + * + * **Compile Time**: Path is parsed, validated against struct, types are checked + * **Runtime**: Direct navigation with no parsing or validation overhead + * + * Example: + * ```cpp + * struct User { std::string name; std::vector emails; }; + * + * std::string email; + * path_accessor::extract_field(doc, email); + * + * // Compile time validates: + * // 1. User has "emails" field + * // 2. "emails" is array-like + * // 3. Element type is std::string + * // 4. static_assert(^^std::string == ^^std::string) + * + * // Runtime just navigates: + * // doc.get_object().find_field("emails").get_array().at(0).get(email) + * ``` + * + * ## Key Reflection APIs + * + * - `^^Type`: Reflect operator, converts type to std::meta::info + * - `std::meta::nonstatic_data_members_of(type)`: Get all fields of a struct + * - `std::meta::identifier_of(member)`: Get field name as string_view + * - `std::meta::type_of(member)`: Get reflected type of a field + * - `std::meta::is_array_type(type)`: Check if C-style array + * - `std::meta::remove_extent(array)`: Extract element type from array + * - `std::meta::members_of(type)`: Get all members including typedefs + * - `std::meta::is_type(member)`: Check if member is a type (vs field) + * + * All operations execute at compile time in consteval contexts. + */ +#ifndef SIMDJSON_GENERIC_ONDEMAND_COMPILE_TIME_ACCESSORS_H + +/* amalgamation skipped (editor-only): #ifndef SIMDJSON_CONDITIONAL_INCLUDE */ +/* amalgamation skipped (editor-only): #define SIMDJSON_GENERIC_ONDEMAND_COMPILE_TIME_ACCESSORS_H */ +/* amalgamation skipped (editor-only): */ +/* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ + +// Arguably, we should just check SIMDJSON_STATIC_REFLECTION since it +// is unlikely that we will have reflection support without concepts support. +#if SIMDJSON_SUPPORTS_CONCEPTS && SIMDJSON_STATIC_REFLECTION + +#include +#include +#include + +namespace simdjson { +namespace lsx { +namespace ondemand { +/*** + * JSONPath implementation for compile-time access + * RFC 9535 JSONPath: Query Expressions for JSON, https://www.rfc-editor.org/rfc/rfc9535 + */ +namespace json_path { + +// Note: value type must be fully defined before this header is included +// This is ensured by including this in amalgamated.h after value-inl.h + +using ::simdjson::lsx::ondemand::value; + +// Path step types +enum class step_type { + field, // .field_name or ["field_name"] + array_index // [index] +}; + +// Represents a single step in a JSON path +template +struct path_step { + step_type type; + char key[N]; // Field name (empty for array indices) + std::size_t index; // Array index (0 for field access) + + constexpr path_step(step_type t, const char (&k)[N], std::size_t idx = 0) + : type(t), index(idx) { + for (std::size_t i = 0; i < N; ++i) { + key[i] = k[i]; + } + } + + constexpr std::string_view key_view() const { + return {key, N - 1}; + } +}; + +// Helper to create field step +template +consteval auto make_field_step(const char (&name)[N]) { + return path_step(step_type::field, name, 0); +} + +// Helper to create array index step +consteval auto make_index_step(std::size_t idx) { + return path_step<1>(step_type::array_index, "", idx); +} + +// Parse state for compile-time JSON path parsing +struct parse_result { + bool success; + std::size_t pos; + std::string_view error_msg; +}; + +// Compile-time JSON path parser +// Supports subset: .field, ["field"], [index], nested combinations +template +struct json_path_parser { + static constexpr std::string_view path_str = Path.view(); + + // Skip leading $ if present + static consteval std::size_t skip_root() { + if (!path_str.empty() && path_str[0] == '$') { + return 1; + } + return 0; + } + + // Count the number of steps in the path at compile time + static consteval std::size_t count_steps() { + std::size_t count = 0; + std::size_t i = skip_root(); + + while (i < path_str.size()) { + if (path_str[i] == '.') { + // Field access: .field + ++i; + if (i >= path_str.size()) break; + + // Skip field name + while (i < path_str.size() && path_str[i] != '.' && path_str[i] != '[') { + ++i; + } + ++count; + } else if (path_str[i] == '[') { + // Array or bracket notation + ++i; + if (i >= path_str.size()) break; + + if (path_str[i] == '"' || path_str[i] == '\'') { + // Field access: ["field"] or ['field'] + char quote = path_str[i]; + ++i; + while (i < path_str.size() && path_str[i] != quote) { + ++i; + } + if (i < path_str.size()) ++i; // skip closing quote + if (i < path_str.size() && path_str[i] == ']') ++i; + } else { + // Array index: [0], [123] + while (i < path_str.size() && path_str[i] != ']') { + ++i; + } + if (i < path_str.size()) ++i; // skip ] + } + ++count; + } else { + ++i; + } + } + + return count; + } + + // Parse a field name at compile time + static consteval std::size_t parse_field_name(std::size_t start, char* out, std::size_t max_len) { + std::size_t len = 0; + std::size_t i = start; + + while (i < path_str.size() && path_str[i] != '.' && path_str[i] != '[' && len < max_len - 1) { + out[len++] = path_str[i++]; + } + out[len] = '\0'; + return i; + } + + // Parse an array index at compile time + static consteval std::pair parse_array_index(std::size_t start) { + std::size_t index = 0; + std::size_t i = start; + + while (i < path_str.size() && path_str[i] >= '0' && path_str[i] <= '9') { + index = index * 10 + (path_str[i] - '0'); + ++i; + } + + return {i, index}; + } +}; + +// Compile-time path accessor generator +template +struct path_accessor { + using value = ::simdjson::lsx::ondemand::value; + + static constexpr auto parser = json_path_parser(); + static constexpr std::size_t num_steps = parser.count_steps(); + static constexpr std::string_view path_view = Path.view(); + + // Compile-time accessor generation + // If T is a struct, validates the path at compile time + // If T is void, skips validation + template + static inline simdjson_result access(DocOrValue& doc_or_val) noexcept { + // Validate path at compile time if T is a struct + if constexpr (std::is_class_v) { + constexpr bool path_valid = validate_path(); + static_assert(path_valid, "JSON path does not match struct definition"); + } + + // Parse the path at compile time to build access steps + return access_impl(doc_or_val.get_value()); + } + + // Extract value at path directly into target with compile-time type validation + // Example: std::string name; path_accessor::extract_field(doc, name); + template + static inline error_code extract_field(DocOrValue& doc_or_val, FieldType& target) noexcept { + static_assert(std::is_class_v, "extract_field requires T to be a struct type for validation"); + + // Validate path exists in struct definition + constexpr bool path_valid = validate_path(); + static_assert(path_valid, "JSON path does not match struct definition"); + + // Get the type at the end of the path + constexpr auto final_type = get_final_type(); + + // Verify target type matches the field type + static_assert(final_type == ^^FieldType, "Target type does not match the field type at the path"); + + // All validation done at compile time - just navigate and extract + auto json_value = access_impl(doc_or_val.get_value()); + if (json_value.error()) return json_value.error(); + + return json_value.get(target); + } + +private: + // Get the final type by walking the path through the struct type + template + static consteval std::enable_if_t, std::meta::info> get_final_type() { + auto current_type = ^^T; + std::size_t i = parser.skip_root(); + + while (i < path_view.size()) { + if (path_view[i] == '.') { + // .field syntax + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != '.' && path_view[i] != '[') { + ++i; + } + + std::string_view field_name = path_view.substr(field_start, i - field_start); + + auto members = std::meta::nonstatic_data_members_of( + current_type, std::meta::access_context::unchecked() + ); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == field_name) { + current_type = std::meta::type_of(mem); + break; + } + } + + } else if (path_view[i] == '[') { + ++i; + if (i >= path_view.size()) break; + + if (path_view[i] == '"' || path_view[i] == '\'') { + // ["field"] syntax + char quote = path_view[i]; + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != quote) { + ++i; + } + + std::string_view field_name = path_view.substr(field_start, i - field_start); + if (i < path_view.size()) ++i; // skip quote + if (i < path_view.size() && path_view[i] == ']') ++i; + + auto members = std::meta::nonstatic_data_members_of( + current_type, std::meta::access_context::unchecked() + ); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == field_name) { + current_type = std::meta::type_of(mem); + break; + } + } + + } else { + // [index] syntax - extract element type + while (i < path_view.size() && path_view[i] >= '0' && path_view[i] <= '9') { + ++i; + } + if (i < path_view.size() && path_view[i] == ']') ++i; + + current_type = get_element_type_reflected(current_type); + } + } else { + ++i; + } + } + + return current_type; + } + +private: + // Walk path and extract directly into final field using compile-time reflection + template + static inline error_code extract_with_reflection(simdjson_result current, TargetType& target_ref) noexcept { + if (current.error()) return current.error(); + + // Base case: end of path - extract into target + if constexpr (PathPos >= path_view.size()) { + return current.get(target_ref); + } + // Field access: .field_name + else if constexpr (path_view[PathPos] == '.') { + constexpr auto field_info = parse_next_field(PathPos); + constexpr std::string_view field_name = std::get<0>(field_info); + constexpr std::size_t next_pos = std::get<1>(field_info); + + constexpr auto member_info = find_member_by_name(CurrentType, field_name); + static_assert(member_info != ^^void, "Field not found in struct"); + + constexpr auto member_type = std::meta::type_of(member_info); + + auto obj_result = current.get_object(); + if (obj_result.error()) return obj_result.error(); + auto obj = obj_result.value_unsafe(); + auto field_value = obj.find_field_unordered(field_name); + + if constexpr (next_pos >= path_view.size()) { + return field_value.get(target_ref); + } else { + return extract_with_reflection(field_value, target_ref); + } + } + // Bracket notation: [index] or ["field"] + else if constexpr (path_view[PathPos] == '[') { + constexpr auto bracket_info = parse_bracket(PathPos); + constexpr bool is_field = std::get<0>(bracket_info); + constexpr std::size_t next_pos = std::get<2>(bracket_info); + + if constexpr (is_field) { + constexpr std::string_view field_name = std::get<1>(bracket_info); + constexpr auto member_info = find_member_by_name(CurrentType, field_name); + static_assert(member_info != ^^void, "Field not found in struct"); + constexpr auto member_type = std::meta::type_of(member_info); + + auto obj_result = current.get_object(); + if (obj_result.error()) return obj_result.error(); + auto obj = obj_result.value_unsafe(); + auto field_value = obj.find_field_unordered(field_name); + + if constexpr (next_pos >= path_view.size()) { + return field_value.get(target_ref); + } else { + return extract_with_reflection(field_value, target_ref); + } + } else { + constexpr std::size_t index = std::get<3>(bracket_info); + constexpr auto elem_type = get_element_type_reflected(CurrentType); + static_assert(elem_type != ^^void, "Could not determine array element type"); + + auto arr_result = current.get_array(); + if (arr_result.error()) return arr_result.error(); + auto arr = arr_result.value_unsafe(); + auto elem_value = arr.at(index); + + if constexpr (next_pos >= path_view.size()) { + return elem_value.get(target_ref); + } else { + return extract_with_reflection(elem_value, target_ref); + } + } + } + // Skip unexpected characters and continue + else { + return extract_with_reflection(current, target_ref); + } + } + + // Find member by name in reflected type + static consteval std::meta::info find_member_by_name(std::meta::info type_refl, std::string_view name) { + auto members = std::meta::nonstatic_data_members_of(type_refl, std::meta::access_context::unchecked()); + for (auto mem : members) { + if (std::meta::identifier_of(mem) == name) { + return mem; + } + } + } + + // Generate compile-time accessor code by walking the path + template + static inline simdjson_result access_impl(simdjson_result current) noexcept { + if (current.error()) return current; + + if constexpr (PathPos >= path_view.size()) { + return current; + } else if constexpr (path_view[PathPos] == '.') { + constexpr auto field_info = parse_next_field(PathPos); + constexpr std::string_view field_name = std::get<0>(field_info); + constexpr std::size_t next_pos = std::get<1>(field_info); + + auto obj_result = current.get_object(); + if (obj_result.error()) return obj_result.error(); + + auto obj = obj_result.value_unsafe(); + auto next_value = obj.find_field_unordered(field_name); + + return access_impl(next_value); + + } else if constexpr (path_view[PathPos] == '[') { + constexpr auto bracket_info = parse_bracket(PathPos); + constexpr bool is_field = std::get<0>(bracket_info); + constexpr std::size_t next_pos = std::get<2>(bracket_info); + + if constexpr (is_field) { + constexpr std::string_view field_name = std::get<1>(bracket_info); + + auto obj_result = current.get_object(); + if (obj_result.error()) return obj_result.error(); + + auto obj = obj_result.value_unsafe(); + auto next_value = obj.find_field_unordered(field_name); + + return access_impl(next_value); + + } else { + constexpr std::size_t index = std::get<3>(bracket_info); + + auto arr_result = current.get_array(); + if (arr_result.error()) return arr_result.error(); + + auto arr = arr_result.value_unsafe(); + auto next_value = arr.at(index); + + return access_impl(next_value); + } + } else { + return access_impl(current); + } + } + + // Parse next field name + static consteval auto parse_next_field(std::size_t start) { + std::size_t i = start + 1; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != '.' && path_view[i] != '[') { + ++i; + } + std::string_view field_name = path_view.substr(field_start, i - field_start); + return std::make_tuple(field_name, i); + } + + // Parse bracket notation: returns (is_field, field_name, next_pos, index) + static consteval auto parse_bracket(std::size_t start) { + std::size_t i = start + 1; // skip '[' + + if (i < path_view.size() && (path_view[i] == '"' || path_view[i] == '\'')) { + // Field access + char quote = path_view[i]; + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != quote) { + ++i; + } + std::string_view field_name = path_view.substr(field_start, i - field_start); + if (i < path_view.size()) ++i; // skip closing quote + if (i < path_view.size() && path_view[i] == ']') ++i; + + return std::make_tuple(true, field_name, i, std::size_t(0)); + } else { + // Array index + std::size_t index = 0; + while (i < path_view.size() && path_view[i] >= '0' && path_view[i] <= '9') { + index = index * 10 + (path_view[i] - '0'); + ++i; + } + if (i < path_view.size() && path_view[i] == ']') ++i; + + return std::make_tuple(false, std::string_view{}, i, index); + } + } + +public: + // Check if reflected type is array-like (C-style array or indexable container) + // Uses reflection to test: 1) std::meta::is_array_type() for C arrays + // 2) std::meta::substitute() to test concepts::indexable_container concept + static consteval bool is_array_like_reflected(std::meta::info type_reflection) { + if (std::meta::is_array_type(type_reflection)) { + return true; + } + + if (std::meta::can_substitute(^^concepts::indexable_container_v, {type_reflection})) { + return std::meta::extract(std::meta::substitute(^^concepts::indexable_container_v, {type_reflection})); + } + return false; + } + + // Extract element type from reflected array or container + // For C arrays: uses std::meta::remove_extent() + // For containers: finds value_type member using std::meta::members_of() + static consteval std::meta::info get_element_type_reflected(std::meta::info type_reflection) { + if (std::meta::is_array_type(type_reflection)) { + return std::meta::remove_extent(type_reflection); + } + + auto members = std::meta::members_of(type_reflection, std::meta::access_context::unchecked()); + for (auto mem : members) { + if (std::meta::is_type(mem)) { + auto name = std::meta::identifier_of(mem); + if (name == "value_type") { + return mem; + } + } + } + return ^^void; + } + +private: + // Check if type has member with given name + template + static consteval bool has_member(std::string_view member_name) { + constexpr auto members = std::meta::nonstatic_data_members_of(^^Type, std::meta::access_context::unchecked()); + for (auto mem : members) { + if (std::meta::identifier_of(mem) == member_name) { + return true; + } + } + return false; + } + + // Get type of member by name + template + static consteval auto get_member_type(std::string_view member_name) { + constexpr auto members = std::meta::nonstatic_data_members_of(^^Type, std::meta::access_context::unchecked()); + for (auto mem : members) { + if (std::meta::identifier_of(mem) == member_name) { + return std::meta::type_of(mem); + } + } + return ^^void; + } + + // Check if non-reflected type is array-like + template + static consteval bool is_container_type() { + using BaseType = std::remove_cvref_t; + if constexpr (requires { typename BaseType::value_type; }) { + return true; + } + if constexpr (std::is_array_v) { + return true; + } + return false; + } + + // Extract element type from non-reflected container + template + using extract_element_type = std::conditional_t< + requires { typename std::remove_cvref_t::value_type; }, + typename std::remove_cvref_t::value_type, + std::conditional_t< + std::is_array_v>, + std::remove_extent_t>, + void + > + >; + + // Validate path matches struct definition + static consteval bool validate_path() { + if constexpr (!std::is_class_v) { + return true; + } + + auto current_type = ^^T; + std::size_t i = parser.skip_root(); + + while (i < path_view.size()) { + if (path_view[i] == '.') { + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != '.' && path_view[i] != '[') { + ++i; + } + + std::string_view field_name = path_view.substr(field_start, i - field_start); + + bool found = false; + auto members = std::meta::nonstatic_data_members_of(current_type, std::meta::access_context::unchecked()); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == field_name) { + current_type = std::meta::type_of(mem); + found = true; + break; + } + } + + if (!found) { + return false; + } + + } else if (path_view[i] == '[') { + ++i; + if (i >= path_view.size()) return false; + + if (path_view[i] == '"' || path_view[i] == '\'') { + char quote = path_view[i]; + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != quote) { + ++i; + } + + std::string_view field_name = path_view.substr(field_start, i - field_start); + if (i < path_view.size()) ++i; + if (i < path_view.size() && path_view[i] == ']') ++i; + + bool found = false; + auto members = std::meta::nonstatic_data_members_of(current_type, std::meta::access_context::unchecked()); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == field_name) { + current_type = std::meta::type_of(mem); + found = true; + break; + } + } + + if (!found) { + return false; + } + + } else { + while (i < path_view.size() && path_view[i] >= '0' && path_view[i] <= '9') { + ++i; + } + + if (i < path_view.size() && path_view[i] == ']') ++i; + + if (!is_array_like_reflected(current_type)) { + return false; + } + + auto new_type = get_element_type_reflected(current_type); + + if (new_type == ^^void) { + return false; + } + + current_type = new_type; + } + } else { + ++i; + } + } + + return true; + } +}; + +// Compile-time path accessor with validation +template +inline simdjson_result<::simdjson::lsx::ondemand::value> at_path_compiled(DocOrValue& doc_or_val) noexcept { + using accessor = path_accessor; + return accessor::access(doc_or_val); +} + +// Overload without type parameter (no validation) +template +inline simdjson_result<::simdjson::lsx::ondemand::value> at_path_compiled(DocOrValue& doc_or_val) noexcept { + using accessor = path_accessor; + return accessor::access(doc_or_val); +} + +// ============================================================================ +// JSON Pointer Compile-Time Support (RFC 6901) +// ============================================================================ + +// JSON Pointer parser: /field/0/nested (slash-separated) +template +struct json_pointer_parser { + static constexpr std::string_view pointer_str = Pointer.view(); + + // Unescape token: ~0 -> ~, ~1 -> / + static consteval void unescape_token(std::string_view src, char* dest, std::size_t& out_len) { + out_len = 0; + for (std::size_t i = 0; i < src.size(); ++i) { + if (src[i] == '~' && i + 1 < src.size()) { + if (src[i + 1] == '0') { + dest[out_len++] = '~'; + ++i; + } else if (src[i + 1] == '1') { + dest[out_len++] = '/'; + ++i; + } else { + dest[out_len++] = src[i]; + } + } else { + dest[out_len++] = src[i]; + } + } + } + + // Check if token is numeric + static consteval bool is_numeric(std::string_view token) { + if (token.empty()) return false; + if (token[0] == '0' && token.size() > 1) return false; + for (char c : token) { + if (c < '0' || c > '9') return false; + } + return true; + } + + // Parse numeric token to index + static consteval std::size_t parse_index(std::string_view token) { + std::size_t result = 0; + for (char c : token) { + result = result * 10 + (c - '0'); + } + return result; + } + + // Count tokens in pointer + static consteval std::size_t count_tokens() { + if (pointer_str.empty() || pointer_str == "/") return 0; + + std::size_t count = 0; + std::size_t pos = pointer_str[0] == '/' ? 1 : 0; + + while (pos < pointer_str.size()) { + ++count; + std::size_t next_slash = pointer_str.find('/', pos); + if (next_slash == std::string_view::npos) break; + pos = next_slash + 1; + } + + return count; + } + + // Get Nth token + static consteval std::string_view get_token(std::size_t token_index) { + std::size_t pos = pointer_str[0] == '/' ? 1 : 0; + std::size_t current_token = 0; + + while (current_token < token_index) { + std::size_t next_slash = pointer_str.find('/', pos); + pos = next_slash + 1; + ++current_token; + } + + std::size_t token_end = pointer_str.find('/', pos); + if (token_end == std::string_view::npos) token_end = pointer_str.size(); + + return pointer_str.substr(pos, token_end - pos); + } +}; + +// JSON Pointer accessor +template +struct pointer_accessor { + using parser = json_pointer_parser; + static constexpr std::string_view pointer_view = Pointer.view(); + static constexpr std::size_t token_count = parser::count_tokens(); + + // Validate pointer against struct definition + static consteval bool validate_pointer() { + if constexpr (!std::is_class_v) { + return true; + } + + auto current_type = ^^T; + std::size_t pos = pointer_view[0] == '/' ? 1 : 0; + + while (pos < pointer_view.size()) { + // Extract token up to next / + std::size_t token_end = pointer_view.find('/', pos); + if (token_end == std::string_view::npos) token_end = pointer_view.size(); + + std::string_view token = pointer_view.substr(pos, token_end - pos); + + if (parser::is_numeric(token)) { + if (!path_accessor::is_array_like_reflected(current_type)) { + return false; + } + current_type = path_accessor::get_element_type_reflected(current_type); + } else { + bool found = false; + auto members = std::meta::nonstatic_data_members_of(current_type, std::meta::access_context::unchecked()); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == token) { + current_type = std::meta::type_of(mem); + found = true; + break; + } + } + + if (!found) return false; + } + + pos = token_end + 1; + } + + return true; + } + + // Recursive accessor + template + static inline simdjson_result access_impl(simdjson_result current) noexcept { + if constexpr (TokenIndex >= token_count) { + return current; + } else { + constexpr std::string_view token = parser::get_token(TokenIndex); + + if constexpr (parser::is_numeric(token)) { + constexpr std::size_t index = parser::parse_index(token); + auto arr = current.get_array().value_unsafe(); + auto next_value = arr.at(index); + return access_impl(next_value); + } else { + auto obj = current.get_object().value_unsafe(); + auto next_value = obj.find_field_unordered(token); + return access_impl(next_value); + } + } + } + + // Access JSON value at pointer + template + static inline simdjson_result access(DocOrValue& doc_or_val) noexcept { + if constexpr (std::is_class_v) { + constexpr bool pointer_valid = validate_pointer(); + static_assert(pointer_valid, "JSON Pointer does not match struct definition"); + } + + if (pointer_view.empty() || pointer_view == "/") { + if constexpr (requires { doc_or_val.get_value(); }) { + return doc_or_val.get_value(); + } else { + return doc_or_val; + } + } + + simdjson_result current = doc_or_val.get_value(); + return access_impl<0>(current); + } + + // Extract value at pointer directly into target with type validation + template + static inline error_code extract_field(DocOrValue& doc_or_val, FieldType& target) noexcept { + static_assert(std::is_class_v, "extract_field requires T to be a struct type for validation"); + + constexpr bool pointer_valid = validate_pointer(); + static_assert(pointer_valid, "JSON Pointer does not match struct definition"); + + constexpr auto final_type = get_final_type(); + static_assert(final_type == ^^FieldType, "Target type does not match the field type at the pointer"); + + simdjson_result current_value = doc_or_val.get_value(); + auto json_value = access_impl<0>(current_value); + if (json_value.error()) return json_value.error(); + + return json_value.get(target); + } + +private: + // Get final type by walking pointer through struct + template + static consteval std::enable_if_t, std::meta::info> get_final_type() { + auto current_type = ^^T; + std::size_t pos = pointer_view[0] == '/' ? 1 : 0; + + while (pos < pointer_view.size()) { + std::size_t token_end = pointer_view.find('/', pos); + if (token_end == std::string_view::npos) token_end = pointer_view.size(); + + std::string_view token = pointer_view.substr(pos, token_end - pos); + + if (parser::is_numeric(token)) { + current_type = path_accessor::get_element_type_reflected(current_type); + } else { + auto members = std::meta::nonstatic_data_members_of(current_type, std::meta::access_context::unchecked()); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == token) { + current_type = std::meta::type_of(mem); + break; + } + } + } + + pos = token_end + 1; + } + + return current_type; + } +}; + +// Compile-time JSON Pointer accessor with validation +template +inline simdjson_result<::simdjson::lsx::ondemand::value> at_pointer_compiled(DocOrValue& doc_or_val) noexcept { + using accessor = pointer_accessor; + return accessor::access(doc_or_val); +} + +// Overload without type parameter (no validation) +template +inline simdjson_result<::simdjson::lsx::ondemand::value> at_pointer_compiled(DocOrValue& doc_or_val) noexcept { + using accessor = pointer_accessor; + return accessor::access(doc_or_val); +} + +} // namespace json_path +} // namespace ondemand +} // namespace lsx +} // namespace simdjson + +#endif // SIMDJSON_SUPPORTS_CONCEPTS && SIMDJSON_STATIC_REFLECTION +#endif // SIMDJSON_GENERIC_ONDEMAND_COMPILE_TIME_ACCESSORS_H + +/* end file simdjson/generic/ondemand/compile_time_accessors.h for lsx */ + /* end file simdjson/generic/ondemand/amalgamated.h for lsx */ /* including simdjson/lsx/end.h: #include "simdjson/lsx/end.h" */ /* begin file simdjson/lsx/end.h */ @@ -130927,7 +137547,7 @@ class value { * The following code reads z, then y, then x, and thus will not retrieve x or y if fed the * JSON `{ "x": 1, "y": 2, "z": 3 }`: * - * ```c++ + * ```cpp * simdjson::ondemand::parser parser; * auto obj = parser.parse(R"( { "x": 1, "y": 2, "z": 3 } )"_padded); * double z = obj.find_field("z"); @@ -131300,7 +137920,7 @@ struct simdjson_result : public lasx::implementation_simd * The following code reads z, then y, then x, and thus will not retrieve x or y if fed the * JSON `{ "x": 1, "y": 2, "z": 3 }`: * - * ```c++ + * ```cpp * simdjson::ondemand::parser parser; * auto obj = parser.parse(R"( { "x": 1, "y": 2, "z": 3 } )"_padded); * double z = obj.find_field("z"); @@ -133187,7 +139807,7 @@ class array { * JSONPath queries that trivially convertible to JSON Pointer queries: key * names and array indices. * - * https://datatracker.ietf.org/doc/html/draft-normington-jsonpath-00 + * https://www.rfc-editor.org/rfc/rfc9535 (RFC 9535) * * @return The value associated with the given JSONPath expression, or: * - INVALID_JSON_POINTER if the JSONPath to JSON Pointer conversion fails @@ -133865,7 +140485,7 @@ class document { * The following code reads z, then y, then x, and thus will not retrieve x or y if fed the * JSON `{ "x": 1, "y": 2, "z": 3 }`: * - * ```c++ + * ```cpp * simdjson::ondemand::parser parser; * auto obj = parser.parse(R"( { "x": 1, "y": 2, "z": 3 } )"_padded); * double z = obj.find_field("z"); @@ -134159,7 +140779,7 @@ class document { * JSONPath queries that trivially convertible to JSON Pointer queries: key * names and array indices. * - * https://datatracker.ietf.org/doc/html/draft-normington-jsonpath-00 + * https://www.rfc-editor.org/rfc/rfc9535 (RFC 9535) * * Key values are matched exactly, without unescaping or Unicode normalization. * We do a byte-by-byte comparison. E.g. @@ -134192,7 +140812,7 @@ class document { * potentially improving performance by skipping unwanted fields. * * Example: - * ```c++ + * ```cpp * struct Car { * std::string make; * std::string model; @@ -134656,7 +141276,7 @@ class document_stream { /** * Construct an uninitialized document_stream. * - * ```c++ + * ```cpp * document_stream docs; * auto error = parser.iterate_many(json).get(docs); * ``` @@ -135073,7 +141693,7 @@ class object { * The following code reads z, then y, then x, and thus will not retrieve x or y if fed the * JSON `{ "x": 1, "y": 2, "z": 3 }`: * - * ```c++ + * ```cpp * simdjson::ondemand::parser parser; * auto obj = parser.parse(R"( { "x": 1, "y": 2, "z": 3 } )"_padded); * double z = obj.find_field("z"); @@ -135282,7 +141902,7 @@ class object { * potentially improving performance by skipping unwanted fields. * * Example: - * ```c++ + * ```cpp * struct Car { * std::string make; * std::string model; @@ -142264,9 +148884,9 @@ simdjson_inline void string_builder::append(number_type v) noexcept { pv = 0 - pv; // the 0 is for Microsoft } size_t dc = internal::digit_count(pv); - if (negative) { - buffer.get()[position++] = '-'; - } + // by always writing the minus sign, we avoid the branch. + buffer.get()[position] = '-'; + position += negative ? 1 : 0; char *write_pointer = buffer.get() + position + dc - 1; while (pv >= 100) { memcpy(write_pointer - 1, &internal::decimal_table[(pv % 100) * 2], 2); @@ -142935,6 +149555,949 @@ simdjson_warn_unused simdjson_result extract_from(const T &obj, siz #endif /* end file simdjson/generic/ondemand/json_builder.h for lasx */ +// JSON path accessor (compile-time) - must be after inline definitions +/* including simdjson/generic/ondemand/compile_time_accessors.h for lasx: #include "simdjson/generic/ondemand/compile_time_accessors.h" */ +/* begin file simdjson/generic/ondemand/compile_time_accessors.h for lasx */ +/** + * Compile-time JSON Path and JSON Pointer accessors using C++26 reflection (P2996) + * + * This file validates JSON paths/pointers against struct definitions at compile time + * and generates optimized accessor code with zero runtime overhead. + * + * ## How It Works + * + * **Compile Time**: Path is parsed, validated against struct, types are checked + * **Runtime**: Direct navigation with no parsing or validation overhead + * + * Example: + * ```cpp + * struct User { std::string name; std::vector emails; }; + * + * std::string email; + * path_accessor::extract_field(doc, email); + * + * // Compile time validates: + * // 1. User has "emails" field + * // 2. "emails" is array-like + * // 3. Element type is std::string + * // 4. static_assert(^^std::string == ^^std::string) + * + * // Runtime just navigates: + * // doc.get_object().find_field("emails").get_array().at(0).get(email) + * ``` + * + * ## Key Reflection APIs + * + * - `^^Type`: Reflect operator, converts type to std::meta::info + * - `std::meta::nonstatic_data_members_of(type)`: Get all fields of a struct + * - `std::meta::identifier_of(member)`: Get field name as string_view + * - `std::meta::type_of(member)`: Get reflected type of a field + * - `std::meta::is_array_type(type)`: Check if C-style array + * - `std::meta::remove_extent(array)`: Extract element type from array + * - `std::meta::members_of(type)`: Get all members including typedefs + * - `std::meta::is_type(member)`: Check if member is a type (vs field) + * + * All operations execute at compile time in consteval contexts. + */ +#ifndef SIMDJSON_GENERIC_ONDEMAND_COMPILE_TIME_ACCESSORS_H + +/* amalgamation skipped (editor-only): #ifndef SIMDJSON_CONDITIONAL_INCLUDE */ +/* amalgamation skipped (editor-only): #define SIMDJSON_GENERIC_ONDEMAND_COMPILE_TIME_ACCESSORS_H */ +/* amalgamation skipped (editor-only): */ +/* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ + +// Arguably, we should just check SIMDJSON_STATIC_REFLECTION since it +// is unlikely that we will have reflection support without concepts support. +#if SIMDJSON_SUPPORTS_CONCEPTS && SIMDJSON_STATIC_REFLECTION + +#include +#include +#include + +namespace simdjson { +namespace lasx { +namespace ondemand { +/*** + * JSONPath implementation for compile-time access + * RFC 9535 JSONPath: Query Expressions for JSON, https://www.rfc-editor.org/rfc/rfc9535 + */ +namespace json_path { + +// Note: value type must be fully defined before this header is included +// This is ensured by including this in amalgamated.h after value-inl.h + +using ::simdjson::lasx::ondemand::value; + +// Path step types +enum class step_type { + field, // .field_name or ["field_name"] + array_index // [index] +}; + +// Represents a single step in a JSON path +template +struct path_step { + step_type type; + char key[N]; // Field name (empty for array indices) + std::size_t index; // Array index (0 for field access) + + constexpr path_step(step_type t, const char (&k)[N], std::size_t idx = 0) + : type(t), index(idx) { + for (std::size_t i = 0; i < N; ++i) { + key[i] = k[i]; + } + } + + constexpr std::string_view key_view() const { + return {key, N - 1}; + } +}; + +// Helper to create field step +template +consteval auto make_field_step(const char (&name)[N]) { + return path_step(step_type::field, name, 0); +} + +// Helper to create array index step +consteval auto make_index_step(std::size_t idx) { + return path_step<1>(step_type::array_index, "", idx); +} + +// Parse state for compile-time JSON path parsing +struct parse_result { + bool success; + std::size_t pos; + std::string_view error_msg; +}; + +// Compile-time JSON path parser +// Supports subset: .field, ["field"], [index], nested combinations +template +struct json_path_parser { + static constexpr std::string_view path_str = Path.view(); + + // Skip leading $ if present + static consteval std::size_t skip_root() { + if (!path_str.empty() && path_str[0] == '$') { + return 1; + } + return 0; + } + + // Count the number of steps in the path at compile time + static consteval std::size_t count_steps() { + std::size_t count = 0; + std::size_t i = skip_root(); + + while (i < path_str.size()) { + if (path_str[i] == '.') { + // Field access: .field + ++i; + if (i >= path_str.size()) break; + + // Skip field name + while (i < path_str.size() && path_str[i] != '.' && path_str[i] != '[') { + ++i; + } + ++count; + } else if (path_str[i] == '[') { + // Array or bracket notation + ++i; + if (i >= path_str.size()) break; + + if (path_str[i] == '"' || path_str[i] == '\'') { + // Field access: ["field"] or ['field'] + char quote = path_str[i]; + ++i; + while (i < path_str.size() && path_str[i] != quote) { + ++i; + } + if (i < path_str.size()) ++i; // skip closing quote + if (i < path_str.size() && path_str[i] == ']') ++i; + } else { + // Array index: [0], [123] + while (i < path_str.size() && path_str[i] != ']') { + ++i; + } + if (i < path_str.size()) ++i; // skip ] + } + ++count; + } else { + ++i; + } + } + + return count; + } + + // Parse a field name at compile time + static consteval std::size_t parse_field_name(std::size_t start, char* out, std::size_t max_len) { + std::size_t len = 0; + std::size_t i = start; + + while (i < path_str.size() && path_str[i] != '.' && path_str[i] != '[' && len < max_len - 1) { + out[len++] = path_str[i++]; + } + out[len] = '\0'; + return i; + } + + // Parse an array index at compile time + static consteval std::pair parse_array_index(std::size_t start) { + std::size_t index = 0; + std::size_t i = start; + + while (i < path_str.size() && path_str[i] >= '0' && path_str[i] <= '9') { + index = index * 10 + (path_str[i] - '0'); + ++i; + } + + return {i, index}; + } +}; + +// Compile-time path accessor generator +template +struct path_accessor { + using value = ::simdjson::lasx::ondemand::value; + + static constexpr auto parser = json_path_parser(); + static constexpr std::size_t num_steps = parser.count_steps(); + static constexpr std::string_view path_view = Path.view(); + + // Compile-time accessor generation + // If T is a struct, validates the path at compile time + // If T is void, skips validation + template + static inline simdjson_result access(DocOrValue& doc_or_val) noexcept { + // Validate path at compile time if T is a struct + if constexpr (std::is_class_v) { + constexpr bool path_valid = validate_path(); + static_assert(path_valid, "JSON path does not match struct definition"); + } + + // Parse the path at compile time to build access steps + return access_impl(doc_or_val.get_value()); + } + + // Extract value at path directly into target with compile-time type validation + // Example: std::string name; path_accessor::extract_field(doc, name); + template + static inline error_code extract_field(DocOrValue& doc_or_val, FieldType& target) noexcept { + static_assert(std::is_class_v, "extract_field requires T to be a struct type for validation"); + + // Validate path exists in struct definition + constexpr bool path_valid = validate_path(); + static_assert(path_valid, "JSON path does not match struct definition"); + + // Get the type at the end of the path + constexpr auto final_type = get_final_type(); + + // Verify target type matches the field type + static_assert(final_type == ^^FieldType, "Target type does not match the field type at the path"); + + // All validation done at compile time - just navigate and extract + auto json_value = access_impl(doc_or_val.get_value()); + if (json_value.error()) return json_value.error(); + + return json_value.get(target); + } + +private: + // Get the final type by walking the path through the struct type + template + static consteval std::enable_if_t, std::meta::info> get_final_type() { + auto current_type = ^^T; + std::size_t i = parser.skip_root(); + + while (i < path_view.size()) { + if (path_view[i] == '.') { + // .field syntax + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != '.' && path_view[i] != '[') { + ++i; + } + + std::string_view field_name = path_view.substr(field_start, i - field_start); + + auto members = std::meta::nonstatic_data_members_of( + current_type, std::meta::access_context::unchecked() + ); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == field_name) { + current_type = std::meta::type_of(mem); + break; + } + } + + } else if (path_view[i] == '[') { + ++i; + if (i >= path_view.size()) break; + + if (path_view[i] == '"' || path_view[i] == '\'') { + // ["field"] syntax + char quote = path_view[i]; + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != quote) { + ++i; + } + + std::string_view field_name = path_view.substr(field_start, i - field_start); + if (i < path_view.size()) ++i; // skip quote + if (i < path_view.size() && path_view[i] == ']') ++i; + + auto members = std::meta::nonstatic_data_members_of( + current_type, std::meta::access_context::unchecked() + ); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == field_name) { + current_type = std::meta::type_of(mem); + break; + } + } + + } else { + // [index] syntax - extract element type + while (i < path_view.size() && path_view[i] >= '0' && path_view[i] <= '9') { + ++i; + } + if (i < path_view.size() && path_view[i] == ']') ++i; + + current_type = get_element_type_reflected(current_type); + } + } else { + ++i; + } + } + + return current_type; + } + +private: + // Walk path and extract directly into final field using compile-time reflection + template + static inline error_code extract_with_reflection(simdjson_result current, TargetType& target_ref) noexcept { + if (current.error()) return current.error(); + + // Base case: end of path - extract into target + if constexpr (PathPos >= path_view.size()) { + return current.get(target_ref); + } + // Field access: .field_name + else if constexpr (path_view[PathPos] == '.') { + constexpr auto field_info = parse_next_field(PathPos); + constexpr std::string_view field_name = std::get<0>(field_info); + constexpr std::size_t next_pos = std::get<1>(field_info); + + constexpr auto member_info = find_member_by_name(CurrentType, field_name); + static_assert(member_info != ^^void, "Field not found in struct"); + + constexpr auto member_type = std::meta::type_of(member_info); + + auto obj_result = current.get_object(); + if (obj_result.error()) return obj_result.error(); + auto obj = obj_result.value_unsafe(); + auto field_value = obj.find_field_unordered(field_name); + + if constexpr (next_pos >= path_view.size()) { + return field_value.get(target_ref); + } else { + return extract_with_reflection(field_value, target_ref); + } + } + // Bracket notation: [index] or ["field"] + else if constexpr (path_view[PathPos] == '[') { + constexpr auto bracket_info = parse_bracket(PathPos); + constexpr bool is_field = std::get<0>(bracket_info); + constexpr std::size_t next_pos = std::get<2>(bracket_info); + + if constexpr (is_field) { + constexpr std::string_view field_name = std::get<1>(bracket_info); + constexpr auto member_info = find_member_by_name(CurrentType, field_name); + static_assert(member_info != ^^void, "Field not found in struct"); + constexpr auto member_type = std::meta::type_of(member_info); + + auto obj_result = current.get_object(); + if (obj_result.error()) return obj_result.error(); + auto obj = obj_result.value_unsafe(); + auto field_value = obj.find_field_unordered(field_name); + + if constexpr (next_pos >= path_view.size()) { + return field_value.get(target_ref); + } else { + return extract_with_reflection(field_value, target_ref); + } + } else { + constexpr std::size_t index = std::get<3>(bracket_info); + constexpr auto elem_type = get_element_type_reflected(CurrentType); + static_assert(elem_type != ^^void, "Could not determine array element type"); + + auto arr_result = current.get_array(); + if (arr_result.error()) return arr_result.error(); + auto arr = arr_result.value_unsafe(); + auto elem_value = arr.at(index); + + if constexpr (next_pos >= path_view.size()) { + return elem_value.get(target_ref); + } else { + return extract_with_reflection(elem_value, target_ref); + } + } + } + // Skip unexpected characters and continue + else { + return extract_with_reflection(current, target_ref); + } + } + + // Find member by name in reflected type + static consteval std::meta::info find_member_by_name(std::meta::info type_refl, std::string_view name) { + auto members = std::meta::nonstatic_data_members_of(type_refl, std::meta::access_context::unchecked()); + for (auto mem : members) { + if (std::meta::identifier_of(mem) == name) { + return mem; + } + } + } + + // Generate compile-time accessor code by walking the path + template + static inline simdjson_result access_impl(simdjson_result current) noexcept { + if (current.error()) return current; + + if constexpr (PathPos >= path_view.size()) { + return current; + } else if constexpr (path_view[PathPos] == '.') { + constexpr auto field_info = parse_next_field(PathPos); + constexpr std::string_view field_name = std::get<0>(field_info); + constexpr std::size_t next_pos = std::get<1>(field_info); + + auto obj_result = current.get_object(); + if (obj_result.error()) return obj_result.error(); + + auto obj = obj_result.value_unsafe(); + auto next_value = obj.find_field_unordered(field_name); + + return access_impl(next_value); + + } else if constexpr (path_view[PathPos] == '[') { + constexpr auto bracket_info = parse_bracket(PathPos); + constexpr bool is_field = std::get<0>(bracket_info); + constexpr std::size_t next_pos = std::get<2>(bracket_info); + + if constexpr (is_field) { + constexpr std::string_view field_name = std::get<1>(bracket_info); + + auto obj_result = current.get_object(); + if (obj_result.error()) return obj_result.error(); + + auto obj = obj_result.value_unsafe(); + auto next_value = obj.find_field_unordered(field_name); + + return access_impl(next_value); + + } else { + constexpr std::size_t index = std::get<3>(bracket_info); + + auto arr_result = current.get_array(); + if (arr_result.error()) return arr_result.error(); + + auto arr = arr_result.value_unsafe(); + auto next_value = arr.at(index); + + return access_impl(next_value); + } + } else { + return access_impl(current); + } + } + + // Parse next field name + static consteval auto parse_next_field(std::size_t start) { + std::size_t i = start + 1; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != '.' && path_view[i] != '[') { + ++i; + } + std::string_view field_name = path_view.substr(field_start, i - field_start); + return std::make_tuple(field_name, i); + } + + // Parse bracket notation: returns (is_field, field_name, next_pos, index) + static consteval auto parse_bracket(std::size_t start) { + std::size_t i = start + 1; // skip '[' + + if (i < path_view.size() && (path_view[i] == '"' || path_view[i] == '\'')) { + // Field access + char quote = path_view[i]; + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != quote) { + ++i; + } + std::string_view field_name = path_view.substr(field_start, i - field_start); + if (i < path_view.size()) ++i; // skip closing quote + if (i < path_view.size() && path_view[i] == ']') ++i; + + return std::make_tuple(true, field_name, i, std::size_t(0)); + } else { + // Array index + std::size_t index = 0; + while (i < path_view.size() && path_view[i] >= '0' && path_view[i] <= '9') { + index = index * 10 + (path_view[i] - '0'); + ++i; + } + if (i < path_view.size() && path_view[i] == ']') ++i; + + return std::make_tuple(false, std::string_view{}, i, index); + } + } + +public: + // Check if reflected type is array-like (C-style array or indexable container) + // Uses reflection to test: 1) std::meta::is_array_type() for C arrays + // 2) std::meta::substitute() to test concepts::indexable_container concept + static consteval bool is_array_like_reflected(std::meta::info type_reflection) { + if (std::meta::is_array_type(type_reflection)) { + return true; + } + + if (std::meta::can_substitute(^^concepts::indexable_container_v, {type_reflection})) { + return std::meta::extract(std::meta::substitute(^^concepts::indexable_container_v, {type_reflection})); + } + return false; + } + + // Extract element type from reflected array or container + // For C arrays: uses std::meta::remove_extent() + // For containers: finds value_type member using std::meta::members_of() + static consteval std::meta::info get_element_type_reflected(std::meta::info type_reflection) { + if (std::meta::is_array_type(type_reflection)) { + return std::meta::remove_extent(type_reflection); + } + + auto members = std::meta::members_of(type_reflection, std::meta::access_context::unchecked()); + for (auto mem : members) { + if (std::meta::is_type(mem)) { + auto name = std::meta::identifier_of(mem); + if (name == "value_type") { + return mem; + } + } + } + return ^^void; + } + +private: + // Check if type has member with given name + template + static consteval bool has_member(std::string_view member_name) { + constexpr auto members = std::meta::nonstatic_data_members_of(^^Type, std::meta::access_context::unchecked()); + for (auto mem : members) { + if (std::meta::identifier_of(mem) == member_name) { + return true; + } + } + return false; + } + + // Get type of member by name + template + static consteval auto get_member_type(std::string_view member_name) { + constexpr auto members = std::meta::nonstatic_data_members_of(^^Type, std::meta::access_context::unchecked()); + for (auto mem : members) { + if (std::meta::identifier_of(mem) == member_name) { + return std::meta::type_of(mem); + } + } + return ^^void; + } + + // Check if non-reflected type is array-like + template + static consteval bool is_container_type() { + using BaseType = std::remove_cvref_t; + if constexpr (requires { typename BaseType::value_type; }) { + return true; + } + if constexpr (std::is_array_v) { + return true; + } + return false; + } + + // Extract element type from non-reflected container + template + using extract_element_type = std::conditional_t< + requires { typename std::remove_cvref_t::value_type; }, + typename std::remove_cvref_t::value_type, + std::conditional_t< + std::is_array_v>, + std::remove_extent_t>, + void + > + >; + + // Validate path matches struct definition + static consteval bool validate_path() { + if constexpr (!std::is_class_v) { + return true; + } + + auto current_type = ^^T; + std::size_t i = parser.skip_root(); + + while (i < path_view.size()) { + if (path_view[i] == '.') { + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != '.' && path_view[i] != '[') { + ++i; + } + + std::string_view field_name = path_view.substr(field_start, i - field_start); + + bool found = false; + auto members = std::meta::nonstatic_data_members_of(current_type, std::meta::access_context::unchecked()); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == field_name) { + current_type = std::meta::type_of(mem); + found = true; + break; + } + } + + if (!found) { + return false; + } + + } else if (path_view[i] == '[') { + ++i; + if (i >= path_view.size()) return false; + + if (path_view[i] == '"' || path_view[i] == '\'') { + char quote = path_view[i]; + ++i; + std::size_t field_start = i; + while (i < path_view.size() && path_view[i] != quote) { + ++i; + } + + std::string_view field_name = path_view.substr(field_start, i - field_start); + if (i < path_view.size()) ++i; + if (i < path_view.size() && path_view[i] == ']') ++i; + + bool found = false; + auto members = std::meta::nonstatic_data_members_of(current_type, std::meta::access_context::unchecked()); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == field_name) { + current_type = std::meta::type_of(mem); + found = true; + break; + } + } + + if (!found) { + return false; + } + + } else { + while (i < path_view.size() && path_view[i] >= '0' && path_view[i] <= '9') { + ++i; + } + + if (i < path_view.size() && path_view[i] == ']') ++i; + + if (!is_array_like_reflected(current_type)) { + return false; + } + + auto new_type = get_element_type_reflected(current_type); + + if (new_type == ^^void) { + return false; + } + + current_type = new_type; + } + } else { + ++i; + } + } + + return true; + } +}; + +// Compile-time path accessor with validation +template +inline simdjson_result<::simdjson::lasx::ondemand::value> at_path_compiled(DocOrValue& doc_or_val) noexcept { + using accessor = path_accessor; + return accessor::access(doc_or_val); +} + +// Overload without type parameter (no validation) +template +inline simdjson_result<::simdjson::lasx::ondemand::value> at_path_compiled(DocOrValue& doc_or_val) noexcept { + using accessor = path_accessor; + return accessor::access(doc_or_val); +} + +// ============================================================================ +// JSON Pointer Compile-Time Support (RFC 6901) +// ============================================================================ + +// JSON Pointer parser: /field/0/nested (slash-separated) +template +struct json_pointer_parser { + static constexpr std::string_view pointer_str = Pointer.view(); + + // Unescape token: ~0 -> ~, ~1 -> / + static consteval void unescape_token(std::string_view src, char* dest, std::size_t& out_len) { + out_len = 0; + for (std::size_t i = 0; i < src.size(); ++i) { + if (src[i] == '~' && i + 1 < src.size()) { + if (src[i + 1] == '0') { + dest[out_len++] = '~'; + ++i; + } else if (src[i + 1] == '1') { + dest[out_len++] = '/'; + ++i; + } else { + dest[out_len++] = src[i]; + } + } else { + dest[out_len++] = src[i]; + } + } + } + + // Check if token is numeric + static consteval bool is_numeric(std::string_view token) { + if (token.empty()) return false; + if (token[0] == '0' && token.size() > 1) return false; + for (char c : token) { + if (c < '0' || c > '9') return false; + } + return true; + } + + // Parse numeric token to index + static consteval std::size_t parse_index(std::string_view token) { + std::size_t result = 0; + for (char c : token) { + result = result * 10 + (c - '0'); + } + return result; + } + + // Count tokens in pointer + static consteval std::size_t count_tokens() { + if (pointer_str.empty() || pointer_str == "/") return 0; + + std::size_t count = 0; + std::size_t pos = pointer_str[0] == '/' ? 1 : 0; + + while (pos < pointer_str.size()) { + ++count; + std::size_t next_slash = pointer_str.find('/', pos); + if (next_slash == std::string_view::npos) break; + pos = next_slash + 1; + } + + return count; + } + + // Get Nth token + static consteval std::string_view get_token(std::size_t token_index) { + std::size_t pos = pointer_str[0] == '/' ? 1 : 0; + std::size_t current_token = 0; + + while (current_token < token_index) { + std::size_t next_slash = pointer_str.find('/', pos); + pos = next_slash + 1; + ++current_token; + } + + std::size_t token_end = pointer_str.find('/', pos); + if (token_end == std::string_view::npos) token_end = pointer_str.size(); + + return pointer_str.substr(pos, token_end - pos); + } +}; + +// JSON Pointer accessor +template +struct pointer_accessor { + using parser = json_pointer_parser; + static constexpr std::string_view pointer_view = Pointer.view(); + static constexpr std::size_t token_count = parser::count_tokens(); + + // Validate pointer against struct definition + static consteval bool validate_pointer() { + if constexpr (!std::is_class_v) { + return true; + } + + auto current_type = ^^T; + std::size_t pos = pointer_view[0] == '/' ? 1 : 0; + + while (pos < pointer_view.size()) { + // Extract token up to next / + std::size_t token_end = pointer_view.find('/', pos); + if (token_end == std::string_view::npos) token_end = pointer_view.size(); + + std::string_view token = pointer_view.substr(pos, token_end - pos); + + if (parser::is_numeric(token)) { + if (!path_accessor::is_array_like_reflected(current_type)) { + return false; + } + current_type = path_accessor::get_element_type_reflected(current_type); + } else { + bool found = false; + auto members = std::meta::nonstatic_data_members_of(current_type, std::meta::access_context::unchecked()); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == token) { + current_type = std::meta::type_of(mem); + found = true; + break; + } + } + + if (!found) return false; + } + + pos = token_end + 1; + } + + return true; + } + + // Recursive accessor + template + static inline simdjson_result access_impl(simdjson_result current) noexcept { + if constexpr (TokenIndex >= token_count) { + return current; + } else { + constexpr std::string_view token = parser::get_token(TokenIndex); + + if constexpr (parser::is_numeric(token)) { + constexpr std::size_t index = parser::parse_index(token); + auto arr = current.get_array().value_unsafe(); + auto next_value = arr.at(index); + return access_impl(next_value); + } else { + auto obj = current.get_object().value_unsafe(); + auto next_value = obj.find_field_unordered(token); + return access_impl(next_value); + } + } + } + + // Access JSON value at pointer + template + static inline simdjson_result access(DocOrValue& doc_or_val) noexcept { + if constexpr (std::is_class_v) { + constexpr bool pointer_valid = validate_pointer(); + static_assert(pointer_valid, "JSON Pointer does not match struct definition"); + } + + if (pointer_view.empty() || pointer_view == "/") { + if constexpr (requires { doc_or_val.get_value(); }) { + return doc_or_val.get_value(); + } else { + return doc_or_val; + } + } + + simdjson_result current = doc_or_val.get_value(); + return access_impl<0>(current); + } + + // Extract value at pointer directly into target with type validation + template + static inline error_code extract_field(DocOrValue& doc_or_val, FieldType& target) noexcept { + static_assert(std::is_class_v, "extract_field requires T to be a struct type for validation"); + + constexpr bool pointer_valid = validate_pointer(); + static_assert(pointer_valid, "JSON Pointer does not match struct definition"); + + constexpr auto final_type = get_final_type(); + static_assert(final_type == ^^FieldType, "Target type does not match the field type at the pointer"); + + simdjson_result current_value = doc_or_val.get_value(); + auto json_value = access_impl<0>(current_value); + if (json_value.error()) return json_value.error(); + + return json_value.get(target); + } + +private: + // Get final type by walking pointer through struct + template + static consteval std::enable_if_t, std::meta::info> get_final_type() { + auto current_type = ^^T; + std::size_t pos = pointer_view[0] == '/' ? 1 : 0; + + while (pos < pointer_view.size()) { + std::size_t token_end = pointer_view.find('/', pos); + if (token_end == std::string_view::npos) token_end = pointer_view.size(); + + std::string_view token = pointer_view.substr(pos, token_end - pos); + + if (parser::is_numeric(token)) { + current_type = path_accessor::get_element_type_reflected(current_type); + } else { + auto members = std::meta::nonstatic_data_members_of(current_type, std::meta::access_context::unchecked()); + + for (auto mem : members) { + if (std::meta::identifier_of(mem) == token) { + current_type = std::meta::type_of(mem); + break; + } + } + } + + pos = token_end + 1; + } + + return current_type; + } +}; + +// Compile-time JSON Pointer accessor with validation +template +inline simdjson_result<::simdjson::lasx::ondemand::value> at_pointer_compiled(DocOrValue& doc_or_val) noexcept { + using accessor = pointer_accessor; + return accessor::access(doc_or_val); +} + +// Overload without type parameter (no validation) +template +inline simdjson_result<::simdjson::lasx::ondemand::value> at_pointer_compiled(DocOrValue& doc_or_val) noexcept { + using accessor = pointer_accessor; + return accessor::access(doc_or_val); +} + +} // namespace json_path +} // namespace ondemand +} // namespace lasx +} // namespace simdjson + +#endif // SIMDJSON_SUPPORTS_CONCEPTS && SIMDJSON_STATIC_REFLECTION +#endif // SIMDJSON_GENERIC_ONDEMAND_COMPILE_TIME_ACCESSORS_H + +/* end file simdjson/generic/ondemand/compile_time_accessors.h for lasx */ + /* end file simdjson/generic/ondemand/amalgamated.h for lasx */ /* including simdjson/lasx/end.h: #include "simdjson/lasx/end.h" */ /* begin file simdjson/lasx/end.h */