Open filzrev opened 2 weeks ago
Having the same problem currently while updating to 16.x. We have normal floating points (all digits) but there is similar problem with floats like 3.0, 0.0 etc. Essentially single digit at the front and ending on .0.
After looking at the code for a while I think the problem is that during the JsonEventEmitter
code the "G" string formatting for doubles will remove the .0 and the numericRegex ^-?\d+\.?\d+$"
does not match single digit numbers.
Issue being here that the regex made only the "." optional, not the \d afterwards.
Should be fixable with this ^-?\d+(\.\d+)?$
That looks like a legit fix. We do accept prs and would gladly take one to fix this.
Describe the bug When serialize data to JSON. Some data are serialized as unexpected representation.
1. Serialized JSON data of
floating number
that containsExponential Notation
is serialized asstring
. This behavior is changed by #893.And it's required to enable
JsonNumberHandling.AllowReadingFromString
options to deserialize these values asfloat/double
.Older version of YamlDotNet serialize these data as
number
.2. Serialized JSON data of
Nan
/PositiveInfinite
/NegativeInfinite
can't be deserialized with System.Text.Json YamlDotNet serialize these values as string(.nan
/.inf
/-.inf
). But it's not compatible with JsonNumberHandling.AllowNamedFloatingPointLiterals options.To Reproduce
UnitTest Code
```csharp public class YamlJsonRoundTripTests { private static ISerializer YamlJsonSerializer = new SerializerBuilder().JsonCompatible().Build(); private static JsonSerializerOptions CustomSerializerOptions = new JsonSerializerOptions { NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.AllowNamedFloatingPointLiterals }; [Theory] [InlineData(1.234d, false)] [InlineData(double.MinValue, false)] [InlineData(double.MaxValue, false)] // Following data should be serialized to quoted JSON value. [InlineData(double.PositiveInfinity, true)] [InlineData(double.NegativeInfinity, true)] [InlineData(double.NaN, true)] public void YamlJsonRoundTripTest_SystemTextJson(object value, bool expectQuoted) { // Arrange/Act string json = YamlJsonSerializer.Serialize(value); var result = System.Text.Json.JsonSerializer.Deserialize(json, value.GetType(), CustomSerializerOptions); // Assert result.Should().BeOfType(value.GetType()); result.Should().Be(value); if (expectQuoted) { json.Should().StartWith("\"") .And .EndWith("\""); } else { json.Should().NotStartWith("\"") .And .NotEndWith("\""); } } } ```