diff --git a/source/Handlebars.Extension.Test/IssueTests.cs b/source/Handlebars.Extension.Test/IssueTests.cs index 7a531ce..c1754c9 100644 --- a/source/Handlebars.Extension.Test/IssueTests.cs +++ b/source/Handlebars.Extension.Test/IssueTests.cs @@ -1,5 +1,8 @@ +using System.Globalization; using System.Linq; +using System.Threading; using HandlebarsDotNet.Extension.NewtonsoftJson; +using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Xunit; @@ -89,5 +92,25 @@ public void PartialParametersInsideEachTest() Assert.Equal(actual, string.Join("", expected)); } + + // issue: https://github.com/Handlebars-Net/Handlebars.Net.Extension.NewtonsoftJson/issues/4 + [Fact] + public void FormatterRespectsCulture() + { + var value = 22474.1; + + var model = JsonConvert.DeserializeObject("{ \"value\": " + value.ToString(CultureInfo.InvariantCulture) + " }"); + + var source = "{{this.value}}"; + + var handlebars = Handlebars.Create(); + handlebars.Configuration.FormatProvider = CultureInfo.CreateSpecificCulture("de-DE"); + handlebars.Configuration.UseNewtonsoftJson(); + var template = handlebars.Compile(source); + + var output = template(model).ToUpper(); + + Assert.Equal(value.ToString(CultureInfo.CreateSpecificCulture("de-DE")), output); + } } } \ No newline at end of file diff --git a/source/Handlebars.Extension.Test/JsonTests.cs b/source/Handlebars.Extension.Test/JsonTests.cs index 76cc244..f7d6c18 100644 --- a/source/Handlebars.Extension.Test/JsonTests.cs +++ b/source/Handlebars.Extension.Test/JsonTests.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Globalization; using System.Linq; using HandlebarsDotNet.Collections; using HandlebarsDotNet.Extension.NewtonsoftJson; @@ -37,11 +38,14 @@ public class EnvGenerator : IEnumerable [InlineData("\"8C82D441-EE53-47C6-9400-3B5045A4DF71\"")] public void ValueTypes(string value) { + CultureInfo.DefaultThreadCurrentCulture = CultureInfo.CreateSpecificCulture("de-DE"); + var model = JsonConvert.DeserializeObject("{ \"value\": " + value + " }"); var source = "{{this.value}}"; var handlebars = Handlebars.Create(); + handlebars.Configuration.FormatProvider = CultureInfo.InvariantCulture; handlebars.Configuration.UseNewtonsoftJson(); var template = handlebars.Compile(source); @@ -49,7 +53,7 @@ public void ValueTypes(string value) Assert.Equal(value.Trim('"'), output); } - + [Theory] [ClassData(typeof(EnvGenerator))] public void JsonTestIfTruthy(IHandlebars handlebars) diff --git a/source/Handlebars.Extension/Formatters/JFormatterProvider.cs b/source/Handlebars.Extension/Formatters/JFormatterProvider.cs index ef648c6..dfd4912 100644 --- a/source/Handlebars.Extension/Formatters/JFormatterProvider.cs +++ b/source/Handlebars.Extension/Formatters/JFormatterProvider.cs @@ -8,10 +8,18 @@ namespace HandlebarsDotNet.Extension.NewtonsoftJson.Formatters public class JFormatterProvider : IFormatterProvider { private static readonly JTokenFormatter JTokenFormatter = new JTokenFormatter(); + private static readonly JValueFormatter JValueFormatter = new JValueFormatter(); public bool TryCreateFormatter(Type type, out IFormatter? formatter) { - if (typeof(JToken).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo())) + var typeInfo = type.GetTypeInfo(); + if (typeof(JValue).GetTypeInfo().IsAssignableFrom(typeInfo)) + { + formatter = JValueFormatter; + return true; + } + + if (typeof(JToken).GetTypeInfo().IsAssignableFrom(typeInfo)) { formatter = JTokenFormatter; return true; diff --git a/source/Handlebars.Extension/Formatters/JValueFormatter.cs b/source/Handlebars.Extension/Formatters/JValueFormatter.cs new file mode 100644 index 0000000..ed58724 --- /dev/null +++ b/source/Handlebars.Extension/Formatters/JValueFormatter.cs @@ -0,0 +1,14 @@ +using HandlebarsDotNet.IO; +using Newtonsoft.Json.Linq; + +namespace HandlebarsDotNet.Extension.NewtonsoftJson.Formatters +{ + public class JValueFormatter : IFormatter + { + public void Format(T value, in EncodedTextWriter writer) + { + var token = value as JValue; + writer.Write(token!.Value); + } + } +} \ No newline at end of file