Efficient deserialization of Json files on Azure Blob Storage with .NET

Efficient deserialization of Json files on Azure Blob Storage with .NET

System.Text.Json is currently the most modern way to handle Json files efficiently. Efficiency here also means that memory sparing files are read in.

A very efficient way to handle Json files is not to load them into a string first, but to implement the deserialization via streams. This keeps the application more scalable and performant.

System.Text.Json has this built in, so such an implementation can be done within a few lines.

// Create Client
BlobContainerClient container = new(_options.ConnectionString, _options.ContainerName);
BlobClient blobClient = container.GetBlobClient(_options.BlobPath);

// check if blob exists
Azure.Response<bool> exists = await blobClient.ExistsAsync(ct).ConfigureAwait(false);
if (exists.Value is false)
{
    return null;
}

// Get Blob Stream
BlobOpenReadOptions readOptions = new(allowModifications: false);
using Stream stream = await blobClient.OpenReadAsync(readOptions, ct).ConfigureAwait(false);

// read json
JsonSerializerOptions jsonOpions = new();

MyDeserializationTargetClass? myDeserializedObject = await JsonSerializer
    .DeserializeAsync<MyDeserializationTargetClass>(stream, jsonOpions, ct)
    .ConfigureAwait(false);