0

I found some code on SO showing how to serialize a C# object to an Azure blog via a stream. This 2018 vintage code used NewtonSoft JSON serialization and I decided to amend the code to work with System.Text.Json.

The resulting code is so much simpler I am concerned I have taken a shortcut too far that will be problematic later.

Is stream based serialization with System.Text.Json as simple to express as it appears?

New Code

public static class BlobExtensions
{
    public static async Task AppendObjectAsJsonAsync( this AppendBlobClient blob, object obj )
    {
        using var stream = await blob.OpenWriteAsync( false );
        await JsonSerializer.SerializeAsync( stream, obj );
    }
}

Original Newtonsoft Code

public static class BlobExtensions
{
    public static async Task SerializeObjectToBlobAsync(this CloudBlockBlob blob, object obj)
    {
        using (Stream stream = await blob.OpenWriteAsync())
        using (StreamWriter sw = new StreamWriter(stream))
        using (JsonTextWriter jtw = new JsonTextWriter(sw))
        {
            JsonSerializer ser = new JsonSerializer();
            ser.Serialize(jtw, obj);
        }
    }
}

The new code works with both AppendBlobClient and BlockBlobClient, I am showing the AppendBlobClient example because that it a better fit for my applications.

camelCase
  • 1,549
  • 2
  • 16
  • 28
  • 1
    1) You should do [`await using var stream`](https://stackoverflow.com/q/58610350) rather than `using var stream`. 2) You *may* still need to sprinkle your code with `.ConfigureAwait(false)` calls, see [ConfigureAwait(false) relevant in ASP.NET Core?](https://stackoverflow.com/q/42053135/3744182). Though if your existing code doesn't do that your new code shouldn't need to. Otherwise yes, it's pretty much that simple. Async support is one of the reasons MSFT chose to write its own JSON serializer. – dbc Jul 21 '23 at 17:09
  • But what is your question exactly? If your code works, what problem are you having? – dbc Jul 21 '23 at 17:10
  • @dbc - Your comment on "await using" revealed a subtlety in C# I had previously overlooked, thankyou. I will update my example code above once the change works on my local app. I suppose the onus is on each developer to check for an implementation for IAsyncDisposable. – camelCase Jul 21 '23 at 22:56
  • @dbc - My problem is that I was concerned the relative code simplification would have a negative side effect. The majority of example code for Azure Blob streaming covers the classic case of file uploads and this led me to the "Original Newtonsoft Code" in an earlier SO thread. I think my question offers long-term value for others researching this subject. – camelCase Jul 22 '23 at 09:16
  • Then what answer are you looking for? Does my comment answer your question? – dbc Jul 22 '23 at 18:09
  • @dbc - I am reassured that you, as a hi-rep user whose highest ranked skill on SO is C#, have eyeballed my code simplification and cannot spot a problem other than async using. If another user posted saying "I arrived at the same code simplification when converting NewtonSoft based code with streaming to System.Text.Json", then that would be a bonus. Right now I consider the question resolved. – camelCase Jul 23 '23 at 09:19

0 Answers0