-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathPowertoolsLoggingSerializer.cs
More file actions
266 lines (233 loc) · 10.1 KB
/
Copy pathPowertoolsLoggingSerializer.cs
File metadata and controls
266 lines (233 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using Amazon.Lambda.Serialization.SystemTextJson;
using AWS.Lambda.Powertools.Common;
using AWS.Lambda.Powertools.Common.Utils;
using AWS.Lambda.Powertools.Logging.Internal.Converters;
namespace AWS.Lambda.Powertools.Logging.Serializers;
/// <summary>
/// Provides serialization functionality for Powertools logging.
/// </summary>
internal class PowertoolsLoggingSerializer
{
private JsonSerializerOptions _currentOptions;
private LoggerOutputCase _currentOutputCase;
private volatile JsonSerializerOptions _jsonOptions;
private readonly object _lock = new();
private readonly ConcurrentBag<JsonSerializerContext> _additionalContexts = new();
private static JsonSerializerContext _staticAdditionalContexts;
private IJsonTypeInfoResolver _customTypeInfoResolver;
/// <summary>
/// Gets the JsonSerializerOptions instance.
/// </summary>
internal JsonSerializerOptions GetSerializerOptions()
{
// Double-checked locking pattern for thread safety while ensuring we only build once
if (_jsonOptions == null)
{
lock (_lock)
{
if (_jsonOptions == null)
{
BuildJsonSerializerOptions(_currentOptions);
}
}
}
return _jsonOptions;
}
/// <summary>
/// Configures the naming policy for the serializer.
/// </summary>
/// <param name="loggerOutputCase">The case to use for serialization.</param>
internal void ConfigureNamingPolicy(LoggerOutputCase loggerOutputCase)
{
if (_currentOutputCase != loggerOutputCase)
{
lock (_lock)
{
_currentOutputCase = loggerOutputCase;
// Force a full rebuild on next access instead of mutating existing options,
// because JsonSerializerOptions becomes read-only after first serialization.
_jsonOptions = null;
}
}
}
/// <summary>
/// Serializes an object to a JSON string.
/// </summary>
/// <param name="value">The object to serialize.</param>
/// <param name="inputType">The type of the object to serialize.</param>
/// <returns>A JSON string representation of the object.</returns>
/// <exception cref="InvalidOperationException">Thrown when the input type is not known to the serializer.</exception>
internal string Serialize(object value, Type inputType)
{
if (RuntimeFeatureWrapper.IsDynamicCodeSupported)
{
var jsonSerializerOptions = GetSerializerOptions();
#pragma warning disable
return JsonSerializer.Serialize(value, jsonSerializerOptions);
}
// Try to serialize using the configured TypeInfoResolver
var typeInfo = GetTypeInfo(inputType);
if (typeInfo == null)
{
throw new JsonSerializerException(
$"Type {inputType} is not known to the serializer. Ensure it's included in the JsonSerializerContext.");
}
return JsonSerializer.Serialize(value, typeInfo);
}
/// <summary>
/// Adds a JsonSerializerContext to the serializer options.
/// </summary>
/// <param name="context">The JsonSerializerContext to add.</param>
/// <exception cref="ArgumentNullException">Thrown when the context is null.</exception>
internal void AddSerializerContext(JsonSerializerContext context)
{
ArgumentNullException.ThrowIfNull(context);
// Don't add duplicates
if (!_additionalContexts.Contains(context))
{
_additionalContexts.Add(context);
// If we have existing JSON options, update their type resolver
if (_jsonOptions != null && !RuntimeFeatureWrapper.IsDynamicCodeSupported)
{
// Reset the type resolver chain to rebuild it
_jsonOptions.TypeInfoResolver = GetCompositeResolver();
}
}
}
internal static void AddStaticSerializerContext(JsonSerializerContext context)
{
ArgumentNullException.ThrowIfNull(context);
_staticAdditionalContexts = context;
}
/// <summary>
/// Get a composite resolver that includes all configured resolvers
/// </summary>
private IJsonTypeInfoResolver GetCompositeResolver()
{
var resolvers = new List<IJsonTypeInfoResolver>();
// Add custom resolver if provided
if (_customTypeInfoResolver != null)
{
resolvers.Add(_customTypeInfoResolver);
}
// add any static resolvers
if (_staticAdditionalContexts != null)
{
resolvers.Add(_staticAdditionalContexts);
}
// Add default context
resolvers.Add(PowertoolsLoggingSerializationContext.Default);
// Add additional contexts
foreach (var context in _additionalContexts)
{
resolvers.Add(context);
}
return new CompositeJsonTypeInfoResolver(resolvers.ToArray());
}
/// <summary>
/// Gets the JsonTypeInfo for a given type.
/// </summary>
/// <param name="type">The type to get information for.</param>
/// <returns>The JsonTypeInfo for the specified type, or null if not found.</returns>
private JsonTypeInfo GetTypeInfo(Type type)
{
var options = GetSerializerOptions();
return options.TypeInfoResolver?.GetTypeInfo(type, options);
}
/// <summary>
/// Builds and configures the JsonSerializerOptions.
/// </summary>
/// <returns>A configured JsonSerializerOptions instance.</returns>
private void BuildJsonSerializerOptions(JsonSerializerOptions options = null)
{
lock (_lock)
{
// Build into a local variable so _jsonOptions is never visible in a partially-configured state.
// Another thread doing a lock-free read in GetSerializerOptions() could see a non-null _jsonOptions,
// use it for serialization (making it read-only), and then subsequent mutations here would throw.
var newOptions = new JsonSerializerOptions();
// Copy any properties from the original options if provided
if (options != null)
{
// Copy standard properties
newOptions.DefaultIgnoreCondition = options.DefaultIgnoreCondition;
newOptions.PropertyNameCaseInsensitive = options.PropertyNameCaseInsensitive;
newOptions.PropertyNamingPolicy = options.PropertyNamingPolicy;
newOptions.DictionaryKeyPolicy = options.DictionaryKeyPolicy;
newOptions.WriteIndented = options.WriteIndented;
newOptions.ReferenceHandler = options.ReferenceHandler;
newOptions.MaxDepth = options.MaxDepth;
newOptions.IgnoreReadOnlyFields = options.IgnoreReadOnlyFields;
newOptions.IgnoreReadOnlyProperties = options.IgnoreReadOnlyProperties;
newOptions.IncludeFields = options.IncludeFields;
newOptions.NumberHandling = options.NumberHandling;
newOptions.ReadCommentHandling = options.ReadCommentHandling;
newOptions.UnknownTypeHandling = options.UnknownTypeHandling;
newOptions.AllowTrailingCommas = options.AllowTrailingCommas;
// Handle type resolver extraction without setting it yet
if (options.TypeInfoResolver != null)
{
_customTypeInfoResolver = options.TypeInfoResolver;
// If it's a JsonSerializerContext, also add it to our contexts
if (_customTypeInfoResolver is JsonSerializerContext jsonContext)
{
AddSerializerContext(jsonContext);
}
}
}
// Set output case and other properties
SetOutputCase(newOptions);
AddConverters(newOptions);
newOptions.Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping;
newOptions.PropertyNameCaseInsensitive = true;
// Set TypeInfoResolver last, as this makes options read-only
if (!RuntimeFeatureWrapper.IsDynamicCodeSupported)
{
newOptions.TypeInfoResolver = GetCompositeResolver();
}
// Publish fully-configured options in a single atomic assignment
_jsonOptions = newOptions;
}
}
internal void SetOutputCase(JsonSerializerOptions target)
{
switch (_currentOutputCase)
{
case LoggerOutputCase.CamelCase:
target.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
target.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;
break;
case LoggerOutputCase.PascalCase:
target.PropertyNamingPolicy = PascalCaseNamingPolicy.Instance;
target.DictionaryKeyPolicy = PascalCaseNamingPolicy.Instance;
break;
default: // Snake case
// If is default (Not Set) and JsonOptions provided with DictionaryKeyPolicy or PropertyNamingPolicy, use it
target.DictionaryKeyPolicy ??= JsonNamingPolicy.SnakeCaseLower;
target.PropertyNamingPolicy ??= JsonNamingPolicy.SnakeCaseLower;
break;
}
}
private static void AddConverters(JsonSerializerOptions target)
{
target.Converters.Add(new ByteArrayConverter());
target.Converters.Add(new ExceptionConverter());
target.Converters.Add(new MemoryStreamConverter());
target.Converters.Add(new ConstantClassConverter());
target.Converters.Add(new DateOnlyConverter());
target.Converters.Add(new TimeOnlyConverter());
target.Converters.Add(new LogLevelJsonConverter());
}
internal void SetOptions(JsonSerializerOptions options)
{
_currentOptions = options;
}
}