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

Add JValue formatter back #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions source/Handlebars.Extension.Test/IssueTests.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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);
}
}
}
6 changes: 5 additions & 1 deletion source/Handlebars.Extension.Test/JsonTests.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -37,19 +38,22 @@ public class EnvGenerator : IEnumerable<object[]>
[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);

var output = template(model).ToUpper();

Assert.Equal(value.Trim('"'), output);
}

[Theory]
[ClassData(typeof(EnvGenerator))]
public void JsonTestIfTruthy(IHandlebars handlebars)
Expand Down
10 changes: 9 additions & 1 deletion source/Handlebars.Extension/Formatters/JFormatterProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions source/Handlebars.Extension/Formatters/JValueFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using HandlebarsDotNet.IO;
using Newtonsoft.Json.Linq;

namespace HandlebarsDotNet.Extension.NewtonsoftJson.Formatters
{
public class JValueFormatter : IFormatter
{
public void Format<T>(T value, in EncodedTextWriter writer)
{
var token = value as JValue;
writer.Write(token!.Value);
}
}
}