Skip to content

Commit f835352

Browse files
committed
feat(JsonTransformer): add AOT support and configure JSON serializer options
1 parent dfb8ae4 commit f835352

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

libraries/src/AWS.Lambda.Powertools.Parameters/Internal/Transform/JsonTransformer.cs

+20-1
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,31 @@ namespace AWS.Lambda.Powertools.Parameters.Internal.Transform;
2323
/// </summary>
2424
internal class JsonTransformer : ITransformer
2525
{
26+
private readonly JsonSerializerOptions _options;
27+
28+
/// <summary>
29+
/// Initializes a new instance of the <see cref="JsonTransformer"/> class.
30+
/// </summary>
31+
public JsonTransformer()
32+
{
33+
_options = new JsonSerializerOptions
34+
{
35+
PropertyNameCaseInsensitive = true
36+
};
37+
}
38+
2639
/// <summary>
2740
/// Deserialize a JSON value from a JSON string.
2841
/// </summary>
2942
/// <param name="value">JSON string.</param>
3043
/// <typeparam name="T">JSON value type.</typeparam>
3144
/// <returns>JSON value.</returns>
45+
#if NET6_0_OR_GREATER
46+
[System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage("AOT", "IL3050:RequiresDynamicCode",
47+
Justification = "Types are expected to be known at compile time")]
48+
[System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage("Trimming", "IL2026:RequiresUnreferencedCode",
49+
Justification = "Types are expected to be preserved")]
50+
#endif
3251
public T? Transform<T>(string value)
3352
{
3453
if (typeof(T) == typeof(string))
@@ -37,6 +56,6 @@ internal class JsonTransformer : ITransformer
3756
if (string.IsNullOrWhiteSpace(value))
3857
return default;
3958

40-
return JsonSerializer.Deserialize<T>(value);
59+
return JsonSerializer.Deserialize<T>(value, _options);
4160
}
4261
}

libraries/tests/AWS.Lambda.Powertools.Parameters.Tests/AppConfig/AppConfigProviderTest.cs

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* permissions and limitations under the License.
1414
*/
1515

16+
using System.Diagnostics.CodeAnalysis;
1617
using System.Text;
1718
using System.Text.Json;
1819
using System.Text.Json.Nodes;
@@ -32,6 +33,7 @@
3233

3334
namespace AWS.Lambda.Powertools.Parameters.Tests.AppConfig;
3435

36+
[SuppressMessage("Usage", "xUnit1030:Do not call ConfigureAwait(false) in test method")]
3537
public class AppConfigProviderTest
3638
{
3739
[Fact]

0 commit comments

Comments
 (0)