Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using System.Threading;
using Elastic.Transport;
Expand All @@ -16,6 +17,15 @@ namespace Elastic.Clients.Elasticsearch.Esql;

public partial class EsqlNamespacedClient
{
#pragma warning disable IL2026, IL3050

private static readonly JsonSerializerOptions EsqlJsonSerializerOptions = new JsonSerializerOptions(JsonSerializerOptions.Default)
{
TypeInfoResolver = EsqlJsonSerializerContext.Default
};

#pragma warning restore IL2026, IL3050

/// <summary>
/// Executes an ES|QL request and returns the response as a stream.
/// </summary>
Expand Down Expand Up @@ -71,9 +81,8 @@ private static IEnumerable<T> EsqlToObject<T>(ElasticsearchClient client, EsqlQu
{
// TODO: Improve performance

// TODO: fixme
#pragma warning disable IL2026, IL3050
using var doc = JsonSerializer.Deserialize<JsonDocument>(response.Data) ?? throw new JsonException();
using var doc = JsonSerializer.Deserialize<JsonDocument>(response.Data, EsqlJsonSerializerOptions) ?? throw new JsonException();
#pragma warning restore IL2026, IL3050

if (!doc.RootElement.TryGetProperty("columns"u8, out var columns) || (columns.ValueKind is not JsonValueKind.Array))
Expand Down Expand Up @@ -107,9 +116,17 @@ private static IEnumerable<T> EsqlToObject<T>(ElasticsearchClient client, EsqlQu
writer.Reset();

var properties = names.Zip(document.EnumerateArray(),
(key, value) => new KeyValuePair<string, JsonNode?>(key, JsonValue.Create(value)));
(key, value) => new KeyValuePair<string, JsonNode?>(key, value.ValueKind switch
{
JsonValueKind.Object => JsonObject.Create(value),
JsonValueKind.Array => JsonArray.Create(value),
_ => JsonValue.Create(value)
}));

foreach (var property in properties)
{
obj.Add(property);
}

obj.WriteTo(writer);
writer.Flush();
Expand All @@ -120,4 +137,8 @@ private static IEnumerable<T> EsqlToObject<T>(ElasticsearchClient client, EsqlQu
yield return result;
}
}

[JsonSerializable(typeof(JsonDocument))]
internal sealed partial class EsqlJsonSerializerContext :
JsonSerializerContext;
}
Loading