22#include < new>
33#include < stdexcept>
44
5- constexpr std::size_t DataStorageSize = std::max({ sizeof (std::string), sizeof (cppjson::JsonObject ), sizeof (double ), sizeof (bool ) });
5+ constexpr std::size_t DataStorageSize = std::max({ sizeof (std::string), sizeof (cppjson::Object ), sizeof (double ), sizeof (bool ) });
66
77cppjson::JsonObject::JsonObject ()
88 : _dataStorage(static_cast <std::byte*>(::operator new (DataStorageSize)))
@@ -67,6 +67,19 @@ bool& cppjson::JsonObject::As<bool>() noexcept(false)
6767 return DangerousAs<bool >();
6868}
6969
70+ template <>
71+ cppjson::Object& cppjson::JsonObject::As<cppjson::Object>() noexcept (false )
72+ {
73+ if (this ->_dataType == JsonType::Null)
74+ {
75+ this ->_dataType = JsonType::Object;
76+ return *new (this ->_dataStorage ) cppjson::Object{};
77+ }
78+
79+ if (this ->_dataType != JsonType::Object) throw std::logic_error (" Cannot convert this object to a bool" );
80+ return DangerousAs<cppjson::Object>();
81+ }
82+
7083template <>
7184std::nullptr_t & cppjson::JsonObject::As<std::nullptr_t >() noexcept (false )
7285{
@@ -76,7 +89,6 @@ std::nullptr_t& cppjson::JsonObject::As<std::nullptr_t>() noexcept(false)
7689 return *new (this ->_dataStorage ) std::nullptr_t {};
7790}
7891
79-
8092template <>
8193const std::string& cppjson::JsonObject::As<std::string>() const noexcept (false )
8294{
@@ -98,6 +110,14 @@ const bool& cppjson::JsonObject::As<bool>() const noexcept(false)
98110 return DangerousAs<bool >();
99111}
100112
113+ template <>
114+ const cppjson::Object& cppjson::JsonObject::As<cppjson::Object>() const noexcept (false )
115+ {
116+ if (this ->_dataType != JsonType::Object) throw std::logic_error (" Cannot convert this object to an object" );
117+ return DangerousAs<cppjson::Object>();
118+ }
119+
120+
101121template <>
102122const std::nullptr_t & cppjson::JsonObject::As<std::nullptr_t >() const noexcept (false )
103123{
0 commit comments