|
2 | 2 | // Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
|
3 | 3 | // See the LICENSE file in the project root for more information.
|
4 | 4 |
|
| 5 | +// ********************************** |
5 | 6 | // IMPORTANT: These tests have a secondary use as code snippets used in documentation.
|
6 | 7 | // We disable formatting in sections of this file to ensure the correct indentation when tagged regions are
|
7 | 8 | // included in the asciidocs. While hard to read, this formatting should be left as-is for docs generation.
|
8 | 9 | // We also include using directives that are not required due to global using directives, but remain here
|
9 | 10 | // so that can appear in the documentation.
|
| 11 | +// ********************************** |
10 | 12 |
|
11 |
| -#pragma warning disable IDE0005 |
| 13 | +#pragma warning disable CS0105 // Using directive appeared previously in this namespace |
| 14 | +#pragma warning disable IDE0005 // Remove unnecessary using directives |
12 | 15 | //tag::usings[]
|
13 | 16 | //tag::converter-usings[]
|
14 | 17 | using System;
|
|
22 | 25 | using Elastic.Clients.Elasticsearch;
|
23 | 26 | using Elastic.Clients.Elasticsearch.Serialization;
|
24 | 27 | //end::usings[]
|
| 28 | +//tag::derived-converter-usings[] |
| 29 | +using System.Text.Json; |
| 30 | +using Elastic.Clients.Elasticsearch.Serialization; |
| 31 | +//end::derived-converter-usings[] |
| 32 | +//tag::vanilla-serializer-using-directives[] |
| 33 | +using System; |
| 34 | +using System.IO; |
| 35 | +using System.Threading; |
| 36 | +using System.Threading.Tasks; |
| 37 | +using Elastic.Transport; |
| 38 | +//end::vanilla-serializer-using-directives[] |
25 | 39 | using System.Text;
|
26 | 40 | using VerifyXunit;
|
27 | 41 | using System.IO;
|
28 |
| -#pragma warning restore IDE0005 |
| 42 | +using System.Threading; |
| 43 | +//tag::querydsl-using-directives[] |
| 44 | +using Elastic.Clients.Elasticsearch.QueryDsl; |
| 45 | +//end::querydsl-using-directives[] |
| 46 | +#pragma warning restore IDE0005 // Remove unnecessary using directives |
| 47 | +#pragma warning restore CS0105 // Using directive appeared previously in this namespace |
29 | 48 |
|
30 | 49 | namespace Tests.Documentation.Serialization;
|
31 | 50 |
|
@@ -67,7 +86,8 @@ static void ConfigureOptions(JsonSerializerOptions o) => // <1>
|
67 | 86 | #pragma warning disable format
|
68 | 87 | //tag::index-person[]
|
69 | 88 | var person = new Person { FirstName = "Steve" };
|
70 |
| - var indexResponse = await client.IndexAsync(person, "my-index-name"); //end::index-person[] |
| 89 | + var indexResponse = await client.IndexAsync(person, "my-index-name"); |
| 90 | + //end::index-person[] |
71 | 91 | #pragma warning restore format
|
72 | 92 |
|
73 | 93 | var requestJson = Encoding.UTF8.GetString(indexResponse.ApiCallDetails.RequestBodyInBytes);
|
@@ -297,77 +317,143 @@ public async Task DerivingFromSystemTextJsonSerializer_ToRegisterACustomEnumConv
|
297 | 317 | .DisableDirectStreaming();
|
298 | 318 | client = new ElasticsearchClient(settings);
|
299 | 319 |
|
300 |
| - var customer = new Customer |
301 |
| - { |
302 |
| - CustomerName = "Customer Ltd", |
303 |
| - Type = CustomerType.Enhanced |
304 |
| - }; |
| 320 | +#pragma warning disable format |
| 321 | +//tag::index-customer-without-jsonconverter-attribute[] |
| 322 | +var customer = new Customer |
| 323 | +{ |
| 324 | + CustomerName = "Customer Ltd", |
| 325 | + CustomerType = CustomerType.Enhanced |
| 326 | +}; |
305 | 327 |
|
306 |
| - var indexResponse = await client.IndexAsync(customer, "my-index-name"); |
| 328 | +var indexResponse = await client.IndexAsync(customer, "my-index-name"); |
| 329 | +//end::index-customer-without-jsonconverter-attribute[] |
| 330 | +#pragma warning restore format |
307 | 331 |
|
308 | 332 | var requestJson = Encoding.UTF8.GetString(indexResponse.ApiCallDetails.RequestBodyInBytes);
|
309 | 333 | await Verifier.Verify(requestJson);
|
310 | 334 |
|
311 | 335 | var ms = new MemoryStream(indexResponse.ApiCallDetails.RequestBodyInBytes);
|
312 | 336 | var deserializedCustomer = client.SourceSerializer.Deserialize<Customer>(ms);
|
313 | 337 | deserializedCustomer.CustomerName.Should().Be("Customer Ltd");
|
314 |
| - deserializedCustomer.Type.Should().Be(CustomerType.Enhanced); |
| 338 | + deserializedCustomer.CustomerType.Should().Be(CustomerType.Enhanced); |
315 | 339 | }
|
316 | 340 |
|
317 |
| - public class Customer |
318 |
| - { |
319 |
| - public string CustomerName { get; set; } |
320 |
| - public CustomerType Type { get; set; } |
321 |
| - } |
| 341 | +#pragma warning disable format |
| 342 | +//tag::customer-without-jsonconverter-attribute[] |
| 343 | +public class Customer |
| 344 | +{ |
| 345 | + public string CustomerName { get; set; } |
| 346 | + public CustomerType CustomerType { get; set; } |
| 347 | +} |
322 | 348 |
|
323 |
| - public enum CustomerType |
324 |
| - { |
325 |
| - Standard, |
326 |
| - Enhanced |
327 |
| - } |
| 349 | +public enum CustomerType |
| 350 | +{ |
| 351 | + Standard, |
| 352 | + Enhanced |
| 353 | +} |
| 354 | +//end::customer-without-jsonconverter-attribute[] |
| 355 | +#pragma warning restore format |
| 356 | + |
| 357 | +#pragma warning disable format |
| 358 | +//tag::my-custom-serializer[] |
| 359 | +public class MyCustomSerializer : SystemTextJsonSerializer // <1> |
| 360 | +{ |
| 361 | + private readonly JsonSerializerOptions _options; |
328 | 362 |
|
329 |
| - public class MyCustomSerializer : SystemTextJsonSerializer |
| 363 | + public MyCustomSerializer(IElasticsearchClientSettings settings) : base(settings) |
330 | 364 | {
|
331 |
| - private readonly JsonSerializerOptions _options; |
| 365 | + var options = DefaultSourceSerializer.CreateDefaultJsonSerializerOptions(false); // <2> |
332 | 366 |
|
333 |
| - public MyCustomSerializer(IElasticsearchClientSettings settings) : base(settings) |
334 |
| - { |
335 |
| - var options = DefaultSourceSerializer.CreateDefaultJsonSerializerOptions(false); |
| 367 | + options.Converters.Add(new CustomerTypeConverter()); // <3> |
336 | 368 |
|
337 |
| - options.Converters.Add(new CustomerTypeConverter()); |
| 369 | + _options = DefaultSourceSerializer.AddDefaultConverters(options); // <4> |
| 370 | + } |
338 | 371 |
|
339 |
| - _options = DefaultSourceSerializer.AddDefaultConverters(options); |
340 |
| - } |
| 372 | + protected override JsonSerializerOptions CreateJsonSerializerOptions() => _options; // <5> |
| 373 | +} |
| 374 | +//end::my-custom-serializer[] |
| 375 | +#pragma warning restore format |
341 | 376 |
|
342 |
| - protected override JsonSerializerOptions CreateJsonSerializerOptions() => _options; |
| 377 | +#pragma warning disable format |
| 378 | +//tag::customer-type-converter[] |
| 379 | +public class CustomerTypeConverter : JsonConverter<CustomerType> |
| 380 | +{ |
| 381 | + public override CustomerType Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 382 | + { |
| 383 | + return reader.GetString() switch // <1> |
| 384 | + { |
| 385 | + "basic" => CustomerType.Standard, |
| 386 | + "premium" => CustomerType.Enhanced, |
| 387 | + _ => throw new JsonException( |
| 388 | + $"Unknown value read when deserializing {nameof(CustomerType)}."), |
| 389 | + }; |
343 | 390 | }
|
344 | 391 |
|
345 |
| - public class CustomerTypeConverter : JsonConverter<CustomerType> |
| 392 | + public override void Write(Utf8JsonWriter writer, CustomerType value, JsonSerializerOptions options) |
346 | 393 | {
|
347 |
| - public override CustomerType Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 394 | + switch (value) // <2> |
348 | 395 | {
|
349 |
| - return reader.GetString() switch |
350 |
| - { |
351 |
| - "basic" => CustomerType.Standard, |
352 |
| - "premium" => CustomerType.Enhanced, |
353 |
| - _ => throw new JsonException( |
354 |
| - $"Unknown value read when deserializing {nameof(CustomerType)}."), |
355 |
| - }; |
| 396 | + case CustomerType.Standard: |
| 397 | + writer.WriteStringValue("basic"); |
| 398 | + return; |
| 399 | + case CustomerType.Enhanced: |
| 400 | + writer.WriteStringValue("premium"); |
| 401 | + return; |
356 | 402 | }
|
357 | 403 |
|
358 |
| - public override void Write(Utf8JsonWriter writer, CustomerType value, JsonSerializerOptions options) |
359 |
| - { |
360 |
| - switch (value) |
361 |
| - { |
362 |
| - case CustomerType.Standard: |
363 |
| - writer.WriteStringValue("basic"); |
364 |
| - return; |
365 |
| - case CustomerType.Enhanced: |
366 |
| - writer.WriteStringValue("premium"); |
367 |
| - return; |
368 |
| - } |
369 |
| - |
370 |
| - writer.WriteNullValue(); |
371 |
| - } |
| 404 | + writer.WriteNullValue(); |
372 | 405 | }
|
373 | 406 | }
|
| 407 | +//end::customer-type-converter[] |
| 408 | +#pragma warning restore format |
| 409 | + |
| 410 | +public void RegisterVanillaSerializer() |
| 411 | +{ |
| 412 | +#pragma warning disable format |
| 413 | +//tag::register-vanilla-serializer[] |
| 414 | +var nodePool = new SingleNodePool(new Uri("http://localhost:9200")); |
| 415 | +var settings = new ElasticsearchClientSettings( |
| 416 | + nodePool, |
| 417 | + sourceSerializer: (defaultSerializer, settings) => |
| 418 | + new VanillaSerializer()); // <1> |
| 419 | +var client = new ElasticsearchClient(settings); |
| 420 | +//end::register-vanilla-serializer[] |
| 421 | +#pragma warning restore format |
| 422 | +} |
| 423 | + |
| 424 | +#pragma warning disable format |
| 425 | +//tag::vanilla-serializer[] |
| 426 | +public class VanillaSerializer : Serializer |
| 427 | +{ |
| 428 | + public override object Deserialize(Type type, Stream stream) => |
| 429 | + throw new NotImplementedException(); |
| 430 | + |
| 431 | + public override T Deserialize<T>(Stream stream) => |
| 432 | + throw new NotImplementedException(); |
| 433 | + |
| 434 | + public override ValueTask<object> DeserializeAsync(Type type, Stream stream, CancellationToken cancellationToken = default) => |
| 435 | + throw new NotImplementedException(); |
| 436 | + |
| 437 | + public override ValueTask<T> DeserializeAsync<T>(Stream stream, CancellationToken cancellationToken = default) => |
| 438 | + throw new NotImplementedException(); |
| 439 | + |
| 440 | + public override void Serialize<T>(T data, Stream stream, SerializationFormatting formatting = SerializationFormatting.None) => |
| 441 | + throw new NotImplementedException(); |
| 442 | + |
| 443 | + public override Task SerializeAsync<T>(T data, Stream stream, |
| 444 | + SerializationFormatting formatting = SerializationFormatting.None, CancellationToken cancellationToken = default) => |
| 445 | + throw new NotImplementedException(); |
| 446 | +} |
| 447 | +//end::vanilla-serializer[] |
| 448 | +#pragma warning restore format |
| 449 | + |
| 450 | +#pragma warning disable format |
| 451 | +//tag::percolation-document[] |
| 452 | +public class MyPercolationDocument |
| 453 | +{ |
| 454 | + public Query Query { get; set; } |
| 455 | + public string Category { get; set; } |
| 456 | +} |
| 457 | +//end::percolation-document[] |
| 458 | +#pragma warning restore format |
| 459 | +} |
0 commit comments