Skip to content

Commit

Permalink
Newtonsoft.Json -> System.Text.Json (#605)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpreyskurantov authored Mar 7, 2024
1 parent 9068bca commit 82aba9a
Show file tree
Hide file tree
Showing 22 changed files with 210 additions and 91 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net452;net6.0</TargetFrameworks>
<TargetFrameworks>net461;net6.0</TargetFrameworks>
<RootNamespace>DevExtreme.AspNet.Data.Tests</RootNamespace>
<IsTestProject>False</IsTestProject>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
<TargetFramework>net472</TargetFramework>
<RootNamespace>DevExtreme.AspNet.Data.Tests</RootNamespace>
<AssemblyName>DevExtreme.AspNet.Data.Tests</AssemblyName>
<DefineConstants>NET4</DefineConstants>
<DefineConstants>NET4;NEWTONSOFT_TESTS</DefineConstants>
</PropertyGroup>

<ItemGroup>
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Web.Extensions" />

<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.3.3" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using DevExtreme.AspNet.Data.Helpers;
using Newtonsoft.Json;

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text.Json;
using Xunit;

namespace DevExtreme.AspNet.Data.Tests {
Expand Down Expand Up @@ -34,7 +35,8 @@ public void OneToManyContains() {
var source = new[] { new Category(), new Category() };
source[0].Products.Add(new Product { Name = "Chai" });

var filter = JsonConvert.DeserializeObject<IList>(@"[ ""Products"", ""Contains"", ""ch"" ]");
var deserializedList = JsonSerializer.Deserialize<IList>(@"[ ""Products"", ""Contains"", ""ch"" ]");
var filter = Compatibility.UnwrapList(deserializedList);

var loadOptions = new SampleLoadOptions {
Filter = filter,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using DevExtreme.AspNet.Data.Helpers;
using System;

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;

namespace DevExtreme.AspNet.Data.Tests {
Expand Down Expand Up @@ -60,6 +59,21 @@ public void MustNotParseDates() {
Assert.IsType<string>(opts.Filter[1]);
}

[Fact]
public void MustParseNumericAsString() {
var opts = new SampleLoadOptions();

DataSourceLoadOptionsParser.Parse(opts, key => {
if(key == DataSourceLoadOptionsParser.KEY_GROUP)
return @"[{""selector"":""freight"",""groupInterval"":100,""isExpanded"":false}]";
return null;
});

Assert.Equal("freight", opts.Group[0].Selector);
Assert.Equal("100", opts.Group[0].GroupInterval);
Assert.False(opts.Group[0].IsExpanded);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<DefineConstants>NEWTONSOFT_TESTS</DefineConstants>
</PropertyGroup>

<ItemGroup>
Expand Down
15 changes: 12 additions & 3 deletions net/DevExtreme.AspNet.Data.Tests/DynamicBindingTests.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
using DevExtreme.AspNet.Data.ResponseModel;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

using System;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using Xunit;

#if NEWTONSOFT_TESTS
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
#endif

namespace DevExtreme.AspNet.Data.Tests {

public class DynamicBindingTests {
Expand Down Expand Up @@ -70,6 +73,7 @@ public void Filter() {
Assert.Equal(2d, expandoResult[0][P1]);
}

#if NEWTONSOFT_TESTS
[Fact]
public void Filter_JValueNull() {
var result = ToDictArray(DataSourceLoader.Load(
Expand All @@ -83,6 +87,7 @@ public void Filter_JValueNull() {

Assert.Single(result);
}
#endif

[Fact]
public void Filter_Null() {
Expand Down Expand Up @@ -147,6 +152,7 @@ public void Grouping() {
Assert.Equal(4m, expandoResult[1].summary[0]);
}

#if NEWTONSOFT_TESTS
[Fact]
public void JArray() {
var sourceData = JsonConvert.DeserializeObject<JArray>(@"[
Expand All @@ -167,6 +173,7 @@ public void JArray() {

Assert.Equal(4m, result.summary[0]);
}
#endif

[Fact]
public void T598818() {
Expand Down Expand Up @@ -213,6 +220,7 @@ public void Issue227() {
Assert.Single(loadResult.data);
}

#if NEWTONSOFT_TESTS
[Fact]
public void NoToStringForNumbers() {
var compiler = new FilterExpressionCompiler(typeof(object), false);
Expand All @@ -229,6 +237,7 @@ void Case(IList clientFilter, string expectedExpr, object trueTestValue) {
10
);
}
#endif

[Fact]
public void T714342() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using Newtonsoft.Json;
using System;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Text.Json;
using Xunit;

namespace DevExtreme.AspNet.Data.Tests {
Expand Down Expand Up @@ -144,7 +143,8 @@ public void Not() {

[Fact]
public void IsUnaryWithJsonCriteria() {
var crit = JsonConvert.DeserializeObject<IList>("[\"!\", []]");
var deserializedList = JsonSerializer.Deserialize<IList>("[\"!\", []]");
var crit = Compatibility.UnwrapList(deserializedList);
var compiler = new FilterExpressionCompiler(typeof(object), false);
Assert.True(compiler.IsUnary(crit));
}
Expand Down Expand Up @@ -241,7 +241,8 @@ public void T105740() {

[Fact]
public void JsonObjects() {
var crit = (IList)JsonConvert.DeserializeObject(@"[ [ ""StringProp"", ""abc"" ], [ ""NullableProp"", null ] ]");
var deserializedList = JsonSerializer.Deserialize<IList>(@"[ [ ""StringProp"", ""abc"" ], [ ""NullableProp"", null ] ]");
var crit = Compatibility.UnwrapList(deserializedList);
var expr = Compile<DataItem1>(crit);
Assert.Equal(@"((obj.StringProp == ""abc"") AndAlso (obj.NullableProp == null))", expr.Body.ToString());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System;
using Xunit;

#if NEWTONSOFT_TESTS
using System.Collections;
using Newtonsoft.Json;
#endif

namespace DevExtreme.AspNet.Data.Tests {

public class FilterExpressionCompilerTypeConversionTests {
Expand Down Expand Up @@ -225,11 +225,15 @@ public void StringFuncOnTimeSpan() {
AssertEvaluation(obj, new[] { "NullableTime", "contains", "23" });
}

#if NEWTONSOFT_TESTS
[Theory]
[InlineData(DateParseHandling.None)]
[InlineData(DateParseHandling.DateTime)]
[InlineData(DateParseHandling.DateTimeOffset)]
public void Issue477(DateParseHandling dateParseHandling) {
//https://github.com/DevExpress/DevExtreme.AspNet.Data/pull/478
//https://github.com/DevExpress/DevExtreme.AspNet.Data/issues/477

var date = new DateTimeOffset(2021, 1, 1, 0, 0, 0, TimeSpan.Zero);
var filterJSON = JsonConvert.SerializeObject(new object[] { "this", date });
var deserializedFilter = JsonConvert.DeserializeObject<IList>(filterJSON, new JsonSerializerSettings {
Expand All @@ -243,6 +247,7 @@ public void Issue477(DateParseHandling dateParseHandling) {
var loadResult = DataSourceLoader.Load(new[] { date }, loadOptions);
Assert.Single(loadResult.data);
}
#endif
}

}
3 changes: 0 additions & 3 deletions net/DevExtreme.AspNet.Data.Tests/GroupHelperTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using DevExtreme.AspNet.Data.Helpers;
using DevExtreme.AspNet.Data.ResponseModel;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;

namespace DevExtreme.AspNet.Data.Tests {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Newtonsoft.Json;
using System;
using System;
using System.Linq;
using System.Text.Json;
using Xunit;

namespace DevExtreme.AspNet.Data.Tests {
Expand Down Expand Up @@ -133,7 +133,7 @@ public void Bug349() {
}

static string DataToString(object data) {
return JsonConvert.SerializeObject(data).Replace("\"", "");
return JsonSerializer.Serialize(data).Replace("\"", "");
}

}
Expand Down
33 changes: 33 additions & 0 deletions net/DevExtreme.AspNet.Data.Tests/ResponseModelExTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#if NEWTONSOFT_TESTS
using DevExtreme.AspNet.Data.ResponseModel;

using System.ComponentModel;
using Xunit;

using Newtonsoft.Json;

namespace DevExtreme.AspNet.Data.Tests {

public class ResponseModelTestsEx {

class LoadResultEx : LoadResult {
[DefaultValue(-1), JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public new int totalCount { get; set; } = -1;
[DefaultValue(-1), JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public new int groupCount { get; set; } = -1;
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public new object[] summary { get; set; }
}

[Fact]
public void EmptyLoadResultSerialization() {
Assert.Equal(
"{\"data\":null}",
JsonConvert.SerializeObject(new LoadResultEx())
);
}

}

}
#endif
16 changes: 8 additions & 8 deletions net/DevExtreme.AspNet.Data.Tests/ResponseModelTests.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
using DevExtreme.AspNet.Data.ResponseModel;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;

using System.Text.Json;
using Xunit;

namespace DevExtreme.AspNet.Data.Tests {

public class ResponseModelTests {

[Fact]
[Fact(Skip = "Skip until consolidation or target bump to net7 and ShouldSerialize")]
public void EmptyLoadResultSerialization() {
//https://github.com/dotnet/runtime/issues/41630
//https://github.com/dotnet/runtime/issues/36236
Assert.Equal(
"{\"data\":null}",
JsonConvert.SerializeObject(new LoadResult())
"{\"data\":null,\"totalCount\":-1,\"groupCount\":-1}",
JsonSerializer.Serialize(new LoadResult())
);
}

[Fact]
public void EmptyGroupSerialization() {
var json = JsonConvert.SerializeObject(new Group());
var json = JsonSerializer.Serialize(new Group());

// these must always be present
Assert.Contains("\"key\":", json);
Expand Down
Loading

0 comments on commit 82aba9a

Please sign in to comment.