Skip to content

Commit f72062e

Browse files
Extract unwrapping DataSourceLoadOptionsBase IList Filter to Converter (#617)
1 parent c4b2709 commit f72062e

File tree

5 files changed

+24
-14
lines changed

5 files changed

+24
-14
lines changed

net/DevExtreme.AspNet.Data.Tests/CustomFilterCompilersTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
namespace DevExtreme.AspNet.Data.Tests {
1212

1313
public class CustomFilterCompilersTests {
14+
static readonly JsonSerializerOptions TESTS_DEFAULT_SERIALIZER_OPTIONS = new JsonSerializerOptions(JsonSerializerDefaults.Web) {
15+
Converters = { new ListConverter() }
16+
};
1417

1518
[Fact]
1619
public void OneToManyContains() {
@@ -35,8 +38,7 @@ public void OneToManyContains() {
3538
var source = new[] { new Category(), new Category() };
3639
source[0].Products.Add(new Product { Name = "Chai" });
3740

38-
var deserializedList = JsonSerializer.Deserialize<IList>(@"[ ""Products"", ""Contains"", ""ch"" ]");
39-
var filter = Compatibility.UnwrapList(deserializedList);
41+
var filter = JsonSerializer.Deserialize<IList>(@"[ ""Products"", ""Contains"", ""ch"" ]", TESTS_DEFAULT_SERIALIZER_OPTIONS);
4042

4143
var loadOptions = new SampleLoadOptions {
4244
Filter = filter,

net/DevExtreme.AspNet.Data.Tests/FilterExpressionCompilerTests.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
namespace DevExtreme.AspNet.Data.Tests {
1010

1111
public class FilterExpressionCompilerTests {
12+
static readonly JsonSerializerOptions TESTS_DEFAULT_SERIALIZER_OPTIONS = new JsonSerializerOptions(JsonSerializerDefaults.Web) {
13+
Converters = { new ListConverter() }
14+
};
1215

1316
class DataItem1 {
1417
public int IntProp { get; set; }
@@ -143,8 +146,7 @@ public void Not() {
143146

144147
[Fact]
145148
public void IsUnaryWithJsonCriteria() {
146-
var deserializedList = JsonSerializer.Deserialize<IList>("[\"!\", []]");
147-
var crit = Compatibility.UnwrapList(deserializedList);
149+
var crit = JsonSerializer.Deserialize<IList>("[\"!\", []]", TESTS_DEFAULT_SERIALIZER_OPTIONS);
148150
var compiler = new FilterExpressionCompiler(typeof(object), false);
149151
Assert.True(compiler.IsUnary(crit));
150152
}
@@ -241,8 +243,7 @@ public void T105740() {
241243

242244
[Fact]
243245
public void JsonObjects() {
244-
var deserializedList = JsonSerializer.Deserialize<IList>(@"[ [ ""StringProp"", ""abc"" ], [ ""NullableProp"", null ] ]");
245-
var crit = Compatibility.UnwrapList(deserializedList);
246+
var crit = JsonSerializer.Deserialize<IList>(@"[ [ ""StringProp"", ""abc"" ], [ ""NullableProp"", null ] ]", TESTS_DEFAULT_SERIALIZER_OPTIONS);
246247
var expr = Compile<DataItem1>(crit);
247248
Assert.Equal(@"((obj.StringProp == ""abc"") AndAlso (obj.NullableProp == null))", expr.Body.ToString());
248249
}

net/DevExtreme.AspNet.Data/Compatibility.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,16 @@ static object GetNumber(ref Utf8JsonReader reader) {
6161
throw new NotImplementedException();
6262
}
6363

64-
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options) {
65-
throw new NotImplementedException();
64+
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options) => throw new NotImplementedException();
65+
}
66+
67+
class ListConverter : JsonConverter<IList> {
68+
public override IList Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
69+
var deserializedList = JsonSerializer.Deserialize<IList>(ref reader);
70+
return Compatibility.UnwrapList(deserializedList);
6671
}
6772

73+
public override void Write(Utf8JsonWriter writer, IList value, JsonSerializerOptions options) => throw new NotImplementedException();
6874
}
6975

7076
}

net/DevExtreme.AspNet.Data/DataSourceLoadOptionsBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using System;
22
using System.Collections;
33
using System.ComponentModel;
4-
using System.Linq;
54
using System.Linq.Expressions;
5+
using System.Text.Json.Serialization;
66

77
namespace DevExtreme.AspNet.Data {
88

@@ -56,6 +56,7 @@ public class DataSourceLoadOptionsBase {
5656
/// <summary>
5757
/// A filter expression.
5858
/// </summary>
59+
[JsonConverter(typeof(ListConverter))]
5960
public IList Filter { get; set; }
6061

6162
/// <summary>

net/DevExtreme.AspNet.Data/Helpers/DataSourceLoadOptionsParser.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ namespace DevExtreme.AspNet.Data.Helpers {
88
/// A parser for the data processing settings.
99
/// </summary>
1010
public static class DataSourceLoadOptionsParser {
11-
static readonly JsonSerializerOptions DEFAULT_SERIALIZER_OPTIONS = new JsonSerializerOptions(JsonSerializerDefaults.Web);
11+
static readonly JsonSerializerOptions DEFAULT_SERIALIZER_OPTIONS = new JsonSerializerOptions(JsonSerializerDefaults.Web) {
12+
Converters = { new ListConverter() }
13+
};
1214

1315
public const string
1416
KEY_REQUIRE_TOTAL_COUNT = "requireTotalCount",
@@ -62,10 +64,8 @@ public static void Parse(DataSourceLoadOptionsBase loadOptions, Func<string, str
6264
if(!String.IsNullOrEmpty(group))
6365
loadOptions.Group = JsonSerializer.Deserialize<GroupingInfo[]>(group, DEFAULT_SERIALIZER_OPTIONS);
6466

65-
if(!String.IsNullOrEmpty(filter)) {
66-
var deserializedList = JsonSerializer.Deserialize<IList>(filter);
67-
loadOptions.Filter = Compatibility.UnwrapList(deserializedList);
68-
}
67+
if(!String.IsNullOrEmpty(filter))
68+
loadOptions.Filter = JsonSerializer.Deserialize<IList>(filter, DEFAULT_SERIALIZER_OPTIONS);
6969

7070
if(!String.IsNullOrEmpty(totalSummary))
7171
loadOptions.TotalSummary = JsonSerializer.Deserialize<SummaryInfo[]>(totalSummary, DEFAULT_SERIALIZER_OPTIONS);

0 commit comments

Comments
 (0)