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

JsonCompatible serializer is not Json compatible #893

Closed
Rackover opened this issue Jan 17, 2024 · 5 comments · Fixed by #941
Closed

JsonCompatible serializer is not Json compatible #893

Rackover opened this issue Jan 17, 2024 · 5 comments · Fixed by #941

Comments

@Rackover
Copy link

Describe the bug
When serializing an object that contains an infinity float, the YAML DotNet serializer in JsonCompatible mode produces a JSON that contains the keyword .inf .
According to JSON spec it should write null, otherwise i suppose writing a very big number or enclosing .inf with quotes would be appreciated. Currently the JSON produced by the serializer in that context is unusable for other parsers that expect valid data.

To Reproduce

 YamlDotNet.Serialization.SerializerBuilder serializerBuilder = new();
 serializerBuilder.JsonCompatible();
 serializerBuilder.WithIndentedSequences();

 var serializer = serializerBuilder.Build();

class Test
{
float a = float.PositiveInfinity;
}

var str = serializer.Serialize(a);
Console.Write(str);
@EdwardCooke
Copy link
Collaborator

Thanks for the report. It’s on my todo list. Might be a bit before I get to it. If you need it sooner go ahead and create a pr. You’ll need to handle other data types like double as well.

@EdwardCooke
Copy link
Collaborator

Can you point me to the JSON spec you're looking at for positive/negative infinity floats?

@Rackover
Copy link
Author

Rackover commented Jan 22, 2024

I realize I might have made it up, it seems the real status of Infinity and NaN is unclear:
https://www.ietf.org/rfc/rfc4627.txt

So it could be Null, it could be Undefined, or as someone proposed here https://stackoverflow.com/a/28763430/6230450 it could be the string "Infinity". Either choice is good I suppose as long as the final produced JSON is valid to deserialize.

@Rackover
Copy link
Author

Rackover commented Feb 19, 2024

            string serialized = serializer.Serialize(heatpoint);
            Regex reg = new Regex("(\":) (\\.inf)");
            serialized = reg.Replace(serialized, "$1 0");

Currently using this as a workaround whenever i do any JSON serialization with the YAMLDotNet serializer (in case this helps anyone). I created a stub PR to hopefully make it easier for you to look into it 🙂 hope that helps !!

@EdwardCooke
Copy link
Collaborator

This will be fixed in the next release.

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.

2 participants