Skip to content

Commit 73d07a2

Browse files
committed
Minor adjustments
1 parent f8287e3 commit 73d07a2

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

src/Elastic.Clients.Elasticsearch/_Shared/Core/LazyJsonConverter.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6+
using System.Diagnostics.CodeAnalysis;
67
using System.Text.Json;
78
using System.Text.Json.Serialization;
89

@@ -14,13 +15,13 @@ public sealed class LazyJsonConverter : JsonConverter<LazyJson>
1415
{
1516
private IElasticsearchClientSettings? _settings;
1617

18+
[UnconditionalSuppressMessage("AOT", "IL3050:Calling members annotated with 'RequiresDynamicCodeAttribute'", Justification = "Always using explicit TypeInfoResolver")]
19+
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute'", Justification = "Always using explicit TypeInfoResolver")]
1720
public override LazyJson Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
1821
{
1922
InitializeSettings(options);
2023

21-
#pragma warning disable IL2026, IL3050 // The `TypeInfoResolver` for `RequestResponseConverter` knows how to handle `JsonElement`.
2224
return new LazyJson(JsonSerializer.Deserialize<JsonElement>(ref reader, options), _settings!);
23-
#pragma warning restore IL2026, IL3050
2425
}
2526

2627
private void InitializeSettings(JsonSerializerOptions options)

src/Elastic.Clients.Elasticsearch/_Shared/Next/JsonWriterExtensions.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,28 @@ public static void WriteUnionValue<T1, T2>(this Utf8JsonWriter writer, JsonSeria
250250
);
251251
}
252252

253-
public static void WriteSpanValue<T>(this Utf8JsonWriter writer, JsonSerializerOptions options, ReadOnlySpan<T> span,
253+
public static void WriteMemoryValue<T>(this Utf8JsonWriter writer, JsonSerializerOptions options, ReadOnlyMemory<T> memory,
254254
JsonWriteFunc<T>? writeElement)
255255
{
256-
writeElement ??= static (w, o, v) => WriteValue(w, o, v);
256+
if (writeElement is null)
257+
{
258+
var converter = options.GetConverter<T>(null);
259+
260+
writeElement = (w, o, v) =>
261+
{
262+
if ((v is null) && !converter.HandleNull)
263+
{
264+
w.WriteNullValue();
265+
return;
266+
}
267+
268+
converter.Write(w, v, o);
269+
};
270+
}
257271

258272
writer.WriteStartArray();
259273

274+
var span = memory.Span;
260275
foreach (var element in span)
261276
{
262277
writeElement(writer, options, element);

src/Elastic.Clients.Elasticsearch/_Shared/Next/VectorConverters.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Buffers;
77
using System.Buffers.Binary;
8+
using System.Runtime.CompilerServices;
89
using System.Runtime.InteropServices;
910
using System.Text.Json;
1011
using System.Text.Json.Serialization;
@@ -57,7 +58,7 @@ public override void Write(Utf8JsonWriter writer, ReadOnlyMemory<float> value, J
5758
switch (_encoding)
5859
{
5960
case FloatVectorDataEncoding.Legacy:
60-
writer.WriteSpanValue(options, value.Span, null);
61+
writer.WriteMemoryValue(options, value, null);
6162
break;
6263

6364
case FloatVectorDataEncoding.Base64:
@@ -69,6 +70,7 @@ public override void Write(Utf8JsonWriter writer, ReadOnlyMemory<float> value, J
6970
}
7071
}
7172

73+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7274
private static ReadOnlyMemory<float> ReadBase64VectorData(ref Utf8JsonReader reader)
7375
{
7476
var bytes = reader.GetBytesFromBase64();
@@ -98,6 +100,7 @@ private static ReadOnlyMemory<float> ReadBase64VectorData(ref Utf8JsonReader rea
98100
return new(result);
99101
}
100102

103+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
101104
private static void WriteBase64VectorData(Utf8JsonWriter writer, ReadOnlyMemory<float> value)
102105
{
103106
if (value.IsEmpty)
@@ -192,7 +195,7 @@ public override void Write(Utf8JsonWriter writer, ReadOnlyMemory<byte> value, Js
192195
switch (_encoding)
193196
{
194197
case ByteVectorDataEncoding.Legacy:
195-
writer.WriteSpanValue(options, value.Span, (w, _, b) => w.WriteNumberValue(unchecked((sbyte)b)));
198+
writer.WriteMemoryValue(options, value, (w, _, b) => w.WriteNumberValue(unchecked((sbyte)b)));
196199
break;
197200

198201
case ByteVectorDataEncoding.Hex:
@@ -208,6 +211,7 @@ public override void Write(Utf8JsonWriter writer, ReadOnlyMemory<byte> value, Js
208211
}
209212
}
210213

214+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
211215
private static ReadOnlyMemory<byte> ReadStringVectorData(ref Utf8JsonReader reader)
212216
{
213217
if (reader.TryGetBytesFromBase64(out var result))
@@ -218,6 +222,7 @@ private static ReadOnlyMemory<byte> ReadStringVectorData(ref Utf8JsonReader read
218222
return ReadHexVectorData(ref reader);
219223
}
220224

225+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
221226
private static ReadOnlyMemory<byte> ReadHexVectorData(ref Utf8JsonReader reader)
222227
{
223228
#if NET5_0_OR_GREATER
@@ -229,6 +234,7 @@ private static ReadOnlyMemory<byte> ReadHexVectorData(ref Utf8JsonReader reader)
229234
return new(data);
230235
}
231236

237+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
232238
private static void WriteHexVectorData(Utf8JsonWriter writer, ReadOnlyMemory<byte> value)
233239
{
234240
if (value.IsEmpty)
@@ -267,7 +273,8 @@ private static void WriteHexVectorData(Utf8JsonWriter writer, ReadOnlyMemory<byt
267273
}
268274

269275
#if !NET5_0_OR_GREATER
270-
public static byte[] FromHex(string data)
276+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
277+
private static byte[] FromHex(string data)
271278
{
272279
if (data.Length is 0)
273280
{

0 commit comments

Comments
 (0)