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 "daw_json_to_string.h"
11 
12 #include <ciso646>
13 
14 namespace daw::json::json_details {
15  /***
16  * Serialize items to an output iterator as members of a class
17  * @tparam JsonMembers member items in json_class
18  * @tparam OutputIterator An Output Iterator that allows writing character
19  * data
20  * @tparam Is index_sequence index into JsonMembers
21  * @tparam Tuple tuple type holding class members
22  * @tparam Value mapped class type to serialize
23  * @param it an Output Iterator to write char data to
24  * @param args A tuple of the member values
25  * @param value class to serialize
26  * @return The OutputIterator it at the final position
27  */
28  template<typename... JsonMembers, typename OutputIterator, std::size_t... Is,
29  typename Tuple, typename Value>
30  [[nodiscard]] inline constexpr OutputIterator
31  serialize_json_class( OutputIterator it, std::index_sequence<Is...>,
32  Tuple const &args, Value const &value ) {
33 
34  bool is_first = true;
35  *it++ = '{';
36 
37  auto visited_members =
38  daw::bounded_vector_t<daw::string_view, sizeof...( JsonMembers ) * 2U>{ };
39  // Tag Members, if any. Putting them ahead means we can parse this faster
40  // in the future
41  (void)( ( tags_to_json_str<Is,
42  daw::traits::nth_element<Is, JsonMembers...>>(
43  is_first, it, value, visited_members ),
44  ... ),
45  0 );
46  // Regular Members
47  (void)( ( to_json_str<Is, daw::traits::nth_element<Is, JsonMembers...>>(
48  is_first, it, args, value, visited_members ),
49  ... ),
50  0 );
51 
52  *it++ = '}';
53  return it;
54  }
55 
56  template<typename... JsonMembers, typename OutputIterator, typename Tuple,
57  typename Value, std::size_t... Is>
58  [[nodiscard]] inline constexpr OutputIterator
59  serialize_ordered_json_class( OutputIterator it, std::index_sequence<Is...>,
60  Tuple const &args, Value const &value ) {
61 
62  *it++ = '[';
63  size_t array_idx = 0;
64  Unused( value );
65  (void)std::array{
66  ( to_json_ordered_str<Is, daw::traits::nth_element<Is, JsonMembers...>>(
67  array_idx, it, args ),
68  0 )... };
69 
70  *it++ = ']';
71  return it;
72  }
73 } // namespace daw::json::json_details
daw_json_to_string.h