Skip to content

Commit b06c403

Browse files
committed
Fix floating point serialization for NETFRAMEWORK builds (#8742)
(cherry picked from commit f81d81a)
1 parent 33f13f4 commit b06c403

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/Elastic.Clients.Elasticsearch/_Shared/Next/DoubleWithFractionalPortionConverter.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@ internal sealed class DoubleWithFractionalPortionConverter :
1717
// https://github.com/dotnet/runtime/blob/main/src/libraries/System.Text.Json/src/System/Text/Json/JsonConstants.cs#L78
1818
public const int MaximumFormatLength = 128 + 2;
1919

20+
#if !NETFRAMEWORK
21+
// Use G17 to ensure round-tripping of double values.
22+
// See here: https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings#the-g-format-specifier
2023
private static readonly StandardFormat DefaultFormat = StandardFormat.Parse("G17");
24+
#else
25+
// .NET Framework does not support a custom precision specifier with the G format.
26+
private static readonly StandardFormat DefaultFormat = StandardFormat.Parse("G");
27+
#endif
28+
2129
private static readonly JsonEncodedText NaN = JsonEncodedText.Encode("NaN"u8);
2230
private static readonly JsonEncodedText PositiveInfinity = JsonEncodedText.Encode("Infinity"u8);
2331
private static readonly JsonEncodedText NegativeInfinity = JsonEncodedText.Encode("-Infinity"u8);

src/Elastic.Clients.Elasticsearch/_Shared/Next/SingleWithFractionalPortionConverter.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@ internal sealed class SingleWithFractionalPortionConverter :
1717
// https://github.com/dotnet/runtime/blob/main/src/libraries/System.Text.Json/src/System/Text/Json/JsonConstants.cs#L79
1818
public const int MaximumFormatLength = 128 + 2;
1919

20+
#if !NETFRAMEWORK
21+
// Use G9 to ensure round-tripping of float values.
22+
// See here: https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings#the-g-format-specifier
2023
private static readonly StandardFormat DefaultFormat = StandardFormat.Parse("G9");
24+
#else
25+
// .NET Framework does not support a custom precision specifier with the G format.
26+
private static readonly StandardFormat DefaultFormat = StandardFormat.Parse("G");
27+
#endif
28+
2129
private static readonly JsonEncodedText NaN = JsonEncodedText.Encode("NaN"u8);
2230
private static readonly JsonEncodedText PositiveInfinity = JsonEncodedText.Encode("Infinity"u8);
2331
private static readonly JsonEncodedText NegativeInfinity = JsonEncodedText.Encode("-Infinity"u8);

0 commit comments

Comments
 (0)