Skip to content

Commit

Permalink
🐛 Fix as_array / as_object may throw an exception
Browse files Browse the repository at this point in the history
  • Loading branch information
yosh-matsuda committed Sep 25, 2023
1 parent d90b192 commit 57da0e2
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions include/cpp_yyjson.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3327,7 +3327,12 @@ namespace yyjson
array& operator=(const array&) = default;
array& operator=(array&&) = default;
};
inline std::optional<array> value::as_array() && { return array(std::move(*this)); }
inline std::optional<array> value::as_array() &&
{
if (is_array()) [[likely]]
return array(std::move(*this));
return std::nullopt;
}

class object final : public const_object_ref
{
Expand All @@ -3351,7 +3356,12 @@ namespace yyjson
object& operator=(const object&) = default;
object& operator=(object&&) = default;
};
inline std::optional<object> value::as_object() && { return object(std::move(*this)); }
inline std::optional<object> value::as_object() &&
{
if (is_object()) [[likely]]
return object(std::move(*this));
return std::nullopt;
}

#pragma region read
template <yyjson_allocator Alloc>
Expand Down

0 comments on commit 57da0e2

Please sign in to comment.