33// See the LICENSE file in the project root for more information.
44
55using System ;
6+ using System . Collections . Generic ;
67using System . Text . Json ;
78using System . Text . Json . Serialization ;
89using System . Text . Json . Serialization . Metadata ;
@@ -17,49 +18,19 @@ namespace Elastic.Clients.Elasticsearch.Serialization;
1718public class DefaultSourceSerializer :
1819 SystemTextJsonSerializer
1920{
20- #if ! NET8_0_OR_GREATER
21- private readonly object _lock = new ( ) ;
22- #endif
23-
2421 /// <summary>
2522 /// Constructs a new <see cref="DefaultSourceSerializer"/> instance that accepts an <see cref="Action{T}"/> that can
2623 /// be provided to customize the default <see cref="JsonSerializerOptions"/>.
2724 /// </summary>
28- /// <param name="settings">An <see cref="IElasticsearchClientSettings"/> instance to which this serializers
25+ /// <param name="settings">An <see cref="IElasticsearchClientSettings"/> instance to which this serializer
2926 /// <see cref="JsonSerializerOptions"/> will be linked.
3027 /// </param>
3128 /// <param name="configureOptions">
3229 /// An optional <see cref="Action{T}"/> to customize the configuration of the default <see cref="JsonSerializerOptions"/>.
3330 /// </param>
3431 public DefaultSourceSerializer ( IElasticsearchClientSettings settings , Action < JsonSerializerOptions > ? configureOptions = null ) :
35- base ( new DefaultSourceSerializerOptionsProvider ( configureOptions ) ) =>
36- LinkSettings ( settings ) ;
37-
38- /// <summary>
39- /// Links the <see cref="JsonSerializerOptions"/> of this serializer to the given <see cref="IElasticsearchClientSettings"/>.
40- /// </summary>
41- private void LinkSettings ( IElasticsearchClientSettings settings )
32+ base ( new DefaultSourceSerializerOptionsProvider ( settings , configureOptions ) )
4233 {
43- var options = GetJsonSerializerOptions ( SerializationFormatting . None ) ;
44- var indentedOptions = GetJsonSerializerOptions ( SerializationFormatting . Indented ) ;
45-
46- #if NET8_0_OR_GREATER
47- ElasticsearchClient . SettingsTable . TryAdd ( options , settings ) ;
48- ElasticsearchClient . SettingsTable . TryAdd ( indentedOptions , settings ) ;
49- #else
50- lock ( _lock )
51- {
52- if ( ! ElasticsearchClient . SettingsTable . TryGetValue ( options , out _ ) )
53- {
54- ElasticsearchClient . SettingsTable . Add ( options , settings ) ;
55- }
56-
57- if ( ! ElasticsearchClient . SettingsTable . TryGetValue ( indentedOptions , out _ ) )
58- {
59- ElasticsearchClient . SettingsTable . Add ( indentedOptions , settings ) ;
60- }
61- }
62- #endif
6334 }
6435}
6536
@@ -69,27 +40,29 @@ private void LinkSettings(IElasticsearchClientSettings settings)
6940public class DefaultSourceSerializerOptionsProvider :
7041 TransportSerializerOptionsProvider
7142{
43+ public DefaultSourceSerializerOptionsProvider ( IElasticsearchClientSettings settings , Action < JsonSerializerOptions > ? configureOptions = null ) :
44+ base ( CreateDefaultBuiltInConverters ( settings ) , null , options => MutateOptions ( options , configureOptions ) )
45+ {
46+ }
47+
48+ public DefaultSourceSerializerOptionsProvider ( IElasticsearchClientSettings settings , bool registerDefaultConverters , Action < JsonSerializerOptions > ? configureOptions = null ) :
49+ base ( registerDefaultConverters ? CreateDefaultBuiltInConverters ( settings ) : [ ] , null , options => MutateOptions ( options , configureOptions ) )
50+ {
51+ }
52+
7253 /// <summary>
7354 /// Returns an array of the built-in <see cref="JsonConverter"/>s that are used registered with the source serializer by default.
7455 /// </summary>
75- private static JsonConverter [ ] DefaultBuiltInConverters =>
56+ private static IReadOnlyCollection < JsonConverter > CreateDefaultBuiltInConverters ( IElasticsearchClientSettings settings ) =>
7657 [
58+ // For context aware JsonConverter/JsonConverterFactory implementations.
59+ new ContextProvider < IElasticsearchClientSettings > ( settings ) ,
60+
7761 new JsonStringEnumConverter ( ) ,
7862 new DoubleWithFractionalPortionConverter ( ) ,
79- new SingleWithFractionalPortionConverter ( ) ,
80- // TODO: Add RequestResponseSerializer
63+ new SingleWithFractionalPortionConverter ( )
8164 ] ;
8265
83- public DefaultSourceSerializerOptionsProvider ( Action < JsonSerializerOptions > ? configureOptions = null ) :
84- base ( DefaultBuiltInConverters , null , options => MutateOptions ( options , configureOptions ) )
85- {
86- }
87-
88- public DefaultSourceSerializerOptionsProvider ( bool registerDefaultConverters = true , Action < JsonSerializerOptions > ? configureOptions = null ) :
89- base ( registerDefaultConverters ? DefaultBuiltInConverters : [ ] , null , options => MutateOptions ( options , configureOptions ) )
90- {
91- }
92-
9366 private static void MutateOptions ( JsonSerializerOptions options , Action < JsonSerializerOptions > ? configureOptions )
9467 {
9568 options . TypeInfoResolver = new DefaultJsonTypeInfoResolver ( ) ;
0 commit comments