Skip to content

Commit 77d2b28

Browse files
authored
Fix bug with JsonPrimitiveVisitor not respecting the culture of JsonSerializer. (#51)
1 parent 0b7c92b commit 77d2b28

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

src/Winton.Extensions.Configuration.Consul/Parsers/Json/JsonPrimitiveVisitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ private ICollection<KeyValuePair<string, string>> VisitPrimitive(JToken primitiv
3333
string key = ConfigurationPath.Combine(_context.Reverse());
3434
return new[]
3535
{
36-
new KeyValuePair<string, string>(key, primitive.ToString())
36+
new KeyValuePair<string, string>(key, primitive.Value<string>())
3737
};
3838
}
3939

test/Winton.Extensions.Configuration.Consul.Test/Parsers/Json/JsonPrimitiveVisitorTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System.Collections.Generic;
2+
using System.Globalization;
3+
using System.Threading;
24
using FluentAssertions;
35
using Newtonsoft.Json.Linq;
46
using Xunit;
@@ -77,6 +79,29 @@ public sealed class VisitJObject : JsonPrimitiveVisitorTests
7779
}
7880
};
7981

82+
[Fact]
83+
private void ShouldConvertPrimitivesToStringUsingJsonSerializerCulture()
84+
{
85+
CultureInfo originalCuluture = Thread.CurrentThread.CurrentCulture;
86+
Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
87+
88+
try
89+
{
90+
IEnumerable<KeyValuePair<string, string>> primitives =
91+
_visitor.VisitJObject(new JObject(new JProperty("Test", new JValue(1.5))));
92+
93+
primitives.Should().BeEquivalentTo(
94+
new List<KeyValuePair<string, string>>
95+
{
96+
new KeyValuePair<string, string>("Test", "1.5")
97+
});
98+
}
99+
finally
100+
{
101+
Thread.CurrentThread.CurrentCulture = originalCuluture;
102+
}
103+
}
104+
80105
[Theory]
81106
[MemberData(nameof(TestCases))]
82107
private void ShouldVisitAllPrimitivesInJObject(

0 commit comments

Comments
 (0)