@@ -88,6 +88,7 @@ template<typename CharType> struct data_variant
88
88
std::uint64_t uint64_t_;
89
89
double double_;
90
90
std::basic_string_view<CharType> string_view_;
91
+ std::nullptr_t null_;
91
92
92
93
constexpr explicit value_t () : empty_{} {}
93
94
constexpr explicit value_t (monostate) : value_t () {}
@@ -99,9 +100,10 @@ template<typename CharType> struct data_variant
99
100
constexpr explicit value_t (std::uint64_t i) : uint64_t_{ i } {}
100
101
constexpr explicit value_t (double d) : double_{ d } {}
101
102
constexpr explicit value_t (std::basic_string_view<CharType> s) : string_view_{ s } {}
103
+ constexpr explicit value_t (std::nullptr_t ) : null_{} {}
102
104
};
103
105
104
- enum struct selected_type { empty, boolean, binary, array, object, integer, uinteger, floating_point, string };
106
+ enum struct selected_type { empty, boolean, binary, array, object, integer, uinteger, floating_point, string, nullish };
105
107
106
108
value_t value{ monostate{} };
107
109
selected_type selected{ selected_type::empty };
@@ -125,6 +127,8 @@ template<typename CharType> struct data_variant
125
127
constexpr data_variant (double d) : value{ d }, selected{ selected_type::floating_point } {}
126
128
// cppcheck-suppress noExplicitConstructor
127
129
constexpr data_variant (std::basic_string_view<CharType> s) : value{ s }, selected{ selected_type::string } {}
130
+ // cppcheck-suppress noExplicitConstructor
131
+ constexpr data_variant (std::nullptr_t ) : value{ nullptr }, selected{ selected_type::nullish } {}
128
132
129
133
[[nodiscard]] constexpr bool is_boolean () const noexcept { return selected == selected_type::boolean; }
130
134
@@ -204,6 +208,8 @@ template<typename CharType> struct data_variant
204
208
return nullptr ;
205
209
}
206
210
}
211
+
212
+ [[nodiscard]] constexpr bool is_null () const noexcept { return selected == selected_type::nullish; }
207
213
};
208
214
209
215
template <typename CharType> struct basic_json
@@ -435,6 +441,12 @@ template<typename CharType> struct basic_json
435
441
} else {
436
442
throw std::runtime_error (" Unexpected type: bool requested" );
437
443
}
444
+ } else if constexpr (std::is_same_v<Type, std::nullptr_t >) {
445
+ if (data.is_null ()) {
446
+ return nullptr ;
447
+ } else {
448
+ throw std::runtime_error (" Unexpected type: null requested" );
449
+ }
438
450
} else {
439
451
throw std::runtime_error (" Unexpected type for get()" );
440
452
}
@@ -447,7 +459,7 @@ template<typename CharType> struct basic_json
447
459
[[nodiscard]] constexpr bool is_structured () const noexcept { return is_object () || is_array (); }
448
460
[[nodiscard]] constexpr bool is_number () const noexcept { return is_number_integer () || is_number_float (); }
449
461
[[nodiscard]] constexpr bool is_number_integer () const noexcept { return is_number_signed () || is_number_unsigned (); }
450
- [[nodiscard]] constexpr bool is_null () const noexcept { return data.selected == data_t ::selected_type::empty ; }
462
+ [[nodiscard]] constexpr bool is_null () const noexcept { return data.selected == data_t ::selected_type::nullish ; }
451
463
[[nodiscard]] constexpr bool is_binary () const noexcept { return data.selected == data_t ::selected_type::binary; }
452
464
453
465
[[nodiscard]] constexpr bool is_number_signed () const noexcept
0 commit comments