Skip to content

Commit 7e34f6b

Browse files
authored
Refactoring and tiny behavior fix for Ids (#7730)
1 parent 71a77de commit 7e34f6b

File tree

2 files changed

+50
-41
lines changed

2 files changed

+50
-41
lines changed

src/Elastic.Clients.Elasticsearch/Core/Infer/Id/Ids.cs

+1-41
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Collections.Generic;
77
using System.Diagnostics;
88
using System.Linq;
9-
using System.Text.Json;
109
using System.Text.Json.Serialization;
1110
using Elastic.Transport;
1211

@@ -22,7 +21,7 @@ public partial class Ids : IUrlParameter, IEquatable<Ids>
2221

2322
public Ids(IList<Id> ids) => _ids = ids;
2423

25-
public Ids(IEnumerable<string> ids) => _ids = ids?.Select(i => new Id(i)).ToList();
24+
public Ids(IEnumerable<string> ids) => _ids = ids.Select(i => new Id(i)).ToList();
2625

2726
public Ids(string value)
2827
{
@@ -84,42 +83,3 @@ public override int GetHashCode()
8483

8584
public static bool operator !=(Ids left, Ids right) => !Equals(left, right);
8685
}
87-
88-
internal sealed class IdsConverter : JsonConverter<Ids>
89-
{
90-
public override Ids? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
91-
{
92-
if (reader.TokenType != JsonTokenType.StartArray)
93-
throw new JsonException($"Unexpected JSON token. Expected {JsonTokenType.StartArray} but read {reader.TokenType}");
94-
95-
var ids = new List<Id>();
96-
97-
while (reader.Read() && reader.TokenType != JsonTokenType.EndArray)
98-
{
99-
var id = JsonSerializer.Deserialize<Id>(ref reader, options);
100-
101-
if (id is not null)
102-
ids.Add(id);
103-
}
104-
105-
return new Ids(ids);
106-
}
107-
108-
public override void Write(Utf8JsonWriter writer, Ids value, JsonSerializerOptions options)
109-
{
110-
if (value is null)
111-
{
112-
writer.WriteNullValue();
113-
return;
114-
}
115-
116-
writer.WriteStartArray();
117-
118-
foreach (var id in value.IdsToSerialize)
119-
{
120-
JsonSerializer.Serialize<Id>(writer, id, options);
121-
}
122-
123-
writer.WriteEndArray();
124-
}
125-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Text.Json;
8+
using System.Text.Json.Serialization;
9+
10+
namespace Elastic.Clients.Elasticsearch;
11+
12+
internal sealed class IdsConverter : JsonConverter<Ids>
13+
{
14+
public override Ids? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
15+
{
16+
if (reader.TokenType != JsonTokenType.StartArray)
17+
throw new JsonException($"Unexpected JSON token. Expected {JsonTokenType.StartArray} but read {reader.TokenType}");
18+
19+
var ids = new List<Id>();
20+
21+
while (reader.Read() && reader.TokenType != JsonTokenType.EndArray)
22+
{
23+
var id = JsonSerializer.Deserialize<Id>(ref reader, options);
24+
25+
if (id is not null)
26+
ids.Add(id);
27+
}
28+
29+
return new Ids(ids);
30+
}
31+
32+
public override void Write(Utf8JsonWriter writer, Ids value, JsonSerializerOptions options)
33+
{
34+
if (value is null)
35+
{
36+
writer.WriteNullValue();
37+
return;
38+
}
39+
40+
writer.WriteStartArray();
41+
42+
foreach (var id in value.IdsToSerialize)
43+
{
44+
JsonSerializer.Serialize<Id>(writer, id, options);
45+
}
46+
47+
writer.WriteEndArray();
48+
}
49+
}

0 commit comments

Comments
 (0)