Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,29 @@ public static JsonException UnexpectedTokenException(this ref Utf8JsonReader rea
return new JsonException($"Expected JSON {valid} token, but got '{reader.TokenType}'.");
}

public static void SafeSkip(this ref Utf8JsonReader reader)
{
// Utf8JsonReader.Skip() unconditionally throws, if the reader instance is constructed with `isFinalBlock = false`.
// This may happen when reading from streams or pipes.

// For custom converters, System.Text.JSON always guarantees that the entire JSON value for the current scope is available.
// See:
// https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/converters-how-to#steps-to-follow-the-basic-pattern

// > Override the Read method to deserialize the incoming JSON and convert it to type T. Use the Utf8JsonReader that's passed to
// > the method to read the JSON. You don't have to worry about handling partial data, as the serializer passes all the data for
// > the current JSON scope.

// We use `TrySkip()` here to avoid the exception.

if (!reader.TrySkip())
{
throw new InvalidOperationException(
"Failed to skip JSON token. This case should never happen and indicates a severe problem. " +
"Please open an issue in the Github repository.");
}
}

/// <summary>
/// Compares the JSON encoded text to the JSON token value in the source and returns <see langword="true"/> if they match.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public static UnionTag MatchProperty(ref Utf8JsonReader reader, JsonSerializerOp
}

internalReader.Read();
internalReader.Skip();
internalReader.SafeSkip();
}

return UnionTag.None;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public override ResponseItem Read(ref Utf8JsonReader reader, Type typeToConvert,

if (options.UnmappedMemberHandling is JsonUnmappedMemberHandling.Skip)
{
reader.Skip();
reader.SafeSkip();
continue;
}

Expand Down