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"
12 #include "daw_json_option_bits.h"
13 
14 namespace 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  // json_number
64  using number_opts_t =
65  json_details::JsonOptionList<JsonNullable, LiteralAsStringOpt,
67 
68  inline constexpr auto number_opts = number_opts_t{ };
71 
72  template<typename... Options>
73  constexpr json_details::json_options_t number_opt( Options... options ) {
74  return number_opts_t::options( options... );
75  }
76 
77  // json_bool
78  using bool_opts_t =
79  json_details::JsonOptionList<JsonNullable, LiteralAsStringOpt>;
80 
81  inline constexpr auto bool_opts = bool_opts_t{ };
84 
85  template<typename... Options>
86  constexpr json_details::json_options_t bool_opt( Options... options ) {
87  return bool_opts_t::options( options... );
88  }
89 
90  /***
91  * Treat empty string values as a null when parsing
92  */
93  enum class EmptyStringNull : unsigned { no, yes }; // 1bit
94 
95  namespace json_details {
96  template<>
97  inline constexpr unsigned json_option_bits_width<EmptyStringNull> = 1;
98 
99  template<>
102  } // namespace json_details
103 
104  enum class EightBitModes : unsigned {
105  DisallowHigh = false,
106  AllowFull = true
107  }; // 1bit
108 
109  namespace json_details {
110  template<>
111  inline constexpr unsigned json_option_bits_width<EightBitModes> = 1;
112 
113  template<>
116  } // namespace json_details
117 
118  // json_string
120  json_details::JsonOptionList<JsonNullable, EightBitModes,
122 
123  inline constexpr auto string_opts = string_opts_t{ };
126 
127  template<typename... Options>
128  constexpr json_details::json_options_t string_opt( Options... options ) {
129  return string_opts_t::options( options... );
130  }
131 
132  /***
133  * In RAW String processing, if we know that there are no escaped double
134  * quotes \" we can stop at the first double quote
135  */
136  enum class AllowEscapeCharacter : unsigned {
137  /*Full string processing to skip escaped characters*/ Allow,
138  /*There will never be a \" sequence inside the string*/ NotBeforeDblQuote
139  };
140  namespace json_details {
141  template<>
143  1;
144 
145  template<>
148  } // namespace json_details
149 
150  // json_string_raw
152  json_details::JsonOptionList<JsonNullable, EightBitModes, EmptyStringNull,
154 
155  inline constexpr auto string_raw_opts = string_raw_opts_t{ };
158 
159  template<typename... Options>
161  string_raw_opt( Options... options ) {
162  return string_raw_opts_t::options( options... );
163  }
164 
165  // json_class
166  using class_opts_t = json_details::JsonOptionList<JsonNullable>;
167  inline constexpr auto class_opts = class_opts_t{ };
170 
171  template<typename... Options>
172  constexpr json_details::json_options_t class_opt( Options... options ) {
173  return class_opts_t::options( options... );
174  }
175 
176  // json_tuple
177  using tuple_opts_t = json_details::JsonOptionList<JsonNullable>;
178  inline constexpr auto tuple_opts = tuple_opts_t{ };
181 
182  template<typename... Options>
183  constexpr json_details::json_options_t tuple_opt( Options... options ) {
184  return tuple_opts_t::options( options... );
185  }
186 
187  /***
188  * Custom JSON types can be Strings(default), unquoted Literals, or a mix
189  * String - Parser always expects a JSON string. Will surround serialized
190  * value with double quotes
191  * Literal - Parser will expect a valid JSON literal number, bool, null
192  * Any - Experimental - Parser will return any valid JSON value excluding
193  * leading whitespace. strings will be quoted. Any is suitable for
194  * constructing a json_value to allow adhock parsing if json_raw is not
195  * suitable
196  */
197  enum class JsonCustomTypes : unsigned { String, Literal, Any }; // 2 bits
198 
199  namespace json_details {
200  template<>
201  inline constexpr unsigned json_option_bits_width<JsonCustomTypes> = 2;
202 
203  template<>
206  } // namespace json_details
207 
208  // json_custom
210  json_details::JsonOptionList<JsonNullable, JsonCustomTypes>;
211 
212  inline constexpr auto json_custom_opts = json_custom_opts_t{ };
215 
216  template<typename... Options>
218  json_custom_opt( Options... options ) {
219  return json_custom_opts_t::options( options... );
220  }
221  } // namespace DAW_JSON_VER
222 } // namespace daw::json
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:97
constexpr auto default_json_option_value< EmptyStringNull >
Definition: daw_json_type_options.h:100
constexpr auto default_json_option_value< JsonCustomTypes >
Definition: daw_json_type_options.h:204
constexpr auto default_json_option_value< AllowEscapeCharacter >
Definition: daw_json_type_options.h:146
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
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:201
constexpr auto default_json_option_value< EightBitModes >
Definition: daw_json_type_options.h:114
constexpr unsigned json_option_bits_width< EightBitModes >
Definition: daw_json_type_options.h:111
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:142
constexpr json_details::json_options_t string_opts_def
Definition: daw_json_type_options.h:124
EightBitModes
Definition: daw_json_type_options.h:104
LiteralAsStringOpt
Definition: daw_json_type_options.h:38
constexpr json_details::json_options_t json_custom_opts_def
Definition: daw_json_type_options.h:213
constexpr auto bool_opts
Definition: daw_json_type_options.h:81
JsonCustomTypes
Definition: daw_json_type_options.h:197
constexpr json_details::json_options_t json_custom_opt(Options... options)
Definition: daw_json_type_options.h:218
json_details::JsonOptionList< JsonNullable, EightBitModes, EmptyStringNull, AllowEscapeCharacter > string_raw_opts_t
Definition: daw_json_type_options.h:153
json_details::JsonOptionList< JsonNullable, LiteralAsStringOpt, JsonRangeCheck > number_opts_t
Definition: daw_json_type_options.h:66
json_details::JsonOptionList< JsonNullable, JsonCustomTypes > json_custom_opts_t
Definition: daw_json_type_options.h:210
constexpr auto string_raw_opts
Definition: daw_json_type_options.h:155
constexpr json_details::json_options_t tuple_opts_def
Definition: daw_json_type_options.h:179
JsonNullable
Definition: daw_json_enums.h:77
constexpr json_details::json_options_t number_opt(Options... options)
Definition: daw_json_type_options.h:73
constexpr json_details::json_options_t class_opts_def
Definition: daw_json_type_options.h:168
constexpr json_details::json_options_t bool_opts_def
Definition: daw_json_type_options.h:82
json_details::JsonOptionList< JsonNullable > class_opts_t
Definition: daw_json_type_options.h:166
AllowEscapeCharacter
Definition: daw_json_type_options.h:136
constexpr json_details::json_options_t string_raw_opt(Options... options)
Definition: daw_json_type_options.h:161
EmptyStringNull
Definition: daw_json_type_options.h:93
constexpr auto tuple_opts
Definition: daw_json_type_options.h:178
JsonRangeCheck
Definition: daw_json_type_options.h:52
constexpr json_details::json_options_t string_raw_opts_def
Definition: daw_json_type_options.h:156
constexpr auto json_custom_opts
Definition: daw_json_type_options.h:212
json_details::JsonOptionList< JsonNullable > tuple_opts_t
Definition: daw_json_type_options.h:177
constexpr json_details::json_options_t tuple_opt(Options... options)
Definition: daw_json_type_options.h:183
constexpr json_details::json_options_t bool_opt(Options... options)
Definition: daw_json_type_options.h:86
constexpr auto class_opts
Definition: daw_json_type_options.h:167
constexpr auto string_opts
Definition: daw_json_type_options.h:123
constexpr json_details::json_options_t number_opts_def
Definition: daw_json_type_options.h:69
constexpr json_details::json_options_t string_opt(Options... options)
Definition: daw_json_type_options.h:128
constexpr auto number_opts
Definition: daw_json_type_options.h:68
constexpr json_details::json_options_t class_opt(Options... options)
Definition: daw_json_type_options.h:172
json_details::JsonOptionList< JsonNullable, EightBitModes, EmptyStringNull > string_opts_t
Definition: daw_json_type_options.h:121
json_details::JsonOptionList< JsonNullable, LiteralAsStringOpt > bool_opts_t
Definition: daw_json_type_options.h:79
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
Definition: version.h:11