DAW JSON Link
Loading...
Searching...
No Matches
daw_json_boost_pfr_mapping.h
Go to the documentation of this file.
1// Copyright (c) Darrell Wright
2//
3// Distributed under the Boost Software License, Version 1.0. (See accompanying
4// file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
5//
6// Official repository: https://github.com/beached/daw_json_link
7//
8
9#pragma once
10
11#include "daw/daw_tuple_forward.h"
13
14#include <daw/daw_attributes.h>
15#include <daw/daw_consteval.h>
16
17#include <boost/pfr.hpp>
18#include <daw/stdinc/tuple_traits.h>
19#include <type_traits>
20#include <utility>
21
22namespace daw::json {
25 template<typename T>
26 requires( std::is_aggregate_v<T> and std::is_class_v<T> and
27 not std::is_empty_v<T> ) //
28 inline constexpr bool use_boost_pfr = false;
29
30 namespace pfr_details {
31 template<typename T, std::size_t Idx>
32 DAW_CONSTEVAL auto get_member_name( ) {
33 DAW_CPP23_STATIC_LOCAL constexpr auto name =
34 boost::pfr::get_name<Idx, T>( );
35 static_assert( not name.empty( ) );
36 return json_name<name.size( ) + 1>(
37 name.data( ), std::make_index_sequence<name.size( ) + 1>{ } );
38 }
39
40 template<typename, typename>
41 struct make_data_contract;
42
43 template<typename T, std::size_t... Is>
44 struct make_data_contract<T, std::index_sequence<Is...>> {
45 private:
46 using member_types = DAW_TYPEOF(
47 boost::pfr::structure_to_tuple( std::declval<T const &>( ) ) );
48
49 public:
50 using type =
52 std::tuple_element_t<Is, member_types>>...>;
53
54 DAW_ATTRIB_INLINE static constexpr auto to_json_data( T const &value ) {
55 return boost::pfr::structure_tie( value );
56 }
57 };
58 } // namespace pfr_details
59
60 template<typename T>
61 requires use_boost_pfr<T> //
63 : pfr_details::make_data_contract<
64 T, std::make_index_sequence<boost::pfr::tuple_size_v<T>>> {};
65} // namespace daw::json
Customization point traits.
constexpr bool use_boost_pfr
Enable Boost PFR mappings for a type.
Mapping class for JSON data structures to C++. It must be specialized in order to parse to a user cla...