DAW JSON Link
daw_json_type_options.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_json_enums.h"
13
14namespace daw::json {
15 inline namespace DAW_JSON_VER {
16 /***
17 * Control whether a type can be missing or null.
18 * MustExist - members make it an error if their value is null or they are
19 * missing
20 * Nullable - members can have a value of null or be missing
21 * NullVisible - members must exist but can have a value of null
22 */
23 namespace json_details {
24 template<>
25 inline constexpr unsigned json_option_bits_width<JsonNullable> = 2;
26
27 template<>
30 } // namespace json_details
31
38 enum class LiteralAsStringOpt : unsigned { Never, Maybe, Always }; // 2bits
39
40 namespace json_details {
41 template<>
42 inline constexpr unsigned json_option_bits_width<LiteralAsStringOpt> = 2;
43
44 template<>
47 } // namespace json_details
48
49 /***
50 * Check if the result of a numeric parse is within the range of the type
51 */
52 enum class JsonRangeCheck : unsigned { Never, CheckForNarrowing }; // 1bit
53
54 namespace json_details {
55 template<>
56 inline constexpr unsigned json_option_bits_width<JsonRangeCheck> = 1;
57
58 template<>
61 } // namespace json_details
62
63 enum class JsonNumberErrors : unsigned {
64 /* Do not allow serialization/parsing of NaN/Inf */
65 None,
66 /* Allow NaN to be expressed/parsed if number can be a string */
68 /* Allow Inf/-Inf to be expressed/parsed if number can be a string */
70 /* Allow NaN/Inf/-Inf to be expressed/parsed if number can be a string */
72 };
73
74 namespace json_details {
75 template<>
76 inline constexpr unsigned json_option_bits_width<JsonNumberErrors> = 2;
77
78 template<>
81 } // namespace json_details
82
83 enum class FPOutputFormat : unsigned {
84 /* Automatically choose between decimal and scientific output formats */
85 Auto,
86 /* Always format in terms of an exponent <whole>[.fraction]e<exponent> */
88 /* Always format in <whole>.<fraction> */
89 // Decimal
90 };
91
92 namespace json_details {
93 template<>
94 inline constexpr unsigned json_option_bits_width<FPOutputFormat> = 2;
95
96 template<>
99 } // namespace json_details
100
101 // json_number
103 json_details::JsonOptionList<JsonNullable, LiteralAsStringOpt,
106
107 inline constexpr auto number_opts = number_opts_t{ };
110
111 template<typename... Options>
112 constexpr json_details::json_options_t number_opt( Options... options ) {
113 return number_opts_t::options( options... );
114 }
115
116 // json_bool
118 json_details::JsonOptionList<JsonNullable, LiteralAsStringOpt>;
119
120 inline constexpr auto bool_opts = bool_opts_t{ };
123
124 template<typename... Options>
125 constexpr json_details::json_options_t bool_opt( Options... options ) {
126 return bool_opts_t::options( options... );
127 }
128
129 /***
130 * Treat empty string values as a null when parsing
131 */
132 enum class EmptyStringNull : unsigned { no, yes }; // 1bit
133
134 namespace json_details {
135 template<>
136 inline constexpr unsigned json_option_bits_width<EmptyStringNull> = 1;
137
138 template<>
141 } // namespace json_details
142
143 enum class EightBitModes : unsigned {
144 DisallowHigh = false,
145 AllowFull = true
146 }; // 1bit
147
148 namespace json_details {
149 template<>
150 inline constexpr unsigned json_option_bits_width<EightBitModes> = 1;
151
152 template<>
155 } // namespace json_details
156
157 // json_string
159 json_details::JsonOptionList<JsonNullable, EightBitModes,
161
162 inline constexpr auto string_opts = string_opts_t{ };
165
166 template<typename... Options>
167 constexpr json_details::json_options_t string_opt( Options... options ) {
168 return string_opts_t::options( options... );
169 }
170
171 /***
172 * In RAW String processing, if we know that there are no escaped double
173 * quotes \" we can stop at the first double quote
174 */
175 enum class AllowEscapeCharacter : unsigned {
176 Allow,
179 };
180 namespace json_details {
181 template<>
183 1;
184
185 template<>
188 } // namespace json_details
189
190 // json_string_raw
192 json_details::JsonOptionList<JsonNullable, EightBitModes, EmptyStringNull,
194
195 inline constexpr auto string_raw_opts = string_raw_opts_t{ };
198
199 template<typename... Options>
201 string_raw_opt( Options... options ) {
202 return string_raw_opts_t::options( options... );
203 }
204
205 // json_class
206 using class_opts_t = json_details::JsonOptionList<JsonNullable>;
207 inline constexpr auto class_opts = class_opts_t{ };
210
211 template<typename... Options>
212 constexpr json_details::json_options_t class_opt( Options... options ) {
213 return class_opts_t::options( options... );
214 }
215
216 // json_tuple
217 using tuple_opts_t = json_details::JsonOptionList<JsonNullable>;
218 inline constexpr auto tuple_opts = tuple_opts_t{ };
221
222 template<typename... Options>
223 constexpr json_details::json_options_t tuple_opt( Options... options ) {
224 return tuple_opts_t::options( options... );
225 }
226
227 /***
228 * Custom JSON types can be Strings(default), unquoted Literals, or a mix
229 * String - Parser always expects a JSON string. Will surround serialized
230 * value with double quotes
231 * Literal - Parser will expect a valid JSON literal number, bool, null
232 * Any - Experimental - Parser will return any valid JSON value excluding
233 * leading whitespace. strings will be quoted. Any is suitable for
234 * constructing a json_value to allow adhock parsing if json_raw is not
235 * suitable
236 */
237 enum class JsonCustomTypes : unsigned { String, Literal, Any }; // 2 bits
238
239 namespace json_details {
240 template<>
241 inline constexpr unsigned json_option_bits_width<JsonCustomTypes> = 2;
242
243 template<>
246 } // namespace json_details
247
248 // json_custom
250 json_details::JsonOptionList<JsonNullable, JsonCustomTypes>;
251
252 inline constexpr auto json_custom_opts = json_custom_opts_t{ };
255
256 template<typename... Options>
258 json_custom_opt( Options... options ) {
259 return json_custom_opts_t::options( options... );
260 }
261 } // namespace DAW_JSON_VER
262} // namespace daw::json
constexpr auto default_json_option_value< JsonNumberErrors >
Definition: daw_json_type_options.h:79
constexpr auto default_json_option_value< LiteralAsStringOpt >
Definition: daw_json_type_options.h:45
constexpr unsigned json_option_bits_width< EmptyStringNull >
Definition: daw_json_type_options.h:136
constexpr auto default_json_option_value< EmptyStringNull >
Definition: daw_json_type_options.h:139
constexpr auto default_json_option_value< JsonCustomTypes >
Definition: daw_json_type_options.h:244
constexpr auto default_json_option_value< AllowEscapeCharacter >
Definition: daw_json_type_options.h:186
constexpr unsigned json_option_bits_width< LiteralAsStringOpt >
Definition: daw_json_type_options.h:42
constexpr unsigned json_option_bits_width< JsonNullable >
Definition: daw_json_type_options.h:25
constexpr auto default_json_option_value< JsonNullable >
Definition: daw_json_type_options.h:28
constexpr unsigned json_option_bits_width< FPOutputFormat >
Definition: daw_json_type_options.h:94
constexpr unsigned json_option_bits_width< JsonNumberErrors >
Definition: daw_json_type_options.h:76
std::uint32_t json_options_t
Definition: daw_json_option_bits.h:23
constexpr auto default_json_option_value< JsonRangeCheck >
Definition: daw_json_type_options.h:59
constexpr unsigned json_option_bits_width< JsonCustomTypes >
Definition: daw_json_type_options.h:241
constexpr auto default_json_option_value< FPOutputFormat >
Definition: daw_json_type_options.h:97
constexpr auto default_json_option_value< EightBitModes >
Definition: daw_json_type_options.h:153
constexpr unsigned json_option_bits_width< EightBitModes >
Definition: daw_json_type_options.h:150
constexpr unsigned json_option_bits_width< JsonRangeCheck >
Definition: daw_json_type_options.h:56
constexpr unsigned json_option_bits_width< AllowEscapeCharacter >
Definition: daw_json_type_options.h:182
constexpr json_details::json_options_t string_opts_def
Definition: daw_json_type_options.h:163
EightBitModes
Definition: daw_json_type_options.h:143
LiteralAsStringOpt
Definition: daw_json_type_options.h:38
constexpr json_details::json_options_t json_custom_opts_def
Definition: daw_json_type_options.h:253
constexpr auto bool_opts
Definition: daw_json_type_options.h:120
JsonCustomTypes
Definition: daw_json_type_options.h:237
constexpr json_details::json_options_t json_custom_opt(Options... options)
Definition: daw_json_type_options.h:258
json_details::JsonOptionList< JsonNullable, EightBitModes, EmptyStringNull, AllowEscapeCharacter > string_raw_opts_t
Definition: daw_json_type_options.h:193
json_details::JsonOptionList< JsonNullable, JsonCustomTypes > json_custom_opts_t
Definition: daw_json_type_options.h:250
constexpr auto string_raw_opts
Definition: daw_json_type_options.h:195
constexpr json_details::json_options_t tuple_opts_def
Definition: daw_json_type_options.h:219
JsonNumberErrors
Definition: daw_json_type_options.h:63
JsonNullable
Definition: daw_json_enums.h:90
constexpr json_details::json_options_t number_opt(Options... options)
Definition: daw_json_type_options.h:112
constexpr json_details::json_options_t class_opts_def
Definition: daw_json_type_options.h:208
constexpr json_details::json_options_t bool_opts_def
Definition: daw_json_type_options.h:121
json_details::JsonOptionList< JsonNullable > class_opts_t
Definition: daw_json_type_options.h:206
AllowEscapeCharacter
Definition: daw_json_type_options.h:175
@ NotBeforeDblQuote
Full string processing to skip escaped characters.
constexpr json_details::json_options_t string_raw_opt(Options... options)
Definition: daw_json_type_options.h:201
EmptyStringNull
Definition: daw_json_type_options.h:132
constexpr auto tuple_opts
Definition: daw_json_type_options.h:218
JsonRangeCheck
Definition: daw_json_type_options.h:52
constexpr json_details::json_options_t string_raw_opts_def
Definition: daw_json_type_options.h:196
json_details::JsonOptionList< JsonNullable, LiteralAsStringOpt, JsonRangeCheck, JsonNumberErrors, FPOutputFormat > number_opts_t
Definition: daw_json_type_options.h:105
constexpr auto json_custom_opts
Definition: daw_json_type_options.h:252
json_details::JsonOptionList< JsonNullable > tuple_opts_t
Definition: daw_json_type_options.h:217
constexpr json_details::json_options_t tuple_opt(Options... options)
Definition: daw_json_type_options.h:223
constexpr json_details::json_options_t bool_opt(Options... options)
Definition: daw_json_type_options.h:125
constexpr auto class_opts
Definition: daw_json_type_options.h:207
constexpr auto string_opts
Definition: daw_json_type_options.h:162
FPOutputFormat
Definition: daw_json_type_options.h:83
constexpr json_details::json_options_t number_opts_def
Definition: daw_json_type_options.h:108
constexpr json_details::json_options_t string_opt(Options... options)
Definition: daw_json_type_options.h:167
constexpr auto number_opts
Definition: daw_json_type_options.h:107
constexpr json_details::json_options_t class_opt(Options... options)
Definition: daw_json_type_options.h:212
json_details::JsonOptionList< JsonNullable, EightBitModes, EmptyStringNull > string_opts_t
Definition: daw_json_type_options.h:160
json_details::JsonOptionList< JsonNullable, LiteralAsStringOpt > bool_opts_t
Definition: daw_json_type_options.h:118
Definition: daw_from_json.h:22
static constexpr json_options_t default_option_flag
Definition: daw_json_option_bits.h:100
static constexpr json_options_t options(Options... options)
Definition: daw_json_option_bits.h:112
#define DAW_JSON_VER
The version string used in namespace definitions. Must be a valid namespace name.
Definition: version.h:16