diff --git a/net/DevExtreme.AspNet.Data.Tests/CustomFilterCompilersTests.cs b/net/DevExtreme.AspNet.Data.Tests/CustomFilterCompilersTests.cs index a9f8c7e8..4f05e5e9 100644 --- a/net/DevExtreme.AspNet.Data.Tests/CustomFilterCompilersTests.cs +++ b/net/DevExtreme.AspNet.Data.Tests/CustomFilterCompilersTests.cs @@ -11,6 +11,9 @@ namespace DevExtreme.AspNet.Data.Tests { public class CustomFilterCompilersTests { + static readonly JsonSerializerOptions TESTS_DEFAULT_SERIALIZER_OPTIONS = new JsonSerializerOptions(JsonSerializerDefaults.Web) { + Converters = { new ListConverter() } + }; [Fact] public void OneToManyContains() { @@ -35,8 +38,7 @@ public void OneToManyContains() { var source = new[] { new Category(), new Category() }; source[0].Products.Add(new Product { Name = "Chai" }); - var deserializedList = JsonSerializer.Deserialize(@"[ ""Products"", ""Contains"", ""ch"" ]"); - var filter = Compatibility.UnwrapList(deserializedList); + var filter = JsonSerializer.Deserialize(@"[ ""Products"", ""Contains"", ""ch"" ]", TESTS_DEFAULT_SERIALIZER_OPTIONS); var loadOptions = new SampleLoadOptions { Filter = filter, diff --git a/net/DevExtreme.AspNet.Data.Tests/FilterExpressionCompilerTests.cs b/net/DevExtreme.AspNet.Data.Tests/FilterExpressionCompilerTests.cs index 0bf12fe3..9924bafb 100644 --- a/net/DevExtreme.AspNet.Data.Tests/FilterExpressionCompilerTests.cs +++ b/net/DevExtreme.AspNet.Data.Tests/FilterExpressionCompilerTests.cs @@ -9,6 +9,9 @@ namespace DevExtreme.AspNet.Data.Tests { public class FilterExpressionCompilerTests { + static readonly JsonSerializerOptions TESTS_DEFAULT_SERIALIZER_OPTIONS = new JsonSerializerOptions(JsonSerializerDefaults.Web) { + Converters = { new ListConverter() } + }; class DataItem1 { public int IntProp { get; set; } @@ -143,8 +146,7 @@ public void Not() { [Fact] public void IsUnaryWithJsonCriteria() { - var deserializedList = JsonSerializer.Deserialize("[\"!\", []]"); - var crit = Compatibility.UnwrapList(deserializedList); + var crit = JsonSerializer.Deserialize("[\"!\", []]", TESTS_DEFAULT_SERIALIZER_OPTIONS); var compiler = new FilterExpressionCompiler(typeof(object), false); Assert.True(compiler.IsUnary(crit)); } @@ -241,8 +243,7 @@ public void T105740() { [Fact] public void JsonObjects() { - var deserializedList = JsonSerializer.Deserialize(@"[ [ ""StringProp"", ""abc"" ], [ ""NullableProp"", null ] ]"); - var crit = Compatibility.UnwrapList(deserializedList); + var crit = JsonSerializer.Deserialize(@"[ [ ""StringProp"", ""abc"" ], [ ""NullableProp"", null ] ]", TESTS_DEFAULT_SERIALIZER_OPTIONS); var expr = Compile(crit); Assert.Equal(@"((obj.StringProp == ""abc"") AndAlso (obj.NullableProp == null))", expr.Body.ToString()); } diff --git a/net/DevExtreme.AspNet.Data/Compatibility.cs b/net/DevExtreme.AspNet.Data/Compatibility.cs index 0040b3d5..c8ca7a20 100644 --- a/net/DevExtreme.AspNet.Data/Compatibility.cs +++ b/net/DevExtreme.AspNet.Data/Compatibility.cs @@ -61,10 +61,16 @@ static object GetNumber(ref Utf8JsonReader reader) { throw new NotImplementedException(); } - public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options) { - throw new NotImplementedException(); + public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options) => throw new NotImplementedException(); + } + + class ListConverter : JsonConverter { + public override IList Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { + var deserializedList = JsonSerializer.Deserialize(ref reader); + return Compatibility.UnwrapList(deserializedList); } + public override void Write(Utf8JsonWriter writer, IList value, JsonSerializerOptions options) => throw new NotImplementedException(); } } diff --git a/net/DevExtreme.AspNet.Data/DataSourceLoadOptionsBase.cs b/net/DevExtreme.AspNet.Data/DataSourceLoadOptionsBase.cs index 4d6da287..8275faa6 100644 --- a/net/DevExtreme.AspNet.Data/DataSourceLoadOptionsBase.cs +++ b/net/DevExtreme.AspNet.Data/DataSourceLoadOptionsBase.cs @@ -1,8 +1,8 @@ using System; using System.Collections; using System.ComponentModel; -using System.Linq; using System.Linq.Expressions; +using System.Text.Json.Serialization; namespace DevExtreme.AspNet.Data { @@ -56,6 +56,7 @@ public class DataSourceLoadOptionsBase { /// /// A filter expression. /// + [JsonConverter(typeof(ListConverter))] public IList Filter { get; set; } /// diff --git a/net/DevExtreme.AspNet.Data/Helpers/DataSourceLoadOptionsParser.cs b/net/DevExtreme.AspNet.Data/Helpers/DataSourceLoadOptionsParser.cs index 8dbd5554..85403e0f 100644 --- a/net/DevExtreme.AspNet.Data/Helpers/DataSourceLoadOptionsParser.cs +++ b/net/DevExtreme.AspNet.Data/Helpers/DataSourceLoadOptionsParser.cs @@ -8,7 +8,9 @@ namespace DevExtreme.AspNet.Data.Helpers { /// A parser for the data processing settings. /// public static class DataSourceLoadOptionsParser { - static readonly JsonSerializerOptions DEFAULT_SERIALIZER_OPTIONS = new JsonSerializerOptions(JsonSerializerDefaults.Web); + static readonly JsonSerializerOptions DEFAULT_SERIALIZER_OPTIONS = new JsonSerializerOptions(JsonSerializerDefaults.Web) { + Converters = { new ListConverter() } + }; public const string KEY_REQUIRE_TOTAL_COUNT = "requireTotalCount", @@ -62,10 +64,8 @@ public static void Parse(DataSourceLoadOptionsBase loadOptions, Func(group, DEFAULT_SERIALIZER_OPTIONS); - if(!String.IsNullOrEmpty(filter)) { - var deserializedList = JsonSerializer.Deserialize(filter); - loadOptions.Filter = Compatibility.UnwrapList(deserializedList); - } + if(!String.IsNullOrEmpty(filter)) + loadOptions.Filter = JsonSerializer.Deserialize(filter, DEFAULT_SERIALIZER_OPTIONS); if(!String.IsNullOrEmpty(totalSummary)) loadOptions.TotalSummary = JsonSerializer.Deserialize(totalSummary, DEFAULT_SERIALIZER_OPTIONS);