DAW JSON Link
daw_json_serialize_impl.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 #pragma once
9 
10 #include "to_daw_json_string.h"
11 #include "version.h"
12 
13 #include <array>
14 #include <ciso646>
15 #include <cstddef>
16 #include <utility>
17 
18 namespace daw::json {
19  inline namespace DAW_JSON_VER {
20  namespace json_details {
21 
22  template<typename T, std::size_t Capacity>
23  struct basic_array_t {
24  static constexpr std::size_t capacity = Capacity;
25 
26  private:
27  std::size_t position{ };
28  std::string_view array[capacity]{ };
29 
30  public:
31  constexpr basic_array_t( ) = default;
32 
33  constexpr T const *data( ) const {
34  return array;
35  }
36 
37  constexpr T *data( ) {
38  return array;
39  }
40 
41  constexpr std::size_t size( ) const {
42  return position;
43  }
44 
45  constexpr void push_back( T const &v ) {
46  assert( position < capacity );
47  array[position] = v;
48  ++position;
49  }
50  };
51  template<typename T>
52  struct basic_array_t<T, 0> {
53  static constexpr std::size_t capacity = 0;
54 
55  constexpr basic_array_t( ) = default;
56 
57  constexpr T const *data( ) const {
58  return nullptr;
59  }
60 
61  constexpr T *data( ) {
62  return nullptr;
63  }
64 
65  constexpr std::size_t size( ) const {
66  return 0;
67  }
68  };
69 
70  /***
71  * Serialize items to an output iterator as members of a class
72  * @tparam JsonMembers member items in json_class
73  * @tparam OutputIterator An Output Iterator that allows writing
74  * character data
75  * @tparam Is index_sequence index into JsonMembers
76  * @tparam Tuple tuple type holding class members
77  * @tparam Value mapped class type to serialize
78  * @param it an Output Iterator to write char data to
79  * @param args A tuple of the member values
80  * @param value class to serialize
81  * @return The OutputIterator it at the final position
82  */
83  template<typename... JsonMembers, typename OutputIterator,
84  std::size_t... Is, typename Tuple, typename Value>
85  [[nodiscard]] inline constexpr OutputIterator
86  serialize_json_class( OutputIterator it, Tuple const &args,
87  Value const &value, std::index_sequence<Is...> ) {
88 
89  *it++ = '{';
90 
91  using visit_size = std::integral_constant<
92  std::size_t,
93  ( sizeof...( JsonMembers ) +
94  ( static_cast<std::size_t>( has_dependent_member_v<JsonMembers> ) +
95  ... + 0 ) )>;
96  auto visited_members =
98 
99  // Tag Members, if any. Putting them ahead means we can parse this
100  // faster in the future
101 
102  // Using list init to ensure serialization happens in order
103  bool is_first = true;
104 
105  // gcc complains when JsonMembers is empty
106  (void)visited_members;
107  (void)is_first;
108  {
109  using Names = fwd_pack<JsonMembers...>;
110  daw::Empty const expander[]{
112  Is, traits::nth_element<Is, JsonMembers...>, Names>(
113  is_first, it, args, value, visited_members ),
114  daw::Empty{ } )...,
115  daw::Empty{} };
116  (void)expander;
117  }
118 
119  // Regular Members
120  {
121  daw::Empty const expander[]{
122  ( to_json_str<Is, traits::nth_element<Is, JsonMembers...>>(
123  is_first, it, args, value, visited_members ),
124  daw::Empty{ } )...,
125  daw::Empty{} };
126  (void)expander;
127  }
128  *it++ = '}';
129  return it;
130  }
131 
132  template<typename... JsonMembers, typename OutputIterator, typename Tuple,
133  typename Value, std::size_t... Is>
134  [[nodiscard]] inline constexpr OutputIterator
135  serialize_ordered_json_class( OutputIterator it, Tuple const &args,
136  Value const &value,
137  std::index_sequence<Is...> ) {
138 
139  *it++ = '[';
140  size_t array_idx = 0;
141  (void)array_idx; // gcc was complaining on empty pack
142  Unused( value );
143  {
144  daw::Empty const expander[]{
145  ( to_json_ordered_str<Is, traits::nth_element<Is, JsonMembers...>>(
146  array_idx, it, args ),
147  daw::Empty{ } )...,
148  daw::Empty{} };
149  (void)expander;
150  }
151  *it++ = ']';
152  return it;
153  }
154  } // namespace json_details
155  } // namespace DAW_JSON_VER
156 } // namespace daw::json
Iterator & it
Definition: daw_json_traits.h:231
constexpr OutputIterator serialize_ordered_json_class(OutputIterator it, Tuple const &args, Value const &value, std::index_sequence< Is... >)
Definition: daw_json_serialize_impl.h:135
constexpr void to_json_ordered_str(std::size_t &array_idx, OutputIterator &it, Tuple< Args... > const &tp)
Definition: to_daw_json_string.h:1377
constexpr void dependent_member_to_json_str(bool &, OutputIterator const &, TpArgs const &, Value const &, VisitedMembers const &)
Definition: to_daw_json_string.h:1271
constexpr OutputIterator serialize_json_class(OutputIterator it, Tuple const &args, Value const &value, std::index_sequence< Is... >)
Definition: daw_json_serialize_impl.h:86
constexpr void to_json_str(bool &is_first, OutputIterator &it, Tuple const &tp, Value const &, Visited &visited_members)
Definition: to_daw_json_string.h:1342
Definition: daw_from_json.h:22
constexpr std::size_t size() const
Definition: daw_json_serialize_impl.h:65
constexpr T const * data() const
Definition: daw_json_serialize_impl.h:57
constexpr T * data()
Definition: daw_json_serialize_impl.h:61
Definition: daw_json_serialize_impl.h:23
constexpr T * data()
Definition: daw_json_serialize_impl.h:37
constexpr void push_back(T const &v)
Definition: daw_json_serialize_impl.h:45
static constexpr std::size_t capacity
Definition: daw_json_serialize_impl.h:24
constexpr T const * data() const
Definition: daw_json_serialize_impl.h:33
constexpr std::size_t size() const
Definition: daw_json_serialize_impl.h:41
#define DAW_JSON_VER
Definition: version.h:11