Consuming JSON Objects Stream (NDJSON) With HttpClient

29 paź 2020 | Blog | httpclient | ndjson | IT

Some time ago I've written about streaming JSON objects from server to client in ASP.NET Core. Later I've created a small library with this functionality which supports IAsyncEnumerable<T> and has separated implementations for System.Text.Json and Newtonsoft.Json. The number of downloads on NuGet isn't high but looks like somebody is using it.

Recently, I've received a question, how this JSON objects stream can be consumed with HttpClient. I've once read that instead of writing a response to an email it's better to write a blog post, so here I am.

Consuming NDJSON Stream With HttpClient

My demo project consumes the stream from browser client. In order to do it, it's using cool browsers capabilities like Streams API. Unfortunately this doesn't make things obvious enough for somebody not familiar with those APIs to immediately port the implementation.

The way in which one should be thinking about NDJSON response is a string. It's a string that is being append new lines and every line represents a single JSON object. So, in case of .NET, the good old tools we have for "string as stream" are good enough. For example StreamReader. We can use it to asynchronously read lines from response stream and then deserialize them into objects. Below code wraps the whole thing in nice extension method which returns IAsyncEnumerable<T>.

internal static class HttpContentNdjsonExtensions
{
private static readonly JsonSerializerOptions _serializerOptions
= new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
PropertyNami

POSTY TEGO AUTORA

Monitoring C# Azure Functions in the Isolated Worker Model - Infrastructure & Configuration Deep Dive26 lis 2024

Blog | programowanie | .net | c# | azure functions | IT

ASP.NET Core 9 and IAsyncEnumerable - Async Streaming JSON and NDJSON From Blazor WebAssembly24 wrz 2024

Blog | programowanie | .net | c# | asp.net core | blazor | IT

Azure Functions Extensibility - Extensions and Isolated Worker Model5 mar 2024

Blog | programowanie | .net | c# | azure functions | azure | IT

Azure Functions Extensibility - Runtime Scaling22 lut 2024

Blog | programowanie | .net | c# | azure | IT

Yet Another Developer Blog

noreply@blogger.com (Tomasz Pęczek)