DAW JSON Link
daw_json_serialize_policy.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 "version.h"
12 
13 #include "daw_json_option_bits.h"
18 
19 #include <daw/daw_move.h>
20 
21 #include <cstddef>
22 #include <iterator>
23 #include <memory>
24 
25 namespace daw::json {
26  inline namespace DAW_JSON_VER {
27  /***
28  * Create the parser options flag for BasicParsePolicy
29  * @tparam Policies Policy types that satisfy the `is_policy_flag` trait.
30  * @param policies A list of parser options to change from the defaults.
31  * @return A json_options_t that encodes the options for the parser
32  */
33  template<typename... Policies>
34  constexpr json_options_t serialize_options( Policies... policies ) {
35  static_assert( json_details::are_option_flags<Policies...>,
36  "Only registered policy types are allowed" );
37  auto result = json_details::serialization::default_policy_flag;
38  if constexpr( sizeof...( Policies ) > 0 ) {
39  result |=
40  ( json_details::serialization::set_bits_for( policies ) | ... );
41  }
42  return result;
43  }
44  namespace json_details {
45  template<typename T>
46  DAW_ATTRIB_INLINE constexpr daw::string_view as_sv( T const &value ) {
47  if constexpr( std::is_same_v<char, DAW_TYPEOF( value )> ) {
48  return daw::string_view( &value, 1 );
49  } else {
50  return daw::string_view( value );
51  }
52  }
53  } // namespace json_details
54 
55  template<typename WritableType,
56  json_options_t PolicyFlags =
57  json_details::serialization::default_policy_flag>
59  static_assert(
60  concepts::is_writable_output_type_v<WritableType>,
61  "Output type does not have a writeable_output_trait specialization" );
63  WritableType *m_writable;
64 
65  DAW_ATTRIB_INLINE static DAW_CONSTEVAL json_options_t policy_flags( ) {
66  return PolicyFlags;
67  }
68 
69  std::size_t indentation_level = 0;
70 
71  DAW_ATTRIB_INLINE constexpr WritableType const &get( ) const {
72  return *m_writable;
73  }
74 
75  DAW_ATTRIB_INLINE constexpr WritableType &get( ) {
76  return *m_writable;
77  }
78 
79  DAW_ATTRIB_INLINE explicit constexpr serialization_policy( WritableType &writable )
80  : m_writable( std::addressof( writable ) ) {}
81 
82  static constexpr options::SerializationFormat serialization_format =
83  json_details::serialization::get_bits_for<options::SerializationFormat>(
84  PolicyFlags );
85 
86  static constexpr options::IndentationType indentation_type =
87  json_details::serialization::get_bits_for<options::IndentationType>(
88  PolicyFlags );
89 
90  static constexpr options::RestrictedStringOutput
91  restricted_string_output = json_details::serialization::get_bits_for<
92  options::RestrictedStringOutput>( PolicyFlags );
93 
94  static constexpr options::NewLineDelimiter newline_delimiter =
95  json_details::serialization::get_bits_for<options::NewLineDelimiter>(
96  PolicyFlags );
97 
98  static constexpr options::OutputTrailingComma output_trailing_comma =
99  json_details::serialization::get_bits_for<options::OutputTrailingComma>(
100  PolicyFlags );
101 
102  DAW_ATTRIB_INLINE constexpr void add_indent( ) {
103  if constexpr( serialization_format !=
104  options::SerializationFormat::Minified ) {
105  ++indentation_level;
106  }
107  }
108 
109  DAW_ATTRIB_INLINE constexpr void del_indent( ) {
110  if constexpr( serialization_format !=
111  options::SerializationFormat::Minified ) {
112  --indentation_level;
113  }
114  }
115 
116  DAW_ATTRIB_INLINE constexpr void output_indent( ) {
117  if constexpr( serialization_format !=
118  options::SerializationFormat::Minified ) {
119  constexpr std::string_view indent =
120  json_details::serialization::generate_indent<serialization_format,
121  indentation_type>;
122  if( not indent.empty( ) ) {
123  for( std::size_t n = 0; n < indentation_level; ++n ) {
124  write_output( *m_writable, indent );
125  }
126  }
127  }
128  }
129 
130  static constexpr std::string_view newline =
131  newline_delimiter == options::NewLineDelimiter::n ? "\n" : "\r\n";
132 
133  DAW_ATTRIB_INLINE constexpr void output_newline( ) {
134  if constexpr( serialization_format !=
135  options::SerializationFormat::Minified ) {
136  write_output( *m_writable, newline );
137  }
138  }
139 
140  static constexpr daw::string_view space =
141  serialization_format != options::SerializationFormat::Minified
142  ? " "
143  : nullptr;
144 
145  DAW_ATTRIB_INLINE constexpr void next_member( ) {
146  output_newline( );
147  output_indent( );
148  }
149 
150  template<typename ContiguousCharRange, typename... ContiguousCharRanges>
151  DAW_ATTRIB_FLATINLINE constexpr void
152  write( ContiguousCharRange const &chr,
153  ContiguousCharRanges const &...chrs ) {
154 
155  write_output( *m_writable, json_details::as_sv( chr ),
156  json_details::as_sv( chrs )... );
157  }
158 
159  DAW_ATTRIB_INLINE constexpr void copy_buffer( char const *first,
160  char const *last ) {
161  write(
162  daw::string_view( first, static_cast<std::size_t>( last - first ) ) );
163  }
164 
165  DAW_ATTRIB_FLATINLINE constexpr void put( char c ) {
166  put_output( *m_writable, c );
167  }
168 
169  DAW_ATTRIB_INLINE constexpr serialization_policy &operator=( char c ) {
170  put_output( *m_writable, c );
171  return *this;
172  }
173 
174  DAW_ATTRIB_INLINE constexpr serialization_policy &operator*( ) {
175  return *this;
176  }
177 
178  DAW_ATTRIB_INLINE constexpr serialization_policy &operator++( ) {
179  return *this;
180  }
181 
182  DAW_ATTRIB_INLINE constexpr serialization_policy operator++( int ) & {
183  return *this;
184  }
185  };
186 
187  template<typename, typename...>
188  inline constexpr bool is_serialization_policy_v = false;
189 
190  template<typename OutputIterator, json_options_t PolicyFlags>
191  inline constexpr bool is_serialization_policy_v<
193  } // namespace DAW_JSON_VER
194 } // namespace daw::json
constexpr json_options_t serialize_options(Policies... policies)
constexpr decltype(auto) get(basic_json_pair< PolicyFlags, Allocator > const &parse_state)
constexpr WritableOutput & put_output(WritableOutput &out, CharT c)
constexpr WritableOutput & write_output(WritableOutput &out, StringViews const &...svs)
Customization point traits.
constexpr void write(ContiguousCharRange const &chr, ContiguousCharRanges const &...chrs)
#define DAW_JSON_VER
The version string used in namespace definitions. Must be a valid namespace name.
Definition: version.h:25