using System.Collections.Generic;
using System.IO.Pipelines;
using System.Threading.Tasks;
static readonly byte[] NewLineChars = {(byte)'\r', (byte)'\n'};
static readonly byte[] WhiteSpaceChars = {(byte)'\r', (byte)'\n', (byte)' ', (byte)'\t'};
private static async Task Main()
JsonSerializerOptions jsonOptions = new(JsonSerializerDefaults.Web);
using var client = new HttpClient();
using var response = await client.GetAsync("https://some.uri", HttpCompletionOption.ResponseHeadersRead);
using var stream = await response.Content.ReadAsStreamAsync();
var pipeReader = PipeReader.Create(stream);
await foreach (var foo in ReadItemsAsync<Foo>(pipeReader, jsonOptions))
Console.WriteLine($"foo: {foo.Some}");
static async IAsyncEnumerable<TValue> ReadItemsAsync<TValue>(PipeReader pipeReader, JsonSerializerOptions jsonOptions = null)
var result = await pipeReader.ReadAsync();
var buffer = result.Buffer;
bool isCompleted = result.IsCompleted;
SequencePosition bufferPosition = buffer.Start;
var(value, advanceSequence) = TryReadNextItem<TValue>(buffer, ref bufferPosition, isCompleted, jsonOptions);
pipeReader.AdvanceTo(bufferPosition, buffer.End);
static (TValue, bool) TryReadNextItem<TValue>(ReadOnlySequence<byte> sequence, ref SequencePosition sequencePosition, bool isCompleted, JsonSerializerOptions jsonOptions)
var reader = new SequenceReader<byte>(sequence.Slice(sequencePosition));
if (reader.TryReadToAny(out ReadOnlySpan<byte> itemBytes, NewLineChars, advancePastDelimiter: true))
sequencePosition = reader.Position;
if (itemBytes.TrimStart(WhiteSpaceChars).IsEmpty)
return (JsonSerializer.Deserialize<TValue>(itemBytes, jsonOptions), false);
var remainingReader = sequence.Slice(reader.Position);
ReadOnlySpan<byte> remainingSpan = remainingReader.IsSingleSegment ? remainingReader.First.Span : remainingReader.ToArray();
reader.Advance(remainingReader.Length);
sequencePosition = reader.Position;
if (!remainingSpan.TrimStart(WhiteSpaceChars).IsEmpty)
return (JsonSerializer.Deserialize<TValue>(remainingSpan, jsonOptions), true);