|
| 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 | +#pragma warning disable IDE0005 |
| 6 | +using System; |
| 7 | +using System.Buffers.Text; |
| 8 | +using System.Globalization; |
| 9 | +using System.Text; |
| 10 | +using System.Text.Json; |
| 11 | +using System.Text.Json.Serialization; |
| 12 | +using static Elastic.Clients.Elasticsearch.Serialization.JsonConstants; |
| 13 | +#pragma warning restore IDE0005 |
| 14 | + |
| 15 | +namespace Elastic.Clients.Elasticsearch.Serialization; |
| 16 | + |
| 17 | +internal sealed class DoubleWithFractionalPortionConverter : JsonConverter<double> |
| 18 | +{ |
| 19 | + // We don't handle floating point literals (NaN, etc.) because for source serialization because Elasticsearch only support finite values for numeric fields. |
| 20 | + // We must handle the possibility of numbers as strings in the source however. |
| 21 | + |
| 22 | + public override double Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 23 | + { |
| 24 | + if (reader.TokenType == JsonTokenType.String && options.NumberHandling.HasFlag(JsonNumberHandling.AllowReadingFromString)) |
| 25 | + { |
| 26 | + var value = reader.GetString(); |
| 27 | + |
| 28 | + if (!double.TryParse(value, out var parsedValue)) |
| 29 | + ThrowHelper.ThrowJsonException($"Unable to parse '{value}' as a double."); |
| 30 | + |
| 31 | + return parsedValue; |
| 32 | + } |
| 33 | + |
| 34 | + return reader.GetDouble(); |
| 35 | + } |
| 36 | + |
| 37 | + public override void Write(Utf8JsonWriter writer, double value, JsonSerializerOptions options) |
| 38 | + { |
| 39 | + Span<byte> utf8bytes = stackalloc byte[128]; // This is the size used in STJ for future proofing. https://github.com/dotnet/runtime/blob/dae6c2472b699b7cff2efeb5ce06b75c9551bc40/src/libraries/System.Text.Json/src/System/Text/Json/JsonConstants.cs#L79 |
| 40 | + |
| 41 | + // NOTE: This code is based on https://github.com/dotnet/runtime/blob/dae6c2472b699b7cff2efeb5ce06b75c9551bc40/src/libraries/System.Text.Json/src/System/Text/Json/JsonConstants.cs#L79 |
| 42 | + |
| 43 | + // Frameworks that are not .NET Core 3.0 or higher do not produce round-trippable strings by |
| 44 | + // default. Further, the Utf8Formatter on older frameworks does not support taking a precision |
| 45 | + // specifier for 'G' nor does it represent other formats such as 'R'. As such, we duplicate |
| 46 | + // the .NET Core 3.0 logic of forwarding to the UTF16 formatter and transcoding it back to UTF8, |
| 47 | + // with some additional changes to remove dependencies on Span APIs which don't exist downlevel. |
| 48 | + |
| 49 | + // PERFORMANCE: This code could be benchmarked and tweaked to make it faster. |
| 50 | + |
| 51 | +#if NETCOREAPP |
| 52 | + if (Utf8Formatter.TryFormat(value, utf8bytes, out var bytesWritten)) |
| 53 | + { |
| 54 | + if (utf8bytes.IndexOfAny(NonIntegerBytes) == -1) |
| 55 | + { |
| 56 | + utf8bytes[bytesWritten++] = (byte)'.'; |
| 57 | + utf8bytes[bytesWritten++] = (byte)'0'; |
| 58 | + } |
| 59 | + |
| 60 | +#pragma warning disable IDE0057 // Use range operator |
| 61 | + writer.WriteRawValue(utf8bytes.Slice(0, bytesWritten), skipInputValidation: true); |
| 62 | +#pragma warning restore IDE0057 // Use range operator |
| 63 | + |
| 64 | + return; |
| 65 | + } |
| 66 | +#else |
| 67 | + var utf16Text = value.ToString("G17", CultureInfo.InvariantCulture); |
| 68 | + |
| 69 | + if (utf16Text.Length < utf8bytes.Length) |
| 70 | + { |
| 71 | + try |
| 72 | + { |
| 73 | + var bytes = Encoding.UTF8.GetBytes(utf16Text); |
| 74 | + |
| 75 | + if (bytes.Length < utf8bytes.Length) |
| 76 | + { |
| 77 | + bytes.CopyTo(utf8bytes); |
| 78 | + } |
| 79 | + } |
| 80 | + catch |
| 81 | + { |
| 82 | + // Swallow this and fall through to our general exception. |
| 83 | + } |
| 84 | + } |
| 85 | +#endif |
| 86 | + |
| 87 | + ThrowHelper.ThrowJsonException($"Unable to serialize double value."); |
| 88 | + } |
| 89 | +} |
0 commit comments