akamsteeg / AtleX.HaveIBeenPwned

A fully async .NET Standard client library for the API of HaveIBeenPwned.com
https://www.nuget.org/packages/AtleX.HaveIBeenPwned/
MIT License
5 stars 0 forks source link

Use `TypeInfoResolverChain.Add()` on `JsonSerializerOptions` on .NET 8+ to ensure using the serialization context #93

Closed akamsteeg closed 8 months ago

akamsteeg commented 8 months ago

In ,NET 8, registering a serialization context changed.

,NET 6:

result.AddContext<JsonSerializationContext>();

.NET 8:

result.TypeInfoResolverChain.Add(new JsonSerializationContext());

We must add a specific TFM for .NET 8 to AtleX.HaveIBeenPwned and add target specific code to JsonSerializerOptionsFactory:

  public static JsonSerializerOptions Create()
  {
    var result = new JsonSerializerOptions();

#if NET8_0_OR_GREATER
    result.TypeInfoResolverChain.Add(new JsonSerializationContext());
#elif NET6_0_OR_GREATER                  
    result.AddContext<JsonSerializationContext>();
#endif

    return result;
  }