Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp2.1;netcoreapp3.1</TargetFrameworks>
<TargetFrameworks>netcoreapp2.1;netcoreapp3.1;net6.0</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT'">$(TargetFrameworks);net472</TargetFrameworks>
<ProjectGuid>1956A22F-7B26-4747-8125-7EAC0B94665D</ProjectGuid>
<RootNamespace>HandlebarsDotNet.Extension.Test</RootNamespace>
Expand Down Expand Up @@ -53,7 +53,7 @@
</ItemGroup>


<ItemGroup Condition="'$(TargetFramework)'=='netcoreapp3.1' or '$(TargetFramework)'=='netcoreapp2.1'">
<ItemGroup Condition="'$(TargetFramework)'=='netcoreapp2.1' or '$(TargetFramework)'=='netcoreapp3.1' or '$(TargetFramework)'=='net6.0'">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="System.Collections.NonGeneric" Version="4.3.0" />
</ItemGroup>
Expand Down
77 changes: 43 additions & 34 deletions source/Handlebars.Extension.Test/JsonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@ namespace HandlebarsDotNet.Extension.Test
{
public class JsonTests
{
public delegate object JsonModelFactory(string json);

public class EnvGenerator : IEnumerable<object[]>
{
private readonly List<IHandlebars> _data = new List<IHandlebars>
private readonly List<(IHandlebars, JsonModelFactory)> _data = new List<(IHandlebars, JsonModelFactory)>
{
Handlebars.Create(new HandlebarsConfiguration().UseJson())
(Handlebars.Create(new HandlebarsConfiguration().UseJson()), json => JsonDocument.Parse(json)),
#if NET6_0_OR_GREATER
(Handlebars.Create(new HandlebarsConfiguration().UseJson()), json => System.Text.Json.Nodes.JsonNode.Parse(json)),
#endif
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this still be needed if we bumped system.text.json to v6?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bumping STJ package to v6 will eliminate the need for this conditional code.

};

public IEnumerator<object[]> GetEnumerator() => _data.Select(o => new object[] { o }).GetEnumerator();
public IEnumerator<object[]> GetEnumerator() => _data.Select(item => new object[] { item.Item1, item.Item2 }).GetEnumerator();

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
Expand Down Expand Up @@ -51,9 +56,9 @@ public void ValueTypes(string value)

[Theory]
[ClassData(typeof(EnvGenerator))]
public void JsonTestIfTruthy(IHandlebars handlebars)
public void JsonTestIfTruthy(IHandlebars handlebars, JsonModelFactory jsonModelFactory)
{
var model = JsonDocument.Parse("{\"truthy\":true}");
var model = jsonModelFactory("{\"truthy\":true}");

var source = "{{#if truthy}}{{truthy}}{{/if}}";

Expand All @@ -66,9 +71,9 @@ public void JsonTestIfTruthy(IHandlebars handlebars)

[Theory]
[ClassData(typeof(EnvGenerator))]
public void JsonTestIfFalsy(IHandlebars handlebars)
public void JsonTestIfFalsy(IHandlebars handlebars, JsonModelFactory jsonModelFactory)
{
var model = JsonDocument.Parse("{\"falsy\":false}");
var model = jsonModelFactory("{\"falsy\":false}");

var source = "{{#if (not falsy)}}{{falsy}}{{/if}}";

Expand All @@ -82,9 +87,9 @@ public void JsonTestIfFalsy(IHandlebars handlebars)

[Theory]
[ClassData(typeof(EnvGenerator))]
public void JsonTestIfFalsyMissingField(IHandlebars handlebars)
public void JsonTestIfFalsyMissingField(IHandlebars handlebars, JsonModelFactory jsonModelFactory)
{
var model = JsonDocument.Parse("{\"myfield\":\"test1\"}");
var model = jsonModelFactory("{\"myfield\":\"test1\"}");

var source = "{{myfield}}{{#if mymissingfield}}{{mymissingfield}}{{/if}}";

Expand All @@ -97,9 +102,9 @@ public void JsonTestIfFalsyMissingField(IHandlebars handlebars)

[Theory]
[ClassData(typeof(EnvGenerator))]
public void JsonTestIfFalsyValue(IHandlebars handlebars)
public void JsonTestIfFalsyValue(IHandlebars handlebars, JsonModelFactory jsonModelFactory)
{
var model = JsonDocument.Parse("{\"myfield\":\"test1\",\"falsy\":null}");
var model = jsonModelFactory("{\"myfield\":\"test1\",\"falsy\":null}");

var source = "{{myfield}}{{#if falsy}}{{falsy}}{{/if}}";

Expand All @@ -112,9 +117,9 @@ public void JsonTestIfFalsyValue(IHandlebars handlebars)

[Theory]
[ClassData(typeof(EnvGenerator))]
public void ArrayIterator(IHandlebars handlebars)
public void ArrayIterator(IHandlebars handlebars, JsonModelFactory jsonModelFactory)
{
var model = JsonDocument.Parse("[{\"Key\": \"Key1\", \"Value\": \"Val1\"},{\"Key\": \"Key2\", \"Value\": \"Val2\"}]");
var model = jsonModelFactory("[{\"Key\": \"Key1\", \"Value\": \"Val1\"},{\"Key\": \"Key2\", \"Value\": \"Val2\"}]");

var source = "{{#each this}}{{Key}}{{Value}}{{/each}}";

Expand All @@ -127,9 +132,9 @@ public void ArrayIterator(IHandlebars handlebars)

[Theory]
[ClassData(typeof(EnvGenerator))]
public void ArrayIteratorProperties(IHandlebars handlebars)
public void ArrayIteratorProperties(IHandlebars handlebars, JsonModelFactory jsonModelFactory)
{
var model = JsonDocument.Parse("[{\"Key\": \"Key1\", \"Value\": \"Val1\"},{\"Key\": \"Key2\", \"Value\": \"Val2\"}]");
var model = jsonModelFactory("[{\"Key\": \"Key1\", \"Value\": \"Val1\"},{\"Key\": \"Key2\", \"Value\": \"Val2\"}]");

var source = "{{#each this}}{{@index}}-{{@first}}-{{@last}}-{{@value.Key}}-{{@value.Value}};{{/each}}";

Expand All @@ -142,9 +147,9 @@ public void ArrayIteratorProperties(IHandlebars handlebars)

[Theory]
[ClassData(typeof(EnvGenerator))]
public void ArrayIndexProperties(IHandlebars handlebars)
public void ArrayIndexProperties(IHandlebars handlebars, JsonModelFactory jsonModelFactory)
{
var model = JsonDocument.Parse("[\"Index0\", \"Index1\"]");
var model = jsonModelFactory("[\"Index0\", \"Index1\"]");

var source = "{{@root.1}}";

Expand All @@ -157,9 +162,9 @@ public void ArrayIndexProperties(IHandlebars handlebars)

[Theory]
[ClassData(typeof(EnvGenerator))]
public void ArrayIndexPropertiesNested(IHandlebars handlebars)
public void ArrayIndexPropertiesNested(IHandlebars handlebars, JsonModelFactory jsonModelFactory)
{
var model = JsonDocument.Parse("[{}, {\"Items\": [\"Index0\", \"Index1\"]}]");
var model = jsonModelFactory("[{}, {\"Items\": [\"Index0\", \"Index1\"]}]");

var source = "{{@root.1.Items.1}}";

Expand All @@ -172,9 +177,9 @@ public void ArrayIndexPropertiesNested(IHandlebars handlebars)

[Theory]
[ClassData(typeof(EnvGenerator))]
public void ArrayCount(IHandlebars handlebars)
public void ArrayCount(IHandlebars handlebars, JsonModelFactory jsonModelFactory)
{
var model = JsonDocument.Parse("[{\"Key\": \"Key1\", \"Value\": \"Val1\"},{\"Key\": \"Key2\", \"Value\": \"Val2\"}]");
var model = jsonModelFactory("[{\"Key\": \"Key1\", \"Value\": \"Val1\"},{\"Key\": \"Key2\", \"Value\": \"Val2\"}]");

var source = "{{this.Count}} = {{this.Length}}";

Expand All @@ -187,9 +192,9 @@ public void ArrayCount(IHandlebars handlebars)

[Theory]
[ClassData(typeof(EnvGenerator))]
public void ArrayListProperties(IHandlebars handlebars)
public void ArrayListProperties(IHandlebars handlebars, JsonModelFactory jsonModelFactory)
{
var model = JsonDocument.Parse("[{\"Key\": \"Key1\", \"Value\": \"Val1\"},{\"Key\": \"Key2\", \"Value\": \"Val2\"}]");
var model = jsonModelFactory("[{\"Key\": \"Key1\", \"Value\": \"Val1\"},{\"Key\": \"Key2\", \"Value\": \"Val2\"}]");

var source = "{{listProperties this}}";

Expand All @@ -203,8 +208,9 @@ public void ArrayListProperties(IHandlebars handlebars)

[Theory]
[ClassData(typeof(EnvGenerator))]
public void ObjectIterator(IHandlebars handlebars){
var model = JsonDocument.Parse("{\"Key1\": \"Val1\", \"Key2\": \"Val2\"}");
public void ObjectIterator(IHandlebars handlebars, JsonModelFactory jsonModelFactory)
{
var model = jsonModelFactory("{\"Key1\": \"Val1\", \"Key2\": \"Val2\"}");

var source = "{{#each this}}{{@key}}{{@value}}{{/each}}";

Expand All @@ -217,8 +223,9 @@ public void ObjectIterator(IHandlebars handlebars){

[Theory]
[ClassData(typeof(EnvGenerator))]
public void ObjectIteratorProperties(IHandlebars handlebars){
var model = JsonDocument.Parse("{\"Key1\": \"Val1\", \"Key2\": \"Val2\"}");
public void ObjectIteratorProperties(IHandlebars handlebars, JsonModelFactory jsonModelFactory)
{
var model = jsonModelFactory("{\"Key1\": \"Val1\", \"Key2\": \"Val2\"}");

var source = "{{#each this}}{{@index}}-{{@first}}-{{@last}}-{{@key}}-{{@value}};{{/each}}";

Expand All @@ -231,8 +238,9 @@ public void ObjectIteratorProperties(IHandlebars handlebars){

[Theory]
[ClassData(typeof(EnvGenerator))]
public void ObjectListProperties(IHandlebars handlebars){
var model = JsonDocument.Parse("{\"Key1\": \"Val1\", \"Key2\": \"Val2\"}");
public void ObjectListProperties(IHandlebars handlebars, JsonModelFactory jsonModelFactory)
{
var model = jsonModelFactory("{\"Key1\": \"Val1\", \"Key2\": \"Val2\"}");

var source = "{{ListProperties this}}";
handlebars.RegisterHelper(new ListPropertiesHelper());
Expand All @@ -246,8 +254,9 @@ public void ObjectListProperties(IHandlebars handlebars){

[Theory]
[ClassData(typeof(EnvGenerator))]
public void ObjectIteratorPropertiesWithLast(IHandlebars handlebars){
var model = JsonDocument.Parse("{\"Key1\": \"Val1\", \"Key2\": \"Val2\"}");
public void ObjectIteratorPropertiesWithLast(IHandlebars handlebars, JsonModelFactory jsonModelFactory)
{
var model = jsonModelFactory("{\"Key1\": \"Val1\", \"Key2\": \"Val2\"}");

var source = "{{#each this}}{{@index}}-{{@first}}-{{@last}}-{{@key}}-{{@value}};{{/each}}";

Expand All @@ -261,7 +270,7 @@ public void ObjectIteratorPropertiesWithLast(IHandlebars handlebars){

[Theory]
[ClassData(typeof(EnvGenerator))]
public void WithParentIndexJsonNet(IHandlebars handlebars)
public void WithParentIndexJsonNet(IHandlebars handlebars, JsonModelFactory jsonModelFactory)
{
var source = @"
{{#each level1}}
Expand Down Expand Up @@ -328,9 +337,9 @@ public void WithParentIndexJsonNet(IHandlebars handlebars)
}
};

var json = JsonDocument.Parse(JsonSerializer.Serialize(data));
var model = jsonModelFactory(JsonSerializer.Serialize(data));

var result = template(json);
var result = template(model);

const string expected = @"
id=0
Expand Down
31 changes: 31 additions & 0 deletions source/Handlebars.Extension/CountMemberAliasProvider.JsonNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#if NET6_0_OR_GREATER
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed if STJ is bumped to v6?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bumping STJ package to v6 will eliminate the need for this conditional code.


using System;
using System.Text.Json.Nodes;
using HandlebarsDotNet.PathStructure;

namespace HandlebarsDotNet.Extension.Json
{
public partial class CountMemberAliasProvider : IMemberAliasProvider<JsonNode>
{
public bool TryGetMemberByAlias(JsonNode instance, Type targetType, ChainSegment memberAlias, out object? value)
{
if (!EqualsIgnoreCase("count", memberAlias) && !EqualsIgnoreCase("length", memberAlias))
{
value = null;
return false;
}

if (!(instance is JsonArray jsonArray))
{
value = null;
return false;
}

value = jsonArray.Count;
return true;
}
}
}

#endif
10 changes: 5 additions & 5 deletions source/Handlebars.Extension/CountMemberAliasProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace HandlebarsDotNet.Extension.Json
{
public class CountMemberAliasProvider : IMemberAliasProvider<JsonElement>
public partial class CountMemberAliasProvider : IMemberAliasProvider<JsonElement>
{
public bool TryGetMemberByAlias(JsonElement instance, Type targetType, ChainSegment memberAlias, out object? value)
{
Expand All @@ -22,11 +22,11 @@ public bool TryGetMemberByAlias(JsonElement instance, Type targetType, ChainSegm

value = instance.GetArrayLength();
return true;
}

static bool EqualsIgnoreCase(string a, ChainSegment b)
{
return string.Equals(a, b.TrimmedValue, StringComparison.OrdinalIgnoreCase);
}
private static bool EqualsIgnoreCase(string a, ChainSegment b)
{
return string.Equals(a, b.TrimmedValue, StringComparison.OrdinalIgnoreCase);
}
}
}
2 changes: 1 addition & 1 deletion source/Handlebars.Extension/Handlebars.Extension.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<AssemblyName>HandlebarsDotNet.Extension.Json</AssemblyName>
<ProjectGuid>25080858-B620-4985-8AEF-E135324081B3</ProjectGuid>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">$(TargetFrameworks);net472</TargetFrameworks>
<Version>1.0.0</Version>
<RootNamespace>HandlebarsDotNet.Extension.Json</RootNamespace>
Expand Down
16 changes: 12 additions & 4 deletions source/Handlebars.Extension/JsonFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ namespace HandlebarsDotNet.Extension.Json
public static class JsonFeatureExtensions
{
private static readonly JsonDocumentObjectDescriptor JsonDocumentObjectDescriptor = new JsonDocumentObjectDescriptor();
private static readonly JsonElementObjectDescriptor JsonElementObjectDescriptor = new JsonElementObjectDescriptor();

private static readonly JsonElementObjectDescriptor JsonElementObjectDescriptor = new JsonElementObjectDescriptor();

#if NET6_0_OR_GREATER
private static readonly JsonNodeObjectDescriptor JsonNodeObjectDescriptor = new JsonNodeObjectDescriptor();
#endif

/// <summary>
/// Adds <see cref="IObjectDescriptorProvider"/>s required to support <c>System.Text.Json</c>.
/// </summary>
Expand All @@ -20,8 +24,12 @@ public static HandlebarsConfiguration UseJson(this HandlebarsConfiguration confi
var providers = configuration.ObjectDescriptorProviders;

providers.Add(JsonDocumentObjectDescriptor);
providers.Add(JsonElementObjectDescriptor);

providers.Add(JsonElementObjectDescriptor);

#if NET6_0_OR_GREATER
providers.Add(JsonNodeObjectDescriptor);
#endif

return configuration;
}
}
Expand Down
Loading