diff --git a/src/WhatsApp/JsonContext.Internal.cs b/src/WhatsApp/JsonContext.Internal.cs new file mode 100644 index 0000000..b383d0f --- /dev/null +++ b/src/WhatsApp/JsonContext.Internal.cs @@ -0,0 +1,26 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Devlooped.WhatsApp; + +[JsonSourceGenerationOptions(JsonSerializerDefaults.Web, + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + UseStringEnumConverter = true, + UnmappedMemberHandling = JsonUnmappedMemberHandling.Skip, + PropertyNameCaseInsensitive = true, + PropertyNamingPolicy = JsonKnownNamingPolicy.SnakeCaseLower, + WriteIndented = true + )] +[JsonSerializable(typeof(GraphMethodException))] +[JsonSerializable(typeof(ErrorResponse))] +[JsonSerializable(typeof(SendResponse))] +[JsonSerializable(typeof(MessageId))] +partial class InternalJsonContext : JsonSerializerContext +{ +} + +record ErrorResponse(GraphMethodException Error); + +record SendResponse(MessageId[] Messages); + +record MessageId(string Id); \ No newline at end of file diff --git a/src/WhatsApp/JsonContext.cs b/src/WhatsApp/JsonContext.cs index 4cf5a2c..135f943 100644 --- a/src/WhatsApp/JsonContext.cs +++ b/src/WhatsApp/JsonContext.cs @@ -6,6 +6,21 @@ namespace Devlooped.WhatsApp; +/// +/// Provides a source-generated JSON serialization context for the built-in types in this library, +/// optimized for web scenarios configured with custom serialization options. +/// +/// +/// This context is used to enable high-performance JSON serialization and deserialization for the +/// specified types using source generation. It is configured with options such as snake_case property naming, +/// ignoring null values when writing, and using string-based enum serialization. The context also +/// supports case-insensitive property name matching and indented JSON output. +/// +/// The property provides a pre-configured instance of +/// that aligns with the context's settings and also includes +/// the encoder for web scenarios. +/// +/// [JsonSourceGenerationOptions(JsonSerializerDefaults.Web, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, UseStringEnumConverter = true, @@ -22,14 +37,13 @@ namespace Devlooped.WhatsApp; [JsonSerializable(typeof(StatusMessage))] [JsonSerializable(typeof(UnsupportedMessage))] [JsonSerializable(typeof(MediaReference))] -[JsonSerializable(typeof(GraphMethodException))] -[JsonSerializable(typeof(ErrorResponse))] -[JsonSerializable(typeof(SendResponse))] -[JsonSerializable(typeof(MessageId))] -partial class JsonContext : JsonSerializerContext +public partial class JsonContext : JsonSerializerContext { - static readonly Lazy options = new Lazy(() => CreateDefaultOptions()); + static readonly Lazy options = new(() => CreateDefaultOptions()); + /// + /// Provides a pre-configured instance of that aligns with the context's settings. + /// public static JsonSerializerOptions DefaultOptions { get => options.Value; } [UnconditionalSuppressMessage("AotAnalysis", "IL3050", Justification = "DefaultJsonTypeInfoResolver is only used when reflection-based serialization is enabled")] @@ -53,10 +67,4 @@ static JsonSerializerOptions CreateDefaultOptions() options.MakeReadOnly(); return options; } -} - -record ErrorResponse(GraphMethodException Error); - -record SendResponse(MessageId[] Messages); - -record MessageId(string Id); \ No newline at end of file +} \ No newline at end of file diff --git a/src/WhatsApp/WhatsAppClient.cs b/src/WhatsApp/WhatsAppClient.cs index 9258974..1bfdce6 100644 --- a/src/WhatsApp/WhatsAppClient.cs +++ b/src/WhatsApp/WhatsAppClient.cs @@ -60,7 +60,7 @@ public HttpClient CreateHttp(string numberId) throw new HttpRequestException(error, null, result.StatusCode); } - var response = await result.Content.ReadFromJsonAsync(JsonContext.Default.SendResponse); + var response = await result.Content.ReadFromJsonAsync(InternalJsonContext.Default.SendResponse); return response?.Messages?.FirstOrDefault()?.Id; } diff --git a/src/WhatsApp/WhatsAppClientExtensions.ResolveMedia.cs b/src/WhatsApp/WhatsAppClientExtensions.ResolveMedia.cs index 6d95518..d9d0642 100644 --- a/src/WhatsApp/WhatsAppClientExtensions.ResolveMedia.cs +++ b/src/WhatsApp/WhatsAppClientExtensions.ResolveMedia.cs @@ -40,7 +40,7 @@ public static async Task ResolveMediaAsync(this IWhatsAppClient await response.Content.LoadIntoBufferAsync(); if (!response.IsSuccessStatusCode && - await response.Content.ReadFromJsonAsync(JsonContext.Default.ErrorResponse, cancellation) is { } error) + await response.Content.ReadFromJsonAsync(InternalJsonContext.Default.ErrorResponse, cancellation) is { } error) throw error.Error; response.EnsureSuccessStatusCode();