Skip to content

Commit f280e77

Browse files
committed
Call Destroy() in type erase
Signed-off-by: TymianekPL <tymi@tymi.org>
1 parent 7b082ca commit f280e77

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

cppjson/src/object.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,25 @@ cppjson::Object& cppjson::JsonObject::As<cppjson::Object>() noexcept(false)
114114
return DangerousAs<cppjson::Object>();
115115
}
116116

117+
template <>
118+
cppjson::Array& cppjson::JsonObject::As<cppjson::Array>() noexcept(false)
119+
{
120+
if (this->_dataType == JsonType::Null)
121+
{
122+
this->_dataType = JsonType::Array;
123+
return *new (this->_dataStorage) cppjson::Array{};
124+
}
125+
126+
if (this->_dataType != JsonType::Array) throw std::logic_error("Cannot convert this object to an array");
127+
return DangerousAs<cppjson::Array>();
128+
}
129+
117130
template <>
118131
std::nullptr_t& cppjson::JsonObject::As<std::nullptr_t>() noexcept(false)
119132
{
120133
if (std::exchange(this->_dataType, JsonType::Null) == JsonType::Null) return DangerousAs<std::nullptr_t>();
121134

135+
Destroy();
122136
return *new (this->_dataStorage) std::nullptr_t{};
123137
}
124138

@@ -150,6 +164,13 @@ const cppjson::Object& cppjson::JsonObject::As<cppjson::Object>() const noexcept
150164
return DangerousAs<cppjson::Object>();
151165
}
152166

167+
template <>
168+
const cppjson::Array& cppjson::JsonObject::As<cppjson::Array>() const noexcept(false)
169+
{
170+
if (this->_dataType != JsonType::Array) throw std::logic_error("Cannot convert this object to an Array");
171+
return DangerousAs<cppjson::Array>();
172+
}
173+
153174
template <>
154175
const std::nullptr_t& cppjson::JsonObject::As<std::nullptr_t>() const noexcept(false)
155176
{

0 commit comments

Comments
 (0)