// Glaze Library // For the license information refer to glaze.hpp #pragma once #include "glaze/core/common.hpp" #include "glaze/core/wrappers.hpp" namespace glz { // ============================================================================ // Auto-inference of JSON type from custom read/write functions // ============================================================================ // These traits allow automatic variant type deduction and schema generation // for types with glz::custom read/write functions by inspecting the input parameter type. // // NOTE: If a type has both `mimic` and `glz::custom`, `custom` takes precedence // for JSON type inference (consistent with runtime behavior where custom read/write is used). namespace detail { // Helper to check if invoking meta::value with T& returns a custom_t template struct has_custom_meta_impl : std::false_type {}; template struct has_custom_meta_impl::value(std::declval()))>> { using result_type = decltype(glz::meta::value(std::declval())); static constexpr bool value = is_specialization_v; }; } template constexpr bool has_custom_meta_v = detail::has_custom_meta_impl::value; namespace detail { // Extract the input type from a custom read function // The second parameter of the read lambda determines the JSON type template struct custom_read_input_type { static constexpr bool has_custom = false; using type = void; }; template struct custom_read_input_type< T, std::enable_if_t && is_invocable_concrete::value(std::declval()))::from_t> && (glz::tuple_size_v::value( std::declval()))::from_t>> >= 2)>> { static constexpr bool has_custom = true; using CustomT = decltype(glz::meta::value(std::declval())); using From = typename CustomT::from_t; using Args = invocable_args_t; using type = std::decay_t>; }; } template using custom_read_input_t = typename detail::custom_read_input_type::type; // Concept: type has custom read that takes a numeric input template concept custom_num_t = has_custom_meta_v && num_t>; // Concept: type has custom read that takes a string input template concept custom_str_t = has_custom_meta_v && str_t>; // Concept: type has custom read that takes a bool input template concept custom_bool_t = has_custom_meta_v && bool_t>; } // namespace glz