Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
filzrev opened this issue Oct 11, 2024 · 2 comments · Fixed by #992

Comments

@filzrev
Copy link
Contributor

filzrev commented Oct 11, 2024

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
    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
Copy link

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+)?$

@EdwardCooke
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants
@Schinkenkoenig @EdwardCooke @filzrev and others