Skip to content

Commit

Permalink
Fix Json source generators (#91)
Browse files Browse the repository at this point in the history
* Introduce JsonSerializerOptionsForJsonSerializerContext
* Use GenerationMode=Metadata
  • Loading branch information
trejjam committed Jun 7, 2023
1 parent 5229665 commit 305bf7d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using PayPal.Sdk.Checkout.Core.Converters;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace PayPal.Sdk.Checkout.Core.MessageSerializers;

public static class JsonSerializerOptionsForJsonSerializerContext
{
public static JsonSerializerOptions ConvertersContextOptions { get; } = new()
{
DefaultIgnoreCondition = JsonIgnoreCondition.Never,
IgnoreReadOnlyFields = false,
IgnoreReadOnlyProperties = false,
IncludeFields = false,
WriteIndented = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Converters =
{
new JsonStringEnumConverterFactory(),
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,19 @@ namespace PayPal.Sdk.Checkout.Core.MessageSerializers;
[JsonSourceGenerationOptions(
WriteIndented = true,
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
GenerationMode = JsonSourceGenerationMode.Serialization
GenerationMode = JsonSourceGenerationMode.Metadata
)]
[JsonSerializable(typeof(AccessToken))]
[JsonSerializable(typeof(RefreshToken))]
internal partial class PayPalAuthenticationJsonSerializerContext : JsonSerializerContext
{
private static JsonSerializerOptions ConvertersContextOptions { get; } = new()
{
DefaultIgnoreCondition = JsonIgnoreCondition.Never,
IgnoreReadOnlyFields = false,
IgnoreReadOnlyProperties = false,
IncludeFields = false,
WriteIndented = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Converters =
{
new JsonStringEnumConverterFactory(),
},
};

private static PayPalAuthenticationJsonSerializerContext? _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 PayPalAuthenticationJsonSerializerContext CustomConverters => _convertersContext
??= new PayPalAuthenticationJsonSerializerContext(
new JsonSerializerOptions(ConvertersContextOptions)
new JsonSerializerOptions(JsonSerializerOptionsForJsonSerializerContext.ConvertersContextOptions)
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace PayPal.Sdk.Checkout.Core.MessageSerializers;
[JsonSourceGenerationOptions(
WriteIndented = true,
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
GenerationMode = JsonSourceGenerationMode.Serialization
GenerationMode = JsonSourceGenerationMode.Metadata
)]
[JsonSerializable(typeof(AuthorizeRequest))]
[JsonSerializable(typeof(IReadOnlyCollection<StringPatch>))]
Expand All @@ -19,27 +19,13 @@ namespace PayPal.Sdk.Checkout.Core.MessageSerializers;
[JsonSerializable(typeof(OrderRequest))]
internal partial class PayPalOrderJsonSerializerContext : JsonSerializerContext
{
private static JsonSerializerOptions ConvertersContextOptions { get; } = new()
{
DefaultIgnoreCondition = JsonIgnoreCondition.Never,
IgnoreReadOnlyFields = false,
IgnoreReadOnlyProperties = false,
IncludeFields = false,
WriteIndented = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Converters =
{
new JsonStringEnumConverterFactory(),
},
};

private static PayPalOrderJsonSerializerContext? _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 PayPalOrderJsonSerializerContext CustomConverters => _convertersContext
??= new PayPalOrderJsonSerializerContext(
new JsonSerializerOptions(ConvertersContextOptions)
new JsonSerializerOptions(JsonSerializerOptionsForJsonSerializerContext.ConvertersContextOptions)
);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using PayPal.Sdk.Checkout.Core.Converters;
using PayPal.Sdk.Checkout.Payments;
using System.Text.Json;
using System.Text.Json.Serialization;
Expand All @@ -8,7 +7,7 @@ namespace PayPal.Sdk.Checkout.Core.MessageSerializers;
[JsonSourceGenerationOptions(
WriteIndented = true,
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
GenerationMode = JsonSourceGenerationMode.Serialization
GenerationMode = JsonSourceGenerationMode.Metadata
)]
[JsonSerializable(typeof(Authorization))]
[JsonSerializable(typeof(Capture))]
Expand All @@ -18,27 +17,13 @@ namespace PayPal.Sdk.Checkout.Core.MessageSerializers;
[JsonSerializable(typeof(RefundRequest))]
internal partial class PayPalPaymentsJsonSerializerContext : JsonSerializerContext
{
private static JsonSerializerOptions ConvertersContextOptions { get; } = new()
{
DefaultIgnoreCondition = JsonIgnoreCondition.Never,
IgnoreReadOnlyFields = false,
IgnoreReadOnlyProperties = false,
IncludeFields = false,
WriteIndented = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Converters =
{
new JsonStringEnumConverterFactory(),
},
};

private static PayPalPaymentsJsonSerializerContext? _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 PayPalPaymentsJsonSerializerContext CustomConverters => _convertersContext
??= new PayPalPaymentsJsonSerializerContext(
new JsonSerializerOptions(ConvertersContextOptions)
new JsonSerializerOptions(JsonSerializerOptionsForJsonSerializerContext.ConvertersContextOptions)
);
}

0 comments on commit 305bf7d

Please sign in to comment.