From fd9a72a6577c178a387b60813bea20798ad8ec38 Mon Sep 17 00:00:00 2001 From: Rasmus Melchior Jacobsen Date: Tue, 15 Feb 2022 14:38:18 +0100 Subject: [PATCH 1/4] Add .NET 6 support --- .../Handlebars.Extension.Test.csproj | 4 ++-- source/Handlebars.Extension/Handlebars.Extension.csproj | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/source/Handlebars.Extension.Test/Handlebars.Extension.Test.csproj b/source/Handlebars.Extension.Test/Handlebars.Extension.Test.csproj index d885830..c29e83d 100644 --- a/source/Handlebars.Extension.Test/Handlebars.Extension.Test.csproj +++ b/source/Handlebars.Extension.Test/Handlebars.Extension.Test.csproj @@ -1,7 +1,7 @@ - netcoreapp2.1;netcoreapp3.1 + netcoreapp2.1;netcoreapp3.1;net6.0 $(TargetFrameworks);net472 1956A22F-7B26-4747-8125-7EAC0B94665D HandlebarsDotNet.Extension.Test @@ -53,7 +53,7 @@ - + diff --git a/source/Handlebars.Extension/Handlebars.Extension.csproj b/source/Handlebars.Extension/Handlebars.Extension.csproj index 2a86a4f..55a9487 100644 --- a/source/Handlebars.Extension/Handlebars.Extension.csproj +++ b/source/Handlebars.Extension/Handlebars.Extension.csproj @@ -3,7 +3,7 @@ HandlebarsDotNet.Extension.Json 25080858-B620-4985-8AEF-E135324081B3 - netstandard2.0;netstandard2.1 + netstandard2.0;netstandard2.1;net6.0 $(TargetFrameworks);net472 1.0.0 HandlebarsDotNet.Extension.Json From b1f159bfd7b034b65d3d5729317b13c584a18bcf Mon Sep 17 00:00:00 2001 From: Rasmus Melchior Jacobsen Date: Tue, 15 Feb 2022 14:39:32 +0100 Subject: [PATCH 2/4] Add JsonNode support --- .../CountMemberAliasProvider.JsonNode.cs | 31 ++++ .../CountMemberAliasProvider.cs | 10 +- source/Handlebars.Extension/JsonFeature.cs | 16 +- .../Handlebars.Extension/JsonNodeIterator.cs | 172 ++++++++++++++++++ .../JsonNodeMemberAccessor.cs | 47 +++++ .../JsonNodeObjectDescriptor.cs | 44 +++++ source/Handlebars.Extension/Utils.JsonNode.cs | 48 +++++ source/Handlebars.Extension/Utils.cs | 2 +- 8 files changed, 360 insertions(+), 10 deletions(-) create mode 100644 source/Handlebars.Extension/CountMemberAliasProvider.JsonNode.cs create mode 100644 source/Handlebars.Extension/JsonNodeIterator.cs create mode 100644 source/Handlebars.Extension/JsonNodeMemberAccessor.cs create mode 100644 source/Handlebars.Extension/JsonNodeObjectDescriptor.cs create mode 100644 source/Handlebars.Extension/Utils.JsonNode.cs diff --git a/source/Handlebars.Extension/CountMemberAliasProvider.JsonNode.cs b/source/Handlebars.Extension/CountMemberAliasProvider.JsonNode.cs new file mode 100644 index 0000000..2b68963 --- /dev/null +++ b/source/Handlebars.Extension/CountMemberAliasProvider.JsonNode.cs @@ -0,0 +1,31 @@ +#if NET6_0_OR_GREATER + +using System; +using System.Text.Json.Nodes; +using HandlebarsDotNet.PathStructure; + +namespace HandlebarsDotNet.Extension.Json +{ + public partial class CountMemberAliasProvider : IMemberAliasProvider + { + 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 \ No newline at end of file diff --git a/source/Handlebars.Extension/CountMemberAliasProvider.cs b/source/Handlebars.Extension/CountMemberAliasProvider.cs index 66b44b7..c91f52a 100644 --- a/source/Handlebars.Extension/CountMemberAliasProvider.cs +++ b/source/Handlebars.Extension/CountMemberAliasProvider.cs @@ -4,7 +4,7 @@ namespace HandlebarsDotNet.Extension.Json { - public class CountMemberAliasProvider : IMemberAliasProvider + public partial class CountMemberAliasProvider : IMemberAliasProvider { public bool TryGetMemberByAlias(JsonElement instance, Type targetType, ChainSegment memberAlias, out object? value) { @@ -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); } } } \ No newline at end of file diff --git a/source/Handlebars.Extension/JsonFeature.cs b/source/Handlebars.Extension/JsonFeature.cs index a862c6e..4c571e4 100644 --- a/source/Handlebars.Extension/JsonFeature.cs +++ b/source/Handlebars.Extension/JsonFeature.cs @@ -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 + /// /// Adds s required to support System.Text.Json. /// @@ -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; } } diff --git a/source/Handlebars.Extension/JsonNodeIterator.cs b/source/Handlebars.Extension/JsonNodeIterator.cs new file mode 100644 index 0000000..61ff790 --- /dev/null +++ b/source/Handlebars.Extension/JsonNodeIterator.cs @@ -0,0 +1,172 @@ +#if NET6_0_OR_GREATER + +using System; +using System.Collections.Generic; +using System.Runtime.CompilerServices; +using System.Text.Json.Nodes; +using HandlebarsDotNet.Collections; +using HandlebarsDotNet.Compiler; +using HandlebarsDotNet.Iterators; +using HandlebarsDotNet.PathStructure; +using HandlebarsDotNet.Runtime; +using HandlebarsDotNet.ValueProviders; + +namespace HandlebarsDotNet.Extension.Json +{ + public class JsonNodeIterator : IIterator + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Iterate( + in EncodedTextWriter writer, + BindingContext context, + ChainSegment[] blockParamsVariables, + object input, + TemplateDelegate template, + TemplateDelegate ifEmpty + ) + { + Iterate(writer, context, blockParamsVariables, (JsonNode)input, template, ifEmpty); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void Iterate( + in EncodedTextWriter writer, + BindingContext context, + ChainSegment[] blockParamsVariables, + JsonNode input, + TemplateDelegate template, + TemplateDelegate ifEmpty + ) + { + switch (input) + { + case JsonObject jsonObject: + IterateObject(writer, context, blockParamsVariables, jsonObject, template, ifEmpty); + break; + case JsonArray jsonArray: + IterateArray(writer, context, blockParamsVariables, jsonArray, template, ifEmpty); + break; + + default: + Throw.ArgumentOutOfRangeException(); + break; + } + } + + private static void IterateObject( + in EncodedTextWriter writer, + BindingContext context, + ChainSegment[] blockParamsVariables, + JsonObject target, + TemplateDelegate template, + TemplateDelegate ifEmpty + ) + { + using var innerContext = context.CreateFrame(); + var iterator = new ObjectIteratorValues(innerContext); + var blockParamsValues = new BlockParamsValues(innerContext, blockParamsVariables); + + blockParamsValues.CreateProperty(0, out var _0); + blockParamsValues.CreateProperty(1, out var _1); + + var enumerator = ExtendedEnumerator>.Create(target.GetEnumerator()); + + iterator.First = BoxedValues.True; + iterator.Last = BoxedValues.False; + + int index = 0; + while (enumerator.MoveNext()) + { + var current = enumerator.Current; + + var currentValue = current.Value; + iterator.Key = currentValue.Key; + + if (index == 1) iterator.First = BoxedValues.False; + if (current.IsLast) iterator.Last = BoxedValues.True; + + iterator.Index = BoxedValues.Int(index); + + object? resolvedValue = currentValue.Value; + + blockParamsValues[_0] = resolvedValue; + blockParamsValues[_1] = currentValue.Key; + + iterator.Value = resolvedValue; + innerContext.Value = resolvedValue; + + template(writer, innerContext); + + ++index; + } + + if (index == 0) + { + innerContext.Value = context.Value; + ifEmpty(writer, innerContext); + } + } + + private static void IterateArray( + in EncodedTextWriter writer, + BindingContext context, + ChainSegment[] blockParamsVariables, + JsonArray target, + TemplateDelegate template, + TemplateDelegate ifEmpty + ) + { + using var innerContext = context.CreateFrame(); + var iterator = new IteratorValues(innerContext); + var blockParamsValues = new BlockParamsValues(innerContext, blockParamsVariables); + + blockParamsValues.CreateProperty(0, out var _0); + blockParamsValues.CreateProperty(1, out var _1); + + iterator.First = BoxedValues.True; + iterator.Last = BoxedValues.False; + + var count = target.Count; + var enumerator = target.GetEnumerator(); + + var index = 0; + var lastIndex = count - 1; + while (enumerator.MoveNext()) + { + var value = enumerator.Current; + var objectIndex = BoxedValues.Int(index); + + if (index == 1) iterator.First = BoxedValues.False; + if (index == lastIndex) iterator.Last = BoxedValues.True; + + iterator.Index = objectIndex; + + object? resolvedValue = value; + + blockParamsValues[_0] = resolvedValue; + blockParamsValues[_1] = objectIndex; + + iterator.Value = resolvedValue; + innerContext.Value = resolvedValue; + + template(writer, innerContext); + + ++index; + } + + if (index == 0) + { + innerContext.Value = context.Value; + ifEmpty(writer, innerContext); + } + } + + private static class Throw + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ArgumentOutOfRangeException() => throw new ArgumentOutOfRangeException(); + } + } +} + +#endif \ No newline at end of file diff --git a/source/Handlebars.Extension/JsonNodeMemberAccessor.cs b/source/Handlebars.Extension/JsonNodeMemberAccessor.cs new file mode 100644 index 0000000..cc7c881 --- /dev/null +++ b/source/Handlebars.Extension/JsonNodeMemberAccessor.cs @@ -0,0 +1,47 @@ +#if NET6_0_OR_GREATER + +using System.Text.Json.Nodes; +using HandlebarsDotNet.MemberAccessors; +using HandlebarsDotNet.PathStructure; + +namespace HandlebarsDotNet.Extension.Json +{ + internal class JsonNodeMemberAccessor : IMemberAccessor + { + private readonly CountMemberAliasProvider _aliasProvider = new CountMemberAliasProvider(); + + public bool TryGetValue(object instance, ChainSegment memberName, out object? value) + { + var element = (JsonNode)instance; + + if (element is JsonObject jsonObject && jsonObject.TryGetPropertyValue(memberName.TrimmedValue, out var property)) + { + value = Utils.ExtractProperty(property); + return true; + } + + if (element is JsonArray jsonArray && int.TryParse(memberName, out var index)) + { + if (index >= jsonArray.Count) + { + value = null; + return false; + } + + var indexedElement = jsonArray[index]; + value = Utils.ExtractProperty(indexedElement); + return true; + } + + if (_aliasProvider.TryGetMemberByAlias(element, typeof(JsonNode), memberName, out value)) + { + return true; + } + + value = null; + return false; + } + } +} + +#endif \ No newline at end of file diff --git a/source/Handlebars.Extension/JsonNodeObjectDescriptor.cs b/source/Handlebars.Extension/JsonNodeObjectDescriptor.cs new file mode 100644 index 0000000..9d66379 --- /dev/null +++ b/source/Handlebars.Extension/JsonNodeObjectDescriptor.cs @@ -0,0 +1,44 @@ +#if NET6_0_OR_GREATER + +using System; +using System.Collections; +using System.Text.Json.Nodes; +using HandlebarsDotNet.ObjectDescriptors; + +namespace HandlebarsDotNet.Extension.Json +{ + internal class JsonNodeObjectDescriptor : IObjectDescriptorProvider + { + private static readonly Type Type = typeof(JsonNode); + + private readonly ObjectDescriptor _descriptor = new ObjectDescriptor( + Type, + JsonDocumentMemberAccessor, + (descriptor, instance) => GetEnumerator(instance), + self => new JsonNodeIterator() + ); + + private static readonly JsonNodeMemberAccessor JsonDocumentMemberAccessor = new JsonNodeMemberAccessor(); + + public bool TryGetDescriptor(Type type, out ObjectDescriptor value) + { + if (!Type.IsAssignableFrom(type)) + { + value = ObjectDescriptor.Empty; + return false; + } + + value = _descriptor; + return true; + } + + private static IEnumerable GetEnumerator(object instance) + { + var jsonNode = (JsonNode)instance; + + return Utils.GetEnumerator(jsonNode); + } + } +} + +#endif \ No newline at end of file diff --git a/source/Handlebars.Extension/Utils.JsonNode.cs b/source/Handlebars.Extension/Utils.JsonNode.cs new file mode 100644 index 0000000..d4c27cf --- /dev/null +++ b/source/Handlebars.Extension/Utils.JsonNode.cs @@ -0,0 +1,48 @@ +#if NET6_0_OR_GREATER + +using System.Collections; +using System.Collections.Generic; +using System.Text.Json; +using System.Text.Json.Nodes; + +namespace HandlebarsDotNet.Extension.Json +{ + internal static partial class Utils + { + public static IEnumerable GetEnumerator(JsonNode jsonNode) + { + return jsonNode switch + { + JsonObject jsonObject => EnumerateObject(jsonObject), + JsonArray _ => ArrayProperties, + _ => Throw.ArgumentOutOfRangeException() + }; + + static IEnumerable EnumerateObject(JsonObject jsonObject) + { + foreach (var property in jsonObject) + { + yield return property.Key; + } + } + } + + public static object? ExtractProperty(JsonNode? property) + { + if (property == null) + { + return null; + } + + var type = property.GetType(); + if (type == typeof(JsonObject) || type == typeof(JsonArray)) + { + return property; + } + + return ExtractProperty(property.GetValue()); + } + } +} + +#endif \ No newline at end of file diff --git a/source/Handlebars.Extension/Utils.cs b/source/Handlebars.Extension/Utils.cs index 230fb09..e84a7e0 100644 --- a/source/Handlebars.Extension/Utils.cs +++ b/source/Handlebars.Extension/Utils.cs @@ -7,7 +7,7 @@ namespace HandlebarsDotNet.Extension.Json { - internal static class Utils + internal static partial class Utils { private static readonly string[] ArrayProperties = { "length" }; From 9f27cb9f61060ae03da052a958506bb3875a3784 Mon Sep 17 00:00:00 2001 From: Rasmus Melchior Jacobsen Date: Tue, 15 Feb 2022 14:40:02 +0100 Subject: [PATCH 3/4] Align tests to test for both JsonDocument and JsonNode --- source/Handlebars.Extension.Test/JsonTests.cs | 77 +++++++++++-------- 1 file changed, 43 insertions(+), 34 deletions(-) diff --git a/source/Handlebars.Extension.Test/JsonTests.cs b/source/Handlebars.Extension.Test/JsonTests.cs index 77408c0..28af8ca 100644 --- a/source/Handlebars.Extension.Test/JsonTests.cs +++ b/source/Handlebars.Extension.Test/JsonTests.cs @@ -13,14 +13,19 @@ namespace HandlebarsDotNet.Extension.Test { public class JsonTests { + public delegate object JsonModelFactory(string json); + public class EnvGenerator : IEnumerable { - private readonly List _data = new List + 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 }; - public IEnumerator GetEnumerator() => _data.Select(o => new object[] { o }).GetEnumerator(); + public IEnumerator GetEnumerator() => _data.Select(item => new object[] { item.Item1, item.Item2 }).GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } @@ -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}}"; @@ -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}}"; @@ -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}}"; @@ -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}}"; @@ -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}}"; @@ -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}}"; @@ -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}}"; @@ -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}}"; @@ -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}}"; @@ -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}}"; @@ -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}}"; @@ -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}}"; @@ -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()); @@ -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}}"; @@ -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}} @@ -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 From 17fc293c4b1a41774deff43f6c3cd0e9e2c435a7 Mon Sep 17 00:00:00 2001 From: James Thompson Date: Tue, 9 Apr 2024 07:08:53 +1000 Subject: [PATCH 4/4] Improve none net 6 behavior --- .../Handlebars.Extension.Test.csproj | 17 ++- source/Handlebars.Extension.Test/JsonTests.cs | 12 +-- source/Handlebars.Extension.sln | 40 +++---- .../CountMemberAliasProvider.JsonNode.cs | 9 +- .../Handlebars.Extension.csproj | 12 +-- source/Handlebars.Extension/JsonFeature.cs | 24 ++--- .../Handlebars.Extension/JsonNodeIterator.cs | 100 +++++++++--------- .../JsonNodeMemberAccessor.cs | 4 - .../JsonNodeObjectDescriptor.cs | 24 ++--- source/Handlebars.Extension/Utils.JsonNode.cs | 34 +++--- 10 files changed, 125 insertions(+), 151 deletions(-) diff --git a/source/Handlebars.Extension.Test/Handlebars.Extension.Test.csproj b/source/Handlebars.Extension.Test/Handlebars.Extension.Test.csproj index c29e83d..11d6390 100644 --- a/source/Handlebars.Extension.Test/Handlebars.Extension.Test.csproj +++ b/source/Handlebars.Extension.Test/Handlebars.Extension.Test.csproj @@ -1,7 +1,7 @@ - netcoreapp2.1;netcoreapp3.1;net6.0 + netcoreapp3.1;net6.0 $(TargetFrameworks);net472 1956A22F-7B26-4747-8125-7EAC0B94665D HandlebarsDotNet.Extension.Test @@ -10,7 +10,7 @@ false true - + 0618;1701 @@ -27,15 +27,15 @@ $(DefineConstants);netFramework - + $(DefineConstants);netcoreapp;netstandard - + $(DefineConstants);netcoreapp;netstandard - + all @@ -45,23 +45,22 @@ - + - - + - + diff --git a/source/Handlebars.Extension.Test/JsonTests.cs b/source/Handlebars.Extension.Test/JsonTests.cs index 28af8ca..e7b5a4f 100644 --- a/source/Handlebars.Extension.Test/JsonTests.cs +++ b/source/Handlebars.Extension.Test/JsonTests.cs @@ -19,10 +19,8 @@ public class EnvGenerator : IEnumerable { private readonly List<(IHandlebars, JsonModelFactory)> _data = new List<(IHandlebars, JsonModelFactory)> { - (Handlebars.Create(new HandlebarsConfiguration().UseJson()), json => JsonDocument.Parse(json)), -#if NET6_0_OR_GREATER + (Handlebars.Create(new HandlebarsConfiguration().UseJson()), json => JsonDocument.Parse(json)), (Handlebars.Create(new HandlebarsConfiguration().UseJson()), json => System.Text.Json.Nodes.JsonNode.Parse(json)), -#endif }; public IEnumerator GetEnumerator() => _data.Select(item => new object[] { item.Item1, item.Item2 }).GetEnumerator(); @@ -208,7 +206,7 @@ public void ArrayListProperties(IHandlebars handlebars, JsonModelFactory jsonMod [Theory] [ClassData(typeof(EnvGenerator))] - public void ObjectIterator(IHandlebars handlebars, JsonModelFactory jsonModelFactory) + public void ObjectIterator(IHandlebars handlebars, JsonModelFactory jsonModelFactory) { var model = jsonModelFactory("{\"Key1\": \"Val1\", \"Key2\": \"Val2\"}"); @@ -223,7 +221,7 @@ public void ObjectIterator(IHandlebars handlebars, JsonModelFactory jsonModelFac [Theory] [ClassData(typeof(EnvGenerator))] - public void ObjectIteratorProperties(IHandlebars handlebars, JsonModelFactory jsonModelFactory) + public void ObjectIteratorProperties(IHandlebars handlebars, JsonModelFactory jsonModelFactory) { var model = jsonModelFactory("{\"Key1\": \"Val1\", \"Key2\": \"Val2\"}"); @@ -238,7 +236,7 @@ public void ObjectIteratorProperties(IHandlebars handlebars, JsonModelFactory js [Theory] [ClassData(typeof(EnvGenerator))] - public void ObjectListProperties(IHandlebars handlebars, JsonModelFactory jsonModelFactory) + public void ObjectListProperties(IHandlebars handlebars, JsonModelFactory jsonModelFactory) { var model = jsonModelFactory("{\"Key1\": \"Val1\", \"Key2\": \"Val2\"}"); @@ -254,7 +252,7 @@ public void ObjectListProperties(IHandlebars handlebars, JsonModelFactory jsonMo [Theory] [ClassData(typeof(EnvGenerator))] - public void ObjectIteratorPropertiesWithLast(IHandlebars handlebars, JsonModelFactory jsonModelFactory) + public void ObjectIteratorPropertiesWithLast(IHandlebars handlebars, JsonModelFactory jsonModelFactory) { var model = jsonModelFactory("{\"Key1\": \"Val1\", \"Key2\": \"Val2\"}"); diff --git a/source/Handlebars.Extension.sln b/source/Handlebars.Extension.sln index 48eaecf..8ac4c3b 100644 --- a/source/Handlebars.Extension.sln +++ b/source/Handlebars.Extension.sln @@ -1,19 +1,18 @@ - Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.26403.3 +# Visual Studio Version 17 +VisualStudioVersion = 17.9.34714.143 MinimumVisualStudioVersion = 15.0.26124.0 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{E9AC0BCD-C060-4634-BBBB-636167C809B4}" ProjectSection(SolutionItems) = preProject - ..\README.md = ..\README.md Directory.Build.props = Directory.Build.props + ..\README.md = ..\README.md EndProjectSection EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Handlebars.Extension", "Handlebars.Extension\Handlebars.Extension.csproj", "{9822C7B8-7E51-42BC-9A49-72A10491B202}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Handlebars.Extension", "Handlebars.Extension\Handlebars.Extension.csproj", "{25080858-B620-4985-8AEF-E135324081B3}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Handlebars.Extension.Test", "Handlebars.Extension.Test\Handlebars.Extension.Test.csproj", "{6BA232A6-8C4D-4C7D-BD75-1844FE9774AF}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Handlebars.Extension.Test", "Handlebars.Extension.Test\Handlebars.Extension.Test.csproj", "{1956A22F-7B26-4747-8125-7EAC0B94665D}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Handlebars.Extension.Benchmark", "Handlebars.Extension.Benchmark\Handlebars.Extension.Benchmark.csproj", "{B335E9C5-7DD3-416D-89CC-8D48D89DB628}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Handlebars.Extension.Benchmark", "Handlebars.Extension.Benchmark\Handlebars.Extension.Benchmark.csproj", "{95ECC7A5-0A42-4DAF-A546-20522A3F3CF5}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -21,22 +20,25 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {9822C7B8-7E51-42BC-9A49-72A10491B202}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {9822C7B8-7E51-42BC-9A49-72A10491B202}.Debug|Any CPU.Build.0 = Debug|Any CPU - {9822C7B8-7E51-42BC-9A49-72A10491B202}.Release|Any CPU.ActiveCfg = Release|Any CPU - {9822C7B8-7E51-42BC-9A49-72A10491B202}.Release|Any CPU.Build.0 = Release|Any CPU - {6BA232A6-8C4D-4C7D-BD75-1844FE9774AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {6BA232A6-8C4D-4C7D-BD75-1844FE9774AF}.Debug|Any CPU.Build.0 = Debug|Any CPU - {6BA232A6-8C4D-4C7D-BD75-1844FE9774AF}.Release|Any CPU.ActiveCfg = Release|Any CPU - {6BA232A6-8C4D-4C7D-BD75-1844FE9774AF}.Release|Any CPU.Build.0 = Release|Any CPU - {B335E9C5-7DD3-416D-89CC-8D48D89DB628}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {B335E9C5-7DD3-416D-89CC-8D48D89DB628}.Debug|Any CPU.Build.0 = Debug|Any CPU - {B335E9C5-7DD3-416D-89CC-8D48D89DB628}.Release|Any CPU.ActiveCfg = Release|Any CPU - {B335E9C5-7DD3-416D-89CC-8D48D89DB628}.Release|Any CPU.Build.0 = Release|Any CPU + {25080858-B620-4985-8AEF-E135324081B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {25080858-B620-4985-8AEF-E135324081B3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {25080858-B620-4985-8AEF-E135324081B3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {25080858-B620-4985-8AEF-E135324081B3}.Release|Any CPU.Build.0 = Release|Any CPU + {1956A22F-7B26-4747-8125-7EAC0B94665D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1956A22F-7B26-4747-8125-7EAC0B94665D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1956A22F-7B26-4747-8125-7EAC0B94665D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1956A22F-7B26-4747-8125-7EAC0B94665D}.Release|Any CPU.Build.0 = Release|Any CPU + {95ECC7A5-0A42-4DAF-A546-20522A3F3CF5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {95ECC7A5-0A42-4DAF-A546-20522A3F3CF5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {95ECC7A5-0A42-4DAF-A546-20522A3F3CF5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {95ECC7A5-0A42-4DAF-A546-20522A3F3CF5}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {A684E42D-246D-4CF7-B79C-34C49A10B35F} + EndGlobalSection GlobalSection(MonoDevelopProperties) = preSolution Policies = $0 $0.TextStylePolicy = $1 diff --git a/source/Handlebars.Extension/CountMemberAliasProvider.JsonNode.cs b/source/Handlebars.Extension/CountMemberAliasProvider.JsonNode.cs index 2b68963..5101ed0 100644 --- a/source/Handlebars.Extension/CountMemberAliasProvider.JsonNode.cs +++ b/source/Handlebars.Extension/CountMemberAliasProvider.JsonNode.cs @@ -1,7 +1,5 @@ -#if NET6_0_OR_GREATER - using System; -using System.Text.Json.Nodes; +using System.Text.Json.Nodes; using HandlebarsDotNet.PathStructure; namespace HandlebarsDotNet.Extension.Json @@ -15,7 +13,6 @@ public bool TryGetMemberByAlias(JsonNode instance, Type targetType, ChainSegment value = null; return false; } - if (!(instance is JsonArray jsonArray)) { value = null; @@ -26,6 +23,4 @@ public bool TryGetMemberByAlias(JsonNode instance, Type targetType, ChainSegment return true; } } -} - -#endif \ No newline at end of file +} diff --git a/source/Handlebars.Extension/Handlebars.Extension.csproj b/source/Handlebars.Extension/Handlebars.Extension.csproj index 55a9487..2be0336 100644 --- a/source/Handlebars.Extension/Handlebars.Extension.csproj +++ b/source/Handlebars.Extension/Handlebars.Extension.csproj @@ -10,7 +10,7 @@ enable true - + $(DefineConstants);netstandard @@ -28,17 +28,17 @@ https://github.com/Handlebars-Net/Handlebars.Net.Extension.Json/releases/tag/$(Version) true - + - + - + - + - + \ No newline at end of file diff --git a/source/Handlebars.Extension/JsonFeature.cs b/source/Handlebars.Extension/JsonFeature.cs index 4c571e4..1fb195f 100644 --- a/source/Handlebars.Extension/JsonFeature.cs +++ b/source/Handlebars.Extension/JsonFeature.cs @@ -3,33 +3,29 @@ namespace HandlebarsDotNet.Extension.Json { /// - /// + /// /// public static class JsonFeatureExtensions { private static readonly JsonDocumentObjectDescriptor JsonDocumentObjectDescriptor = new JsonDocumentObjectDescriptor(); - private static readonly JsonElementObjectDescriptor JsonElementObjectDescriptor = new JsonElementObjectDescriptor(); - -#if NET6_0_OR_GREATER - private static readonly JsonNodeObjectDescriptor JsonNodeObjectDescriptor = new JsonNodeObjectDescriptor(); -#endif - + private static readonly JsonElementObjectDescriptor JsonElementObjectDescriptor = new JsonElementObjectDescriptor(); + + private static readonly JsonNodeObjectDescriptor JsonNodeObjectDescriptor = new JsonNodeObjectDescriptor(); + /// - /// Adds s required to support System.Text.Json. + /// Adds s required to support System.Text.Json. /// /// /// public static HandlebarsConfiguration UseJson(this HandlebarsConfiguration configuration) { var providers = configuration.ObjectDescriptorProviders; - + providers.Add(JsonDocumentObjectDescriptor); - providers.Add(JsonElementObjectDescriptor); - -#if NET6_0_OR_GREATER + providers.Add(JsonElementObjectDescriptor); + providers.Add(JsonNodeObjectDescriptor); -#endif - + return configuration; } } diff --git a/source/Handlebars.Extension/JsonNodeIterator.cs b/source/Handlebars.Extension/JsonNodeIterator.cs index 61ff790..6e45127 100644 --- a/source/Handlebars.Extension/JsonNodeIterator.cs +++ b/source/Handlebars.Extension/JsonNodeIterator.cs @@ -1,9 +1,7 @@ -#if NET6_0_OR_GREATER - using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Runtime.CompilerServices; -using System.Text.Json.Nodes; +using System.Text.Json.Nodes; using HandlebarsDotNet.Collections; using HandlebarsDotNet.Compiler; using HandlebarsDotNet.Iterators; @@ -17,24 +15,24 @@ public class JsonNodeIterator : IIterator { [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Iterate( - in EncodedTextWriter writer, - BindingContext context, - ChainSegment[] blockParamsVariables, + in EncodedTextWriter writer, + BindingContext context, + ChainSegment[] blockParamsVariables, object input, - TemplateDelegate template, + TemplateDelegate template, TemplateDelegate ifEmpty ) { Iterate(writer, context, blockParamsVariables, (JsonNode)input, template, ifEmpty); - } - + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Iterate( - in EncodedTextWriter writer, - BindingContext context, - ChainSegment[] blockParamsVariables, + in EncodedTextWriter writer, + BindingContext context, + ChainSegment[] blockParamsVariables, JsonNode input, - TemplateDelegate template, + TemplateDelegate template, TemplateDelegate ifEmpty ) { @@ -42,11 +40,11 @@ TemplateDelegate ifEmpty { case JsonObject jsonObject: IterateObject(writer, context, blockParamsVariables, jsonObject, template, ifEmpty); - break; + break; case JsonArray jsonArray: IterateArray(writer, context, blockParamsVariables, jsonArray, template, ifEmpty); - break; - + break; + default: Throw.ArgumentOutOfRangeException(); break; @@ -54,21 +52,21 @@ TemplateDelegate ifEmpty } private static void IterateObject( - in EncodedTextWriter writer, - BindingContext context, - ChainSegment[] blockParamsVariables, + in EncodedTextWriter writer, + BindingContext context, + ChainSegment[] blockParamsVariables, JsonObject target, - TemplateDelegate template, + TemplateDelegate template, TemplateDelegate ifEmpty ) { using var innerContext = context.CreateFrame(); var iterator = new ObjectIteratorValues(innerContext); - var blockParamsValues = new BlockParamsValues(innerContext, blockParamsVariables); - + var blockParamsValues = new BlockParamsValues(innerContext, blockParamsVariables); + blockParamsValues.CreateProperty(0, out var _0); - blockParamsValues.CreateProperty(1, out var _1); - + blockParamsValues.CreateProperty(1, out var _1); + var enumerator = ExtendedEnumerator>.Create(target.GetEnumerator()); iterator.First = BoxedValues.True; @@ -77,42 +75,42 @@ TemplateDelegate ifEmpty int index = 0; while (enumerator.MoveNext()) { - var current = enumerator.Current; - + var current = enumerator.Current; + var currentValue = current.Value; - iterator.Key = currentValue.Key; - + iterator.Key = currentValue.Key; + if (index == 1) iterator.First = BoxedValues.False; - if (current.IsLast) iterator.Last = BoxedValues.True; - - iterator.Index = BoxedValues.Int(index); - - object? resolvedValue = currentValue.Value; - + if (current.IsLast) iterator.Last = BoxedValues.True; + + iterator.Index = BoxedValues.Int(index); + + object? resolvedValue = currentValue.Value; + blockParamsValues[_0] = resolvedValue; - blockParamsValues[_1] = currentValue.Key; - + blockParamsValues[_1] = currentValue.Key; + iterator.Value = resolvedValue; innerContext.Value = resolvedValue; template(writer, innerContext); ++index; - } - + } + if (index == 0) { innerContext.Value = context.Value; ifEmpty(writer, innerContext); } - } - + } + private static void IterateArray( - in EncodedTextWriter writer, - BindingContext context, - ChainSegment[] blockParamsVariables, + in EncodedTextWriter writer, + BindingContext context, + ChainSegment[] blockParamsVariables, JsonArray target, - TemplateDelegate template, + TemplateDelegate template, TemplateDelegate ifEmpty ) { @@ -121,8 +119,8 @@ TemplateDelegate ifEmpty var blockParamsValues = new BlockParamsValues(innerContext, blockParamsVariables); blockParamsValues.CreateProperty(0, out var _0); - blockParamsValues.CreateProperty(1, out var _1); - + blockParamsValues.CreateProperty(1, out var _1); + iterator.First = BoxedValues.True; iterator.Last = BoxedValues.False; @@ -159,14 +157,12 @@ TemplateDelegate ifEmpty innerContext.Value = context.Value; ifEmpty(writer, innerContext); } - } - + } + private static class Throw { [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void ArgumentOutOfRangeException() => throw new ArgumentOutOfRangeException(); } } -} - -#endif \ No newline at end of file +} diff --git a/source/Handlebars.Extension/JsonNodeMemberAccessor.cs b/source/Handlebars.Extension/JsonNodeMemberAccessor.cs index cc7c881..53abe8b 100644 --- a/source/Handlebars.Extension/JsonNodeMemberAccessor.cs +++ b/source/Handlebars.Extension/JsonNodeMemberAccessor.cs @@ -1,5 +1,3 @@ -#if NET6_0_OR_GREATER - using System.Text.Json.Nodes; using HandlebarsDotNet.MemberAccessors; using HandlebarsDotNet.PathStructure; @@ -43,5 +41,3 @@ public bool TryGetValue(object instance, ChainSegment memberName, out object? va } } } - -#endif \ No newline at end of file diff --git a/source/Handlebars.Extension/JsonNodeObjectDescriptor.cs b/source/Handlebars.Extension/JsonNodeObjectDescriptor.cs index 9d66379..77cb6aa 100644 --- a/source/Handlebars.Extension/JsonNodeObjectDescriptor.cs +++ b/source/Handlebars.Extension/JsonNodeObjectDescriptor.cs @@ -1,5 +1,3 @@ -#if NET6_0_OR_GREATER - using System; using System.Collections; using System.Text.Json.Nodes; @@ -9,12 +7,12 @@ namespace HandlebarsDotNet.Extension.Json { internal class JsonNodeObjectDescriptor : IObjectDescriptorProvider { - private static readonly Type Type = typeof(JsonNode); - + private static readonly Type Type = typeof(JsonNode); + private readonly ObjectDescriptor _descriptor = new ObjectDescriptor( - Type, - JsonDocumentMemberAccessor, - (descriptor, instance) => GetEnumerator(instance), + Type, + JsonDocumentMemberAccessor, + (descriptor, instance) => GetEnumerator(instance), self => new JsonNodeIterator() ); @@ -30,15 +28,13 @@ public bool TryGetDescriptor(Type type, out ObjectDescriptor value) value = _descriptor; return true; - } - + } + private static IEnumerable GetEnumerator(object instance) { - var jsonNode = (JsonNode)instance; - + var jsonNode = (JsonNode)instance; + return Utils.GetEnumerator(jsonNode); } } -} - -#endif \ No newline at end of file +} diff --git a/source/Handlebars.Extension/Utils.JsonNode.cs b/source/Handlebars.Extension/Utils.JsonNode.cs index d4c27cf..246263e 100644 --- a/source/Handlebars.Extension/Utils.JsonNode.cs +++ b/source/Handlebars.Extension/Utils.JsonNode.cs @@ -1,5 +1,3 @@ -#if NET6_0_OR_GREATER - using System.Collections; using System.Collections.Generic; using System.Text.Json; @@ -27,22 +25,20 @@ static IEnumerable EnumerateObject(JsonObject jsonObject) } } - public static object? ExtractProperty(JsonNode? property) - { - if (property == null) - { - return null; - } - - var type = property.GetType(); - if (type == typeof(JsonObject) || type == typeof(JsonArray)) - { - return property; - } - - return ExtractProperty(property.GetValue()); + public static object? ExtractProperty(JsonNode? property) + { + if (property == null) + { + return null; + } + + var type = property.GetType(); + if (type == typeof(JsonObject) || type == typeof(JsonArray)) + { + return property; + } + + return ExtractProperty(property.GetValue()); } } -} - -#endif \ No newline at end of file +}