88using System . Linq ;
99using System . Text . Json ;
1010using System . Text . Json . Nodes ;
11+ using System . Text . Json . Serialization ;
1112using System . Threading . Tasks ;
1213using System . Threading ;
1314using Elastic . Transport ;
@@ -16,6 +17,15 @@ namespace Elastic.Clients.Elasticsearch.Esql;
1617
1718public partial class EsqlNamespacedClient
1819{
20+ #pragma warning disable IL2026 , IL3050
21+
22+ private static readonly JsonSerializerOptions EsqlJsonSerializerOptions = new JsonSerializerOptions ( JsonSerializerOptions . Default )
23+ {
24+ TypeInfoResolver = EsqlJsonSerializerContext . Default
25+ } ;
26+
27+ #pragma warning restore IL2026 , IL3050
28+
1929 /// <summary>
2030 /// Executes an ES|QL request and returns the response as a stream.
2131 /// </summary>
@@ -71,9 +81,8 @@ private static IEnumerable<T> EsqlToObject<T>(ElasticsearchClient client, EsqlQu
7181 {
7282 // TODO: Improve performance
7383
74- // TODO: fixme
7584#pragma warning disable IL2026 , IL3050
76- using var doc = JsonSerializer . Deserialize < JsonDocument > ( response . Data ) ?? throw new JsonException ( ) ;
85+ using var doc = JsonSerializer . Deserialize < JsonDocument > ( response . Data , EsqlJsonSerializerOptions ) ?? throw new JsonException ( ) ;
7786#pragma warning restore IL2026 , IL3050
7887
7988 if ( ! doc . RootElement . TryGetProperty ( "columns"u8 , out var columns ) || ( columns . ValueKind is not JsonValueKind . Array ) )
@@ -107,9 +116,17 @@ private static IEnumerable<T> EsqlToObject<T>(ElasticsearchClient client, EsqlQu
107116 writer . Reset ( ) ;
108117
109118 var properties = names . Zip ( document . EnumerateArray ( ) ,
110- ( key , value ) => new KeyValuePair < string , JsonNode ? > ( key , JsonValue . Create ( value ) ) ) ;
119+ ( key , value ) => new KeyValuePair < string , JsonNode ? > ( key , value . ValueKind switch
120+ {
121+ JsonValueKind . Object => JsonObject . Create ( value ) ,
122+ JsonValueKind . Array => JsonArray . Create ( value ) ,
123+ _ => JsonValue . Create ( value )
124+ } ) ) ;
125+
111126 foreach ( var property in properties )
127+ {
112128 obj . Add ( property ) ;
129+ }
113130
114131 obj . WriteTo ( writer ) ;
115132 writer . Flush ( ) ;
@@ -120,4 +137,8 @@ private static IEnumerable<T> EsqlToObject<T>(ElasticsearchClient client, EsqlQu
120137 yield return result ;
121138 }
122139 }
140+
141+ [ JsonSerializable ( typeof ( JsonDocument ) ) ]
142+ internal sealed partial class EsqlJsonSerializerContext :
143+ JsonSerializerContext ;
123144}
0 commit comments