File tree 1 file changed +28
-0
lines changed
src/Elastic.Clients.Elasticsearch.Shared/Types 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 2
2
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3
3
// See the LICENSE file in the project root for more information.
4
4
5
+ using System ;
6
+ using System . Text . Json ;
7
+ using System . Text . Json . Serialization ;
8
+
5
9
using Elastic . Transport ;
6
10
7
11
#if ELASTICSEARCH_SERVERLESS
@@ -10,6 +14,7 @@ namespace Elastic.Clients.Elasticsearch.Serverless;
10
14
namespace Elastic . Clients . Elasticsearch ;
11
15
#endif
12
16
17
+ [ JsonConverter ( typeof ( OpTypeConverter ) ) ]
13
18
public partial struct OpType : IStringable
14
19
{
15
20
public static OpType Index = new ( "index" ) ;
@@ -23,3 +28,26 @@ public partial struct OpType : IStringable
23
28
24
29
public string GetString ( ) => Value ?? string . Empty ;
25
30
}
31
+
32
+ internal sealed class OpTypeConverter :
33
+ JsonConverter < OpType >
34
+ {
35
+ public override OpType Read ( ref Utf8JsonReader reader , Type typeToConvert , JsonSerializerOptions options )
36
+ {
37
+ if ( reader . TokenType != JsonTokenType . String )
38
+ {
39
+ throw new JsonException ( "Unexpected token." ) ;
40
+ }
41
+
42
+ var value = reader . GetString ( ) ;
43
+
44
+ return value switch
45
+ {
46
+ "index" => OpType . Index ,
47
+ "create" => OpType . Create ,
48
+ _ => throw new JsonException ( $ "Unsupported value '{ value } ' for '{ nameof ( OpType ) } ' enum.")
49
+ } ;
50
+ }
51
+
52
+ public override void Write ( Utf8JsonWriter writer , OpType value , JsonSerializerOptions options ) => writer . WriteStringValue ( value . Value ) ;
53
+ }
You can’t perform that action at this time.
0 commit comments