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 integral doubles/floats become strings. #994

Open
peeveen opened this issue Oct 16, 2024 · 0 comments
Open

Serialized integral doubles/floats become strings. #994

peeveen opened this issue Oct 16, 2024 · 0 comments

Comments

@peeveen
Copy link

peeveen commented Oct 16, 2024

If I serialize an integral double value (e.g 1.0), it becomes a string.

EDIT: From debugging, it looks like NumericRegex in JsonEventEmitter.cs is wrong. It does not match against single-digit numbers. For example, it matches 10, but not 1. Perhaps change it to something like ^-?\d+(\.\d+)?$.

EDIT2: I expect this will be fixed by this pull request. I hope it can be made official soon ... 😁

Here's my test code ...

[Fact]
public void TestNumberDeserialization() {
	var deserializer = new DeserializerBuilder()
		.WithAttemptingUnquotedStringTypeDeserialization()
		.Build();
	var yaml = string.Join('\n', "DoubleValue: 1.03", "OtherDoubleValue: 1.0", "StringValue: \"abc\"", "IntValue: 234", "BooleanValue: true");
	var yamlObject = deserializer.Deserialize(yaml);
	var serializer = new SerializerBuilder().JsonCompatible().Build();
	var json = serializer.Serialize(yamlObject);
	// At ths point, json = {"DoubleValue": 1.03, "OtherDoubleValue": "1", "StringValue": "abc", "IntValue": 234, "BooleanValue": true}
	// Why has OtherDoubleValue got quotes around it?
	// Next line fails with error about converting String to Double.
	var testObject = JsonSerializer.Deserialize<TestType>(json);
	testObject.DoubleValue.Should().Be(1.03);
	testObject.OtherDoubleValue.Should().Be(1.0);
	testObject.StringValue.Should().Be("abc");
	testObject.IntValue.Should().Be(234);
	testObject.BooleanValue.Should().BeTrue();
}
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

No branches or pull requests

3 participants
@peeveen and others