Skip to content

Commit

Permalink
Merge pull request #479 from gregsdennis/yaml/decimal-parsing
Browse files Browse the repository at this point in the history
use invariant culture when parsing numbers from yaml data
  • Loading branch information
gregsdennis committed Jun 21, 2023
2 parents 114428e + 2dff9de commit b026cbf
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 19 deletions.
49 changes: 48 additions & 1 deletion Yaml2JsonNode.Tests/ClientTests.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}
}
}
4 changes: 2 additions & 2 deletions Yaml2JsonNode/Yaml2JsonNode.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<TargetFramework>netstandard2.0</TargetFramework>
<PackageId>Yaml2JsonNode</PackageId>
<Authors>Greg Dennis</Authors>
<Version>1.2.1</Version>
<Version>1.2.2</Version>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<FileVersion>1.2.1.0</FileVersion>
<FileVersion>1.2.2.0</FileVersion>
<Description>Allows conversion of YamlDotNet's YAML models to JsonNodes.</Description>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageProjectUrl>https://github.com/gregsdennis/json-everything</PackageProjectUrl>
Expand Down
30 changes: 14 additions & 16 deletions Yaml2JsonNode/YamlConverter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.Json.Nodes;
using YamlDotNet.Core;
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions tools/ApiDocsGenerator/release-notes/rn-yaml.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit b026cbf

Please sign in to comment.