3
3
// See the LICENSE file in the project root for more information.
4
4
5
5
using System ;
6
+ using System . Collections . Generic ;
6
7
using System . Text . Json ;
7
8
using System . Text . Json . Serialization ;
8
9
using System . Text . Json . Serialization . Metadata ;
@@ -17,49 +18,19 @@ namespace Elastic.Clients.Elasticsearch.Serialization;
17
18
public class DefaultSourceSerializer :
18
19
SystemTextJsonSerializer
19
20
{
20
- #if ! NET8_0_OR_GREATER
21
- private readonly object _lock = new ( ) ;
22
- #endif
23
-
24
21
/// <summary>
25
22
/// Constructs a new <see cref="DefaultSourceSerializer"/> instance that accepts an <see cref="Action{T}"/> that can
26
23
/// be provided to customize the default <see cref="JsonSerializerOptions"/>.
27
24
/// </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
29
26
/// <see cref="JsonSerializerOptions"/> will be linked.
30
27
/// </param>
31
28
/// <param name="configureOptions">
32
29
/// An optional <see cref="Action{T}"/> to customize the configuration of the default <see cref="JsonSerializerOptions"/>.
33
30
/// </param>
34
31
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 ) )
42
33
{
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
63
34
}
64
35
}
65
36
@@ -69,27 +40,29 @@ private void LinkSettings(IElasticsearchClientSettings settings)
69
40
public class DefaultSourceSerializerOptionsProvider :
70
41
TransportSerializerOptionsProvider
71
42
{
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
+
72
53
/// <summary>
73
54
/// Returns an array of the built-in <see cref="JsonConverter"/>s that are used registered with the source serializer by default.
74
55
/// </summary>
75
- private static JsonConverter [ ] DefaultBuiltInConverters =>
56
+ private static IReadOnlyCollection < JsonConverter > CreateDefaultBuiltInConverters ( IElasticsearchClientSettings settings ) =>
76
57
[
58
+ // For context aware JsonConverter/JsonConverterFactory implementations.
59
+ new ContextProvider < IElasticsearchClientSettings > ( settings ) ,
60
+
77
61
new JsonStringEnumConverter ( ) ,
78
62
new DoubleWithFractionalPortionConverter ( ) ,
79
- new SingleWithFractionalPortionConverter ( ) ,
80
- // TODO: Add RequestResponseSerializer
63
+ new SingleWithFractionalPortionConverter ( )
81
64
] ;
82
65
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
-
93
66
private static void MutateOptions ( JsonSerializerOptions options , Action < JsonSerializerOptions > ? configureOptions )
94
67
{
95
68
options . TypeInfoResolver = new DefaultJsonTypeInfoResolver ( ) ;
0 commit comments