-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathFieldsConverter.cs
69 lines (60 loc) · 1.62 KB
/
FieldsConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
#if ELASTICSEARCH_SERVERLESS
namespace Elastic.Clients.Elasticsearch.Serverless;
#else
namespace Elastic.Clients.Elasticsearch;
#endif
internal sealed class FieldsConverter : JsonConverter<Fields>
{
public override Fields? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
Fields fields = reader.GetString();
return fields;
}
if (reader.TokenType == JsonTokenType.StartArray)
{
var fields = new List<Field>();
while (reader.Read() && reader.TokenType != JsonTokenType.EndArray)
{
var field = JsonSerializer.Deserialize<Field>(ref reader, options);
fields.Add(field);
}
return new Fields(fields);
}
reader.Read();
return null;
}
public override void Write(Utf8JsonWriter writer, Fields value, JsonSerializerOptions options)
{
if (value is null)
{
writer.WriteNullValue();
return;
}
if (value.ListOfFields.Count == 0)
{
writer.WriteStartObject();
writer.WriteEndObject();
return;
}
if (value.ListOfFields.Count == 1)
{
JsonSerializer.Serialize(writer, value.ListOfFields[0], options);
return;
}
writer.WriteStartArray();
foreach (var field in value.ListOfFields)
{
JsonSerializer.Serialize(writer, field, options);
}
writer.WriteEndArray();
}
}