Skip to content

Commit

Permalink
Re-use JsonSerializerOptions
Browse files Browse the repository at this point in the history
Previously, the JsonSerializerOptions were created per instance of HaveIBeenPwnedClient. Since the (de)serializtion info is stored in the options this caused them to be regenerated for each instance of HaveIBeenPwnedClient.

Now we store a single JsonSerializerOptions for the lifetime of the application in a static readonly field. Also, the context was expanded to cover IEnumerable<T> of the different types to prevent errors on .NET 7.0 where an exception is thrown for (de)serialization types not explicitly configiured in the context.
  • Loading branch information
akamsteeg committed Oct 27, 2022
1 parent 814efca commit 7c3bf7f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/AtleX.HaveIBeenPwned/HaveIBeenPwnedClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AtleX.HaveIBeenPwned.Helpers;
using AtleX.HaveIBeenPwned.Serialization.Json;
using Pitcher;
using SwissArmyKnife;
using System;
Expand Down Expand Up @@ -43,6 +44,11 @@ public sealed class HaveIBeenPwnedClient
/// </remarks>
private readonly bool _enableClientDisposing;

/// <summary>
/// Gets the <see cref="JsonSerializerOptions"/> to use when (de)serializing JSON
/// </summary>
private static readonly JsonSerializerOptions JsonOptions = JsonSerializerOptionsFactory.Create();

/// <summary>
/// Initializes a new instance of <see cref="HaveIBeenPwnedClient"/> with the
/// specified <see cref="HaveIBeenPwnedClientSettings"/> and <see cref="HttpClient"/>
Expand Down Expand Up @@ -337,7 +343,7 @@ private async Task<T> GetAsync<T>(HttpRequestMessage requestMessage, Cancellatio
using var content = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

var result = await JsonSerializer
.DeserializeAsync<T>(content, cancellationToken: cancellationToken)
.DeserializeAsync<T>(content, options: JsonOptions, cancellationToken: cancellationToken)
.ConfigureAwait(false);

if (result is null)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Text.Json;

namespace AtleX.HaveIBeenPwned.Serialization.Json;

/// <summary>
/// Represents a factory for <see cref="JsonSerializerOptions"/>
/// </summary>
internal static class JsonSerializerOptionsFactory
{
/// <summary>
/// Creates a new instance of <see cref="JsonSerializerOptions"/>
/// </summary>
/// <returns>
/// The created <see cref="JsonSerializerOptions"/>
/// </returns>
public static JsonSerializerOptions Create()
{
var result = new JsonSerializerOptions();

return result;
}
}
9 changes: 9 additions & 0 deletions src/ConsoleApp1/ConsoleApp1.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
4 changes: 4 additions & 0 deletions src/ConsoleApp1/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// See https://aka.ms/new-console-template for more information


Console.WriteLine("Hello, World!");

0 comments on commit 7c3bf7f

Please sign in to comment.