Skip to content

Commit

Permalink
AK: Allow destruction of JsonObjectSerializer objects after errors
Browse files Browse the repository at this point in the history
Previously we'd VERIFY() that the user had called finish(). This makes
the following code incorrect though:

auto json = TRY(JsonObjectSerializer<>::try_create(builder));
TRY(json.add("total_time"sv, total_time_scheduled.total));
TRY(json.finish());
return ...;

If the second TRY() returns early we'd fail at the VERIFY() call in the
destructor.

Calling finish() in the destructor - like we had done earlier - is also
not helpful because we have no idea whether the builder is still valid.
Plus we wouldn't be able to handle any errors for that call.

Verifying that either finish() was called or an error occurred doesn't
work either because the caller might have multiple Json*Serializer
objects, e.g. when inserting a JSON array into a JSON object. Forcing
the user to call finish() on their "main" object when a sub-object
caused an error seems unnecessarily tedious.
  • Loading branch information
gunnarbeutner authored and linusg committed Nov 1, 2022
1 parent 92efa21 commit d1bc157
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 10 deletions.
5 changes: 0 additions & 5 deletions AK/JsonArraySerializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ class JsonArraySerializer {

JsonArraySerializer(JsonArraySerializer const&) = delete;

~JsonArraySerializer()
{
VERIFY(m_finished);
}

#ifndef KERNEL
ErrorOr<void> add(JsonValue const& value)
{
Expand Down
5 changes: 0 additions & 5 deletions AK/JsonObjectSerializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ class JsonObjectSerializer {

JsonObjectSerializer(JsonObjectSerializer const&) = delete;

~JsonObjectSerializer()
{
VERIFY(m_finished);
}

#ifndef KERNEL
ErrorOr<void> add(StringView key, JsonValue const& value)
{
Expand Down

0 comments on commit d1bc157

Please sign in to comment.