@@ -88,6 +88,7 @@ template<typename CharType> struct data_variant
8888 std::uint64_t uint64_t_;
8989 double double_;
9090 std::basic_string_view<CharType> string_view_;
91+ std::nullptr_t null_;
9192
9293 constexpr explicit value_t () : empty_{} {}
9394 constexpr explicit value_t (monostate) : value_t () {}
@@ -99,9 +100,10 @@ template<typename CharType> struct data_variant
99100 constexpr explicit value_t (std::uint64_t i) : uint64_t_{ i } {}
100101 constexpr explicit value_t (double d) : double_{ d } {}
101102 constexpr explicit value_t (std::basic_string_view<CharType> s) : string_view_{ s } {}
103+ constexpr explicit value_t (std::nullptr_t ) : null_{} {}
102104 };
103105
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 };
105107
106108 value_t value{ monostate{} };
107109 selected_type selected{ selected_type::empty };
@@ -125,6 +127,8 @@ template<typename CharType> struct data_variant
125127 constexpr data_variant (double d) : value{ d }, selected{ selected_type::floating_point } {}
126128 // cppcheck-suppress noExplicitConstructor
127129 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 } {}
128132
129133 [[nodiscard]] constexpr bool is_boolean () const noexcept { return selected == selected_type::boolean; }
130134
@@ -204,6 +208,8 @@ template<typename CharType> struct data_variant
204208 return nullptr ;
205209 }
206210 }
211+
212+ [[nodiscard]] constexpr bool is_null () const noexcept { return selected == selected_type::nullish; }
207213};
208214
209215template <typename CharType> struct basic_json
@@ -435,6 +441,12 @@ template<typename CharType> struct basic_json
435441 } else {
436442 throw std::runtime_error (" Unexpected type: bool requested" );
437443 }
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+ }
438450 } else {
439451 throw std::runtime_error (" Unexpected type for get()" );
440452 }
@@ -447,7 +459,7 @@ template<typename CharType> struct basic_json
447459 [[nodiscard]] constexpr bool is_structured () const noexcept { return is_object () || is_array (); }
448460 [[nodiscard]] constexpr bool is_number () const noexcept { return is_number_integer () || is_number_float (); }
449461 [[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 ; }
451463 [[nodiscard]] constexpr bool is_binary () const noexcept { return data.selected == data_t ::selected_type::binary; }
452464
453465 [[nodiscard]] constexpr bool is_number_signed () const noexcept
0 commit comments