-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Utilize System.Text.Json.Serialization.JsonSerializerContext (#69)
* Introduce DaktelaJsonSerializerContext * Add custom JsonContent in HttpRequestSerializer * Add DaktelaJsonSerializerContext.SerializationDateTimeOffset * Use JsonSourceGenerationMode.Metadata
- Loading branch information
Showing
28 changed files
with
669 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/Daktela.HttpClient/Api/DaktelaJsonSerializerContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using Daktela.HttpClient.Implementations.JsonConverters; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Daktela.HttpClient.Api; | ||
|
||
[JsonSourceGenerationOptions( | ||
WriteIndented = true, | ||
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, | ||
GenerationMode = JsonSourceGenerationMode.Metadata | ||
)] | ||
[JsonSerializable(typeof(Contacts.CreateContact))] | ||
[JsonSerializable(typeof(Contacts.UpdateContact))] | ||
[JsonSerializable(typeof(Tickets.CreateActivity))] | ||
[JsonSerializable(typeof(Tickets.CreateTicket))] | ||
[JsonSerializable(typeof(Tickets.UpdateActivity))] | ||
[JsonSerializable(typeof(Tickets.UpdateTicket))] | ||
[JsonSerializable(typeof(Tickets.ReadActivity<Tickets.Activities.CallActivity>))] | ||
[JsonSerializable(typeof(Tickets.ReadActivity<Tickets.Activities.CommentActivity>))] | ||
[JsonSerializable(typeof(Tickets.ReadActivity<Tickets.Activities.CustomActivity>))] | ||
[JsonSerializable(typeof(Tickets.ReadActivity<Tickets.Activities.EmailActivity>))] | ||
[JsonSerializable(typeof(Tickets.ReadActivity<Tickets.Activities.MessengerActivity>))] | ||
[JsonSerializable(typeof(Tickets.ReadActivity<Tickets.Activities.SmsActivity>))] | ||
[JsonSerializable(typeof(Tickets.ReadActivity<Tickets.Activities.ViberActivity>))] | ||
[JsonSerializable(typeof(Tickets.ReadActivity<Tickets.Activities.WebChatActivity>))] | ||
[JsonSerializable(typeof(Tickets.ReadActivity<Tickets.Activities.WhatsAppActivity>))] | ||
[JsonSerializable(typeof(Responses.Errors.ComplexErrorResponse))] | ||
[JsonSerializable(typeof(Responses.Errors.ErrorFormMessages))] | ||
[JsonSerializable(typeof(Responses.Errors.NestedErrorForm))] | ||
[JsonSerializable(typeof(Responses.Errors.PlainErrorResponse))] | ||
[JsonSerializable(typeof(Responses.ListResponse<Contacts.ReadContact>))] | ||
[JsonSerializable(typeof(Responses.ListResponse<Tickets.Category>))] | ||
[JsonSerializable(typeof(Responses.ListResponse<Tickets.ReadActivity>))] | ||
[JsonSerializable(typeof(Responses.ListResponse<Tickets.ReadActivityAttachment>))] | ||
[JsonSerializable(typeof(Responses.ListResponse<Tickets.ReadTicket>))] | ||
[JsonSerializable(typeof(Responses.SingleResponse<Contacts.ReadContact>))] | ||
[JsonSerializable(typeof(Responses.SingleResponse<Tickets.ReadTicket>))] | ||
[JsonSerializable(typeof(Responses.SingleResponse<Tickets.ReadActivity>))] | ||
[JsonSerializable(typeof(IDictionary<string, ICollection<string>>))] | ||
public partial class DaktelaJsonSerializerContext : JsonSerializerContext | ||
{ | ||
private static TimeSpan _serializationDateTimeOffset; | ||
|
||
public static TimeSpan SerializationDateTimeOffset | ||
{ | ||
get => _serializationDateTimeOffset; | ||
set | ||
{ | ||
_serializationDateTimeOffset = value; | ||
_convertersContext = null; | ||
} | ||
} | ||
|
||
private static JsonSerializerOptions ConvertersContextOptions => new() | ||
{ | ||
DefaultIgnoreCondition = JsonIgnoreCondition.Never, | ||
IgnoreReadOnlyFields = false, | ||
IgnoreReadOnlyProperties = false, | ||
IncludeFields = false, | ||
WriteIndented = true, | ||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, | ||
Converters = | ||
{ | ||
new DateTimeOffsetConverter(SerializationDateTimeOffset), | ||
new TimeSpanConverter(), | ||
new ReadActivityConverter(), | ||
new CustomFieldsConverter(), | ||
new EnumsConverterFactory(), | ||
new EmailActivityOptionsHeadersAddressConverter(), | ||
new ErrorResponseConverter(), | ||
new ErrorFormConverter(), | ||
}, | ||
}; | ||
|
||
private static DaktelaJsonSerializerContext? _convertersContext; | ||
|
||
/// <summary> | ||
/// The default <see cref="global::System.Text.Json.Serialization.JsonSerializerContext"/> associated with a default <see cref="global::System.Text.Json.JsonSerializerOptions"/> instance. | ||
/// </summary> | ||
public static DaktelaJsonSerializerContext CustomConverters => _convertersContext | ||
??= new DaktelaJsonSerializerContext( | ||
new JsonSerializerOptions(ConvertersContextOptions) | ||
); | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Daktela.HttpClient/Configuration/DaktelaPostConfigureOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using Daktela.HttpClient.Api; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Daktela.HttpClient.Configuration; | ||
|
||
public class DaktelaPostConfigureOptions : IPostConfigureOptions<DaktelaOptions> | ||
{ | ||
public void PostConfigure( | ||
string? name, DaktelaOptions options | ||
) => DaktelaJsonSerializerContext.SerializationDateTimeOffset = options.DateTimeOffset!.Value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.