7
7
using System . Text . Json ;
8
8
using System . Text . Json . Serialization ;
9
9
10
- using Elastic . Transport ;
11
-
12
10
namespace Elastic . Clients . Elasticsearch . Serialization ;
13
11
14
12
internal sealed class KeyValuePairConverterFactory : JsonConverterFactory
15
13
{
16
- private readonly IElasticsearchClientSettings _settings ;
17
-
18
- public KeyValuePairConverterFactory ( IElasticsearchClientSettings settings ) => _settings = settings ;
19
-
20
14
public override bool CanConvert ( Type typeToConvert ) =>
21
15
typeToConvert . IsGenericType &&
22
16
typeToConvert . GetGenericTypeDefinition ( ) == typeof ( KeyValuePair < , > ) ;
@@ -28,29 +22,25 @@ public override JsonConverter CreateConverter(
28
22
var itemOneType = type . GetGenericArguments ( ) [ 0 ] ;
29
23
var itemTwoType = type . GetGenericArguments ( ) [ 1 ] ;
30
24
31
- return ( JsonConverter ) Activator . CreateInstance ( typeof ( KeyValuePairConverter < , > ) . MakeGenericType ( itemOneType , itemTwoType ) , _settings ) ;
25
+ return ( JsonConverter ) Activator . CreateInstance ( typeof ( KeyValuePairConverter < , > ) . MakeGenericType ( itemOneType , itemTwoType ) ) ;
32
26
}
33
27
34
28
private class KeyValuePairConverter < TItem1 , TItem2 > : JsonConverter < KeyValuePair < TItem1 , TItem2 > >
35
29
{
36
- private readonly IElasticsearchClientSettings _settings ;
37
-
38
- public KeyValuePairConverter ( IElasticsearchClientSettings settings ) => _settings = settings ;
39
-
40
30
public override KeyValuePair < TItem1 , TItem2 > Read ( ref Utf8JsonReader reader , Type typeToConvert , JsonSerializerOptions options )
41
31
{
42
32
if ( reader . TokenType != JsonTokenType . StartObject )
43
33
throw new JsonException ( "Unexpected token for KeyValuePair" ) ;
44
34
45
35
reader . Read ( ) ; // property name (key)
46
- var keyString = reader . GetString ( ) ;
36
+ var converter = ( JsonConverter < TItem1 > ) options . GetConverter ( typeof ( TItem1 ) ) ;
37
+ var key = converter . ReadAsPropertyName ( ref reader , typeof ( TItem1 ) , options ) ;
47
38
48
39
reader . Read ( ) ; // value
49
40
var value = JsonSerializer . Deserialize < TItem2 > ( ref reader , options ) ;
50
41
51
42
reader . Read ( ) ; // end object
52
43
53
- var key = ( TItem1 ) Activator . CreateInstance ( typeof ( TItem1 ) , keyString ) ;
54
44
return new KeyValuePair < TItem1 , TItem2 > ( key , value ) ;
55
45
}
56
46
@@ -59,16 +49,10 @@ public override void Write(Utf8JsonWriter writer, KeyValuePair<TItem1, TItem2> v
59
49
{
60
50
writer . WriteStartObject ( ) ;
61
51
62
- if ( value . Key is IUrlParameter parameter )
63
- {
64
- writer . WritePropertyName ( parameter . GetString ( _settings ) ) ;
65
- }
66
- else
67
- {
68
- writer . WritePropertyName ( value . Key . ToString ( ) ) ;
69
- }
52
+ var converter = ( JsonConverter < TItem1 > ) options . GetConverter ( typeof ( TItem1 ) ) ;
53
+ converter . WriteAsPropertyName ( writer , value . Key , options ) ;
54
+ JsonSerializer . Serialize ( writer , value . Value , options ) ;
70
55
71
- JsonSerializer . Serialize < TItem2 > ( writer , value . Value , options ) ;
72
56
writer . WriteEndObject ( ) ;
73
57
}
74
58
}
0 commit comments