|
| 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 | + |
| 9 | +namespace Elastic.Clients.Elasticsearch.Serialization; |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// A delegate that selects a union variant (e.g. based on the current JSON token type). |
| 13 | +/// </summary> |
| 14 | +/// <param name="reader">A reference to a <see cref="Utf8JsonReader"/> instance.</param> |
| 15 | +/// <param name="options">The <see cref="JsonSerializerOptions"/> to use.</param> |
| 16 | +/// <returns>The selected <see cref="UnionTag"/> value.</returns> |
| 17 | +/// <remarks> |
| 18 | +/// IMPORTANT: |
| 19 | +/// The <see cref="Utf8JsonReader"/> is passed by reference for best performance. If the selector function |
| 20 | +/// implementation needs to advance the reader position, it must always operate on a copy of the original |
| 21 | +/// <paramref name="reader"/> or restore the reader state before returning. |
| 22 | +/// </remarks> |
| 23 | +internal delegate UnionTag JsonUnionSelectorFunc(ref Utf8JsonReader reader, JsonSerializerOptions options); |
| 24 | + |
| 25 | +[Flags] |
| 26 | +internal enum JsonTokenTypes |
| 27 | +{ |
| 28 | + None = 0, |
| 29 | + StartObject = 1 << JsonTokenType.StartObject, |
| 30 | + EndObject = 1 << JsonTokenType.EndObject, |
| 31 | + StartArray = 1 << JsonTokenType.StartArray, |
| 32 | + EndArray = 1 << JsonTokenType.EndArray, |
| 33 | + String = 1 << JsonTokenType.String | 1 << JsonTokenType.PropertyName, |
| 34 | + Number = 1 << JsonTokenType.Number, |
| 35 | + True = 1 << JsonTokenType.True, |
| 36 | + False = 1 << JsonTokenType.False |
| 37 | +} |
| 38 | + |
| 39 | +internal static class JsonUnionSelector |
| 40 | +{ |
| 41 | + public static UnionTag ByTokenType(ref Utf8JsonReader reader, JsonSerializerOptions options, JsonTokenTypes first, JsonTokenTypes second) |
| 42 | + { |
| 43 | + _ = reader; |
| 44 | + _ = options; |
| 45 | + |
| 46 | + if (((int)first & (1 << (int)reader.TokenType)) is not 0) |
| 47 | + { |
| 48 | + return UnionTag.T1; |
| 49 | + } |
| 50 | + |
| 51 | + if (((int)second & (1 << (int)reader.TokenType)) is not 0) |
| 52 | + { |
| 53 | + return UnionTag.T2; |
| 54 | + } |
| 55 | + |
| 56 | + return UnionTag.None; |
| 57 | + } |
| 58 | + |
| 59 | + public static UnionTag ByPropertyOfT1(ref Utf8JsonReader reader, JsonSerializerOptions options, string name) |
| 60 | + { |
| 61 | + reader.ValidateToken(JsonTokenType.StartObject); |
| 62 | + |
| 63 | + var internalReader = reader; |
| 64 | + |
| 65 | + while (internalReader.Read() && (internalReader.TokenType is JsonTokenType.PropertyName)) |
| 66 | + { |
| 67 | + if (internalReader.ValueTextEquals(name)) |
| 68 | + { |
| 69 | + return UnionTag.T1; |
| 70 | + } |
| 71 | + |
| 72 | + internalReader.Read(); |
| 73 | + internalReader.Skip(); |
| 74 | + } |
| 75 | + |
| 76 | + return UnionTag.T2; |
| 77 | + } |
| 78 | + |
| 79 | + public static UnionTag ByPropertyOfT2(ref Utf8JsonReader reader, JsonSerializerOptions options, string name) |
| 80 | + { |
| 81 | + reader.ValidateToken(JsonTokenType.StartObject); |
| 82 | + |
| 83 | + var internalReader = reader; |
| 84 | + |
| 85 | + while (internalReader.Read() && (internalReader.TokenType is JsonTokenType.PropertyName)) |
| 86 | + { |
| 87 | + if (internalReader.ValueTextEquals(name)) |
| 88 | + { |
| 89 | + return UnionTag.T2; |
| 90 | + } |
| 91 | + |
| 92 | + internalReader.Read(); |
| 93 | + internalReader.Skip(); |
| 94 | + } |
| 95 | + |
| 96 | + return UnionTag.T1; |
| 97 | + } |
| 98 | +} |
0 commit comments