From 4065c4f1752e7ffd464a787068ba4854ec701d09 Mon Sep 17 00:00:00 2001 From: Greg Dennis Date: Thu, 22 Jun 2023 08:34:44 +1200 Subject: [PATCH 1/4] use invariant culture when parsing numbers from yaml data --- Yaml2JsonNode/YamlConverter.cs | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/Yaml2JsonNode/YamlConverter.cs b/Yaml2JsonNode/YamlConverter.cs index 77d5bbe205..c51e963f35 100644 --- a/Yaml2JsonNode/YamlConverter.cs +++ b/Yaml2JsonNode/YamlConverter.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.Linq; using System.Text.Json.Nodes; using YamlDotNet.Core; @@ -101,23 +102,20 @@ private static YamlSequenceNode ToYamlSequence(this JsonArray arr) private static JsonValue ToJsonValue(this YamlScalarNode yaml) { - switch (yaml.Style) + return yaml.Style switch { - case ScalarStyle.Plain: - return decimal.TryParse(yaml.Value, out var d) - ? JsonValue.Create(d) - : bool.TryParse(yaml.Value, out var b) - ? JsonValue.Create(b) - : JsonValue.Create(yaml.Value)!; - case ScalarStyle.SingleQuoted: - case ScalarStyle.DoubleQuoted: - case ScalarStyle.Literal: - case ScalarStyle.Folded: - case ScalarStyle.Any: - return JsonValue.Create(yaml.Value)!; - default: - throw new ArgumentOutOfRangeException(); - } + ScalarStyle.Plain => decimal.TryParse(yaml.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out var d) + ? JsonValue.Create(d) + : bool.TryParse(yaml.Value, out var b) + ? JsonValue.Create(b) + : JsonValue.Create(yaml.Value)!, + ScalarStyle.SingleQuoted => JsonValue.Create(yaml.Value)!, + ScalarStyle.DoubleQuoted => JsonValue.Create(yaml.Value)!, + ScalarStyle.Literal => JsonValue.Create(yaml.Value)!, + ScalarStyle.Folded => JsonValue.Create(yaml.Value)!, + ScalarStyle.Any => JsonValue.Create(yaml.Value)!, + _ => throw new ArgumentOutOfRangeException() + }; } private static YamlScalarNode ToYamlScalar(this JsonValue val) From a76ce3fab37b5034315ff09437f309a3d4c4e088 Mon Sep 17 00:00:00 2001 From: Greg Dennis Date: Thu, 22 Jun 2023 08:41:25 +1200 Subject: [PATCH 2/4] add tests --- Yaml2JsonNode.Tests/ClientTests.cs | 49 +++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/Yaml2JsonNode.Tests/ClientTests.cs b/Yaml2JsonNode.Tests/ClientTests.cs index 4c5b8a7dff..e27350c653 100644 --- a/Yaml2JsonNode.Tests/ClientTests.cs +++ b/Yaml2JsonNode.Tests/ClientTests.cs @@ -1,4 +1,5 @@ -using System.Text.Json.Nodes; +using System.Globalization; +using System.Text.Json.Nodes; using Json.More; using NUnit.Framework; using YamlDotNet.RepresentationModel; @@ -49,4 +50,50 @@ public void Issue476_YamlNumberAsString() Console.WriteLine(text); } + + [Test] + public void Issue478_DecimalFormatting_Comma() + { + var culture = CultureInfo.CurrentCulture; + + try + { + CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("es-ES"); + + var yamlText = @"data: 4,789"; + + var expected = new JsonObject { ["data"] = 4789 }; + + var actual = YamlSerializer.Parse(yamlText).Single().ToJsonNode(); + + Assert.True(actual.IsEquivalentTo(expected)); + } + finally + { + CultureInfo.CurrentCulture = culture; + } + } + + [Test] + public void Issue478_DecimalFormatting_Dot() + { + var culture = CultureInfo.CurrentCulture; + + try + { + CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("es-ES"); + + var yamlText = @"data: 4.789"; + + var expected = new JsonObject { ["data"] = 4.789 }; + + var actual = YamlSerializer.Parse(yamlText).Single().ToJsonNode(); + + Assert.True(actual.IsEquivalentTo(expected)); + } + finally + { + CultureInfo.CurrentCulture = culture; + } + } } \ No newline at end of file From e228a82361d845413ec285570ff3f6ebf9909c20 Mon Sep 17 00:00:00 2001 From: Greg Dennis Date: Thu, 22 Jun 2023 08:43:33 +1200 Subject: [PATCH 3/4] update version and release notes --- Yaml2JsonNode/Yaml2JsonNode.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Yaml2JsonNode/Yaml2JsonNode.csproj b/Yaml2JsonNode/Yaml2JsonNode.csproj index 9676aeb8b1..43bc8f4a15 100644 --- a/Yaml2JsonNode/Yaml2JsonNode.csproj +++ b/Yaml2JsonNode/Yaml2JsonNode.csproj @@ -4,9 +4,9 @@ netstandard2.0 Yaml2JsonNode Greg Dennis - 1.2.1 + 1.2.2 1.0.0.0 - 1.2.1.0 + 1.2.2.0 Allows conversion of YamlDotNet's YAML models to JsonNodes. LICENSE https://github.com/gregsdennis/json-everything From 2dff9de386a21d63707a41f1d4f21f91c13af346 Mon Sep 17 00:00:00 2001 From: Greg Dennis Date: Thu, 22 Jun 2023 08:43:53 +1200 Subject: [PATCH 4/4] save release notes file this time --- tools/ApiDocsGenerator/release-notes/rn-yaml.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tools/ApiDocsGenerator/release-notes/rn-yaml.md b/tools/ApiDocsGenerator/release-notes/rn-yaml.md index 0496560ceb..28504b824b 100644 --- a/tools/ApiDocsGenerator/release-notes/rn-yaml.md +++ b/tools/ApiDocsGenerator/release-notes/rn-yaml.md @@ -4,6 +4,10 @@ title: Yaml2JsonNode icon: fas fa-tag order: "8.12" --- +# [1.2.2](https://github.com/gregsdennis/json-everything/pull/479) {#release-yaml-1.2.2} + +[#478](https://github.com/gregsdennis/json-everything/issues/478) - Converting numbers from YAML to JSON parses numbers against the machine's current culture, not the invariant culture. + # [1.2.1](https://github.com/gregsdennis/json-everything/pull/477) {#release-yaml-1.2.1} [#476](https://github.com/gregsdennis/json-everything/issues/476) - [@amis92](https://github.com/amis92) discovered that JSON -> YAML -> JSON doesn't work when the data has string-encoded numbers and proposed the fix.