aaubry / YamlDotNet

YamlDotNet is a .NET library for YAML
MIT License
2.56k stars 482 forks source link

Serialized JSON data of some floating point values (Infinite and NaN and with exponential) contains unexpected result #991

Open filzrev opened 2 weeks ago

filzrev commented 2 weeks ago

Describe the bug When serialize data to JSON. Some data are serialized as unexpected representation.

1. Serialized JSON data of floating number that contains Exponential Notation is serialized as string. This behavior is changed by #893.
And it's required to enable JsonNumberHandling.AllowReadingFromString options to deserialize these values as float/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("\""); } } } ```
Schinkenkoenig commented 1 week 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 JsonEventEmittercode 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+)?$

EdwardCooke commented 1 week ago

That looks like a legit fix. We do accept prs and would gladly take one to fix this.