Skip to content

Commit 9aaf423

Browse files
Fix serialization of OpType (#8105) (#8109)
Co-authored-by: Florian Bernd <git@flobernd.de>
1 parent c9b4cdf commit 9aaf423

File tree

1 file changed

+28
-0
lines changed
  • src/Elastic.Clients.Elasticsearch.Shared/Types

1 file changed

+28
-0
lines changed

src/Elastic.Clients.Elasticsearch.Shared/Types/OpType.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information.
44

5+
using System;
6+
using System.Text.Json;
7+
using System.Text.Json.Serialization;
8+
59
using Elastic.Transport;
610

711
#if ELASTICSEARCH_SERVERLESS
@@ -10,6 +14,7 @@ namespace Elastic.Clients.Elasticsearch.Serverless;
1014
namespace Elastic.Clients.Elasticsearch;
1115
#endif
1216

17+
[JsonConverter(typeof(OpTypeConverter))]
1318
public partial struct OpType : IStringable
1419
{
1520
public static OpType Index = new("index");
@@ -23,3 +28,26 @@ public partial struct OpType : IStringable
2328

2429
public string GetString() => Value ?? string.Empty;
2530
}
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+
}

0 commit comments

Comments
 (0)