#ifndef SIMDJSON_CONVERT_H #define SIMDJSON_CONVERT_H #include "simdjson/ondemand.h" #include #if SIMDJSON_SUPPORTS_CONCEPTS namespace simdjson { namespace convert { namespace internal { /** * A utility class for automatically parsing JSON documents. * This template is NOT part of our public API. * It is subject to changes. * @private */ template struct auto_parser { private: parser_type m_parser; ondemand::document m_doc; error_code m_error{SUCCESS}; template static constexpr bool is_nothrow_gettable = requires(ondemand::document doc) { { doc.get() } noexcept; }; public: explicit auto_parser(parser_type &&parser, ondemand::document &&doc) noexcept requires(!std::is_pointer_v); explicit auto_parser(parser_type &&parser, padded_string_view const str) noexcept requires(!std::is_pointer_v); explicit auto_parser(std::remove_pointer_t &parser, ondemand::document &&doc) noexcept requires(std::is_pointer_v); explicit auto_parser(std::remove_pointer_t &parser, padded_string_view const str) noexcept requires(std::is_pointer_v); explicit auto_parser(padded_string_view const str) noexcept requires(std::is_pointer_v); explicit auto_parser(parser_type parser, ondemand::document &&doc) noexcept requires(std::is_pointer_v); auto_parser(auto_parser const &) = delete; auto_parser &operator=(auto_parser const &) = delete; ~auto_parser() = default; // Prevent moving auto_parser(auto_parser&&) = delete; auto_parser &operator=(auto_parser &&) noexcept = delete; simdjson_warn_unused std::remove_pointer_t &parser() noexcept; template simdjson_warn_unused simdjson_inline simdjson_result result() noexcept(is_nothrow_gettable); template simdjson_warn_unused simdjson_inline error_code get(T &value) && noexcept(is_nothrow_gettable); simdjson_warn_unused simdjson_inline simdjson_result array() noexcept; simdjson_warn_unused simdjson_inline simdjson_result object() noexcept; simdjson_warn_unused simdjson_inline simdjson_result number() noexcept; #if SIMDJSON_EXCEPTIONS template simdjson_warn_unused simdjson_inline explicit(false) operator T() noexcept(false); #endif // SIMDJSON_EXCEPTIONS template simdjson_warn_unused simdjson_inline std::optional optional() noexcept(is_nothrow_gettable); }; /** * A utility class for adapting values for the `auto_parser`. * This template is not part of our public API. It is subject to changes. * @private */ template struct to_adaptor { T operator()(simdjson_result &val) const noexcept; auto operator()(padded_string_view const str) const noexcept; auto operator()(ondemand::parser &parser, padded_string_view const str) const noexcept; }; // deduction guide auto_parser(padded_string_view const str) -> auto_parser; } // namespace internal } // namespace convert /** * The simdjson::from instance is EXPERIMENTAL AND SUBJECT TO CHANGES. * * The `from` instance is a utility adaptor for parsing JSON strings into objects. * * The string must be a simdjson::padded_string_view, which can be created from a std::string * with simdjson::pad(), from a simdjson::padded_string, or string literal using the `_padded` * user-defined literal. * * The `from` instance provides a convenient way to convert JSON data into C++ objects using the `auto_parser`. * * Example usage: * * ```cpp * std::map obj = * simdjson::from(R"({"key": "value"})"_padded); * ``` * * This will parse the JSON string and return an object representation. By default, we * use the simdjson::ondemand::parser::get_parser() instance. A parser instance should * be used for just one document at a time. * * You can also pass you own parser instance: * ```cpp * simdjson::ondemand::parser parser; * std::map obj = * simdjson::from(parser, R"({"key": "value"})"_padded); * ``` * The parser instance can be reused. * * This functionality requires C++20 or better. */ static constexpr convert::internal::to_adaptor<> from{}; } // namespace simdjson #endif // SIMDJSON_SUPPORTS_CONCEPTS #endif // SIMDJSON_CONVERT_H