33// See the LICENSE file in the project root for more information
44
55using System ;
6+ using System . Collections . Generic ;
67using System . IO ;
78using System . Text . Json ;
9+ using System . Text . Json . Serialization ;
810using System . Threading ;
911using System . Threading . Tasks ;
1012
@@ -14,14 +16,23 @@ namespace Elastic.Transport;
1416/// An abstract implementation of a transport <see cref="Serializer"/> which serializes using the Microsoft
1517/// <c>System.Text.Json</c> library.
1618/// </summary>
17- public abstract class SystemTextJsonSerializer :
18- Serializer
19+ public abstract class SystemTextJsonSerializer : Serializer
1920{
20- private readonly SemaphoreSlim _semaphore = new ( 1 ) ;
21+ private readonly JsonSerializerOptions ? _options ;
22+ private readonly JsonSerializerOptions ? _indentedOptions ;
2123
22- private bool _initialized ;
23- private JsonSerializerOptions ? _options ;
24- private JsonSerializerOptions ? _indentedOptions ;
24+ /// <summary>
25+ /// An abstract implementation of a transport <see cref="Serializer"/> which serializes using the Microsoft
26+ /// <c>System.Text.Json</c> library.
27+ /// </summary>
28+ protected SystemTextJsonSerializer ( IJsonSerializerOptionsProvider ? provider = null )
29+ {
30+
31+ provider ??= new TransportSerializerOptionsProvider ( ) ;
32+ _options = provider . CreateJsonSerializerOptions ( ) ;
33+ _indentedOptions = provider . CreateJsonSerializerOptions ( ) ;
34+ _indentedOptions . WriteIndented = true ;
35+ }
2536
2637 #region Serializer
2738
@@ -74,80 +85,14 @@ public override Task SerializeAsync<T>(T data, Stream stream,
7485
7586 #endregion Serializer
7687
77- /// <summary>
78- /// A factory method that can create an instance of <see cref="JsonSerializerOptions"/> that will
79- /// be used when serializing.
80- /// </summary>
81- /// <returns></returns>
82- protected abstract JsonSerializerOptions ? CreateJsonSerializerOptions ( ) ;
83-
84- /// <summary>
85- /// A callback function that is invoked after the <see cref="JsonSerializerOptions"/> have been created and the
86- /// serializer got fully initialized.
87- /// </summary>
88- protected virtual void Initialized ( )
89- {
90- }
91-
9288 /// <summary>
9389 /// Returns the <see cref="JsonSerializerOptions"/> for this serializer, based on the given <paramref name="formatting"/>.
9490 /// </summary>
9591 /// <param name="formatting">The serialization formatting.</param>
9692 /// <returns>The requested <see cref="JsonSerializerOptions"/> or <c>null</c>, if the serializer is not initialized yet.</returns>
97- protected internal JsonSerializerOptions ? GetJsonSerializerOptions ( SerializationFormatting formatting = SerializationFormatting . None )
98- {
99- Initialize ( ) ;
100-
101- return ( formatting is SerializationFormatting . None )
102- ? _options
103- : _indentedOptions ;
104- }
93+ protected internal JsonSerializerOptions ? GetJsonSerializerOptions ( SerializationFormatting formatting = SerializationFormatting . None ) =>
94+ formatting is SerializationFormatting . None ? _options : _indentedOptions ;
10595
106- /// <summary>
107- /// Initializes a serializer instance such that its <see cref="JsonSerializerOptions"/> are populated.
108- /// </summary>
109- private void Initialize ( )
110- {
111- // Exit early, if already initialized
112- if ( _initialized )
113- return ;
114-
115- _semaphore . Wait ( ) ;
116-
117- try
118- {
119- // Exit early, if the current thread lost the race
120- if ( _initialized )
121- return ;
122-
123- var options = CreateJsonSerializerOptions ( ) ;
124-
125- if ( options is null )
126- {
127- _options = new JsonSerializerOptions ( ) ;
128- _indentedOptions = new JsonSerializerOptions
129- {
130- WriteIndented = true
131- } ;
132- }
133- else
134- {
135- _options = options ;
136- _indentedOptions = new JsonSerializerOptions ( options )
137- {
138- WriteIndented = true
139- } ;
140- }
141-
142- _initialized = true ;
143-
144- Initialized ( ) ;
145- }
146- finally
147- {
148- _semaphore . Release ( ) ;
149- }
150- }
15196
15297 private static bool TryReturnDefault < T > ( Stream ? stream , out T deserialize )
15398 {
0 commit comments