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