|
5 | 5 | using System.Globalization; |
6 | 6 | using System.Linq; |
7 | 7 | using System.Text.Encodings.Web; |
| 8 | +using System.Text.Json; |
8 | 9 | using System.Threading.Tasks; |
9 | 10 | using TimeZoneConverter; |
10 | 11 | using Xunit; |
@@ -823,6 +824,60 @@ public async Task JsonShouldUseJsonSerializerOption() |
823 | 824 | Assert.Equal(expected, result.ToStringValue()); |
824 | 825 | } |
825 | 826 |
|
| 827 | + [Fact] |
| 828 | + public async Task JsonShouldUseJsonWriterOptionsFromTemplateOptions() |
| 829 | + { |
| 830 | + var options = new TemplateOptions |
| 831 | + { |
| 832 | + JsonWriterOptions = new JsonWriterOptions |
| 833 | + { |
| 834 | + Indented = true |
| 835 | + } |
| 836 | + }; |
| 837 | + |
| 838 | + var input = FluidValue.Create(new { name = "test", value = 123 }, options); |
| 839 | + options.MemberAccessStrategy.Register(input.ToObjectValue().GetType()); |
| 840 | + var context = new TemplateContext(options); |
| 841 | + var result = await MiscFilters.Json(input, new FilterArguments(), context); |
| 842 | + |
| 843 | + // Indented JSON should have newlines |
| 844 | + Assert.Contains("\n", result.ToStringValue()); |
| 845 | + } |
| 846 | + |
| 847 | + [Fact] |
| 848 | + public async Task JsonShouldUseJsonWriterOptionsFromTemplateContext() |
| 849 | + { |
| 850 | + var options = new TemplateOptions(); |
| 851 | + var context = new TemplateContext(options) |
| 852 | + { |
| 853 | + JsonWriterOptions = new JsonWriterOptions |
| 854 | + { |
| 855 | + Indented = true |
| 856 | + } |
| 857 | + }; |
| 858 | + |
| 859 | + var input = FluidValue.Create(new { name = "test", value = 123 }, options); |
| 860 | + options.MemberAccessStrategy.Register(input.ToObjectValue().GetType()); |
| 861 | + var result = await MiscFilters.Json(input, new FilterArguments(), context); |
| 862 | + |
| 863 | + // Indented JSON should have newlines |
| 864 | + Assert.Contains("\n", result.ToStringValue()); |
| 865 | + } |
| 866 | + |
| 867 | + [Fact] |
| 868 | + public async Task JsonShouldSerializeEnumsAsNumbers() |
| 869 | + { |
| 870 | + var options = new TemplateOptions(); |
| 871 | + options.MemberAccessStrategy.Register<TestEnum>(); |
| 872 | + |
| 873 | + var input = FluidValue.Create(TestEnum.Value2, options); |
| 874 | + var context = new TemplateContext(options); |
| 875 | + var result = await MiscFilters.Json(input, new FilterArguments(), context); |
| 876 | + |
| 877 | + // Enum should be serialized as number (1 for Value2) |
| 878 | + Assert.Equal("1", result.ToStringValue()); |
| 879 | + } |
| 880 | + |
826 | 881 | [Theory] |
827 | 882 | [InlineData("", "", "", "0")] |
828 | 883 | [InlineData(123456, "", "", "123456")] |
@@ -1029,5 +1084,12 @@ public DictionaryWithoutIndexableTestObjects(object value) : base(value) |
1029 | 1084 |
|
1030 | 1085 | } |
1031 | 1086 | } |
| 1087 | + |
| 1088 | + private enum TestEnum |
| 1089 | + { |
| 1090 | + Value1 = 0, |
| 1091 | + Value2 = 1, |
| 1092 | + Value3 = 2 |
| 1093 | + } |
1032 | 1094 | } |
1033 | 1095 | } |
0 commit comments