Replace Newtonsoft.Json with System.Text.Json in tests and samples#4323
Conversation
There was a problem hiding this comment.
Pull request overview
Removes the Newtonsoft.Json dependency from all test and sample projects in favor of System.Text.Json, deletes a long-commented legacy AKV sample, and drops the central Newtonsoft.Json PackageVersion. The PR also bundles a number of unrelated pipeline, flaky-test, and unit-test reliability changes that are not described in the PR text.
Changes:
- Migrate
JsonBulkCopyTest,JsonStreamTest, andSqlExceptionTestto STJ; add sharedJsonTestHelperwithJsonNode.DeepEqualson .NET 9+ and a recursiveJsonElementfallback (depth-limited) on earlier TFMs. - Remove
Newtonsoft.JsonPackageReferences from ManualTests/FunctionalTests/Samples csprojs, remove its centralPackageVersion, and deleteAzureKeyVaultProviderLegacyExample_2_0.cs. - Unrelated extras: reset cached switches in
LocalAppContextSwitchesTest, mark several simulated-server testsflaky, gateAsyncCancelledConnectionsTestagainst Kerberos/Managed Instance, addretryCountOnTaskFailureto dotnet install tasks, switch OneBranch symbols variable group/casing, addrestore-dotnet-tools.ymlto OneBranch and Kerberos pipelines, and change Kerberos code-coverage stage condition.
Reviewed changes
Copilot reviewed 19 out of 19 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| Directory.Packages.props | Drop central Newtonsoft.Json PackageVersion. |
| doc/samples/Microsoft.Data.SqlClient.Samples.csproj | Remove Newtonsoft.Json PackageReference. |
| doc/samples/AzureKeyVaultProviderLegacyExample_2_0.cs | Delete already-commented legacy AKV sample. |
| src/.../tests/FunctionalTests/Microsoft.Data.SqlClient.FunctionalTests.csproj | Remove Newtonsoft.Json PackageReference for net8/net462. |
| src/.../tests/FunctionalTests/SqlExceptionTest.cs | Replace Newtonsoft round-trip with STJ property-projection serialization; remove JSONSerializationTest. |
| src/.../tests/ManualTests/Microsoft.Data.SqlClient.ManualTests.csproj | Add JsonTestHelper.cs; drop Newtonsoft.Json PackageReferences. |
| src/.../tests/ManualTests/SQL/JsonTest/JsonBulkCopyTest.cs | Switch JSON serialization and file comparison to STJ + JsonTestHelper. |
| src/.../tests/ManualTests/SQL/JsonTest/JsonStreamTest.cs | Same STJ migration for the stream test. |
| src/.../tests/ManualTests/SQL/JsonTest/JsonTestHelper.cs | New depth-limited deep-equals helper (delegates to JsonNode.DeepEquals on .NET 9+). |
| src/.../tests/ManualTests/DataCommon/DataTestUtility.cs | Add IsNotKerberosTest helper used by gated tests. |
| src/.../tests/ManualTests/SQL/AsyncTest/AsyncCancelledConnectionsTest.cs | Gate CancelAsyncConnections against MI and Kerberos environments. |
| src/.../tests/UnitTests/Microsoft/Data/SqlClient/LocalAppContextSwitchesTest.cs | Reset cached switch fields via helper before asserting defaults. |
| src/.../tests/UnitTests/SimulatedServerTests/ConnectionFailoverTests.cs | Mark two transient-fault retry-disabled tests flaky. |
| src/.../tests/UnitTests/SimulatedServerTests/ConnectionRoutingTestsAzure.cs | Mark routed-location timeout test flaky. |
| eng/pipelines/steps/install-dotnet.yml | Add retryCountOnTaskFailure: 3 to dotnet install tasks. |
| eng/pipelines/onebranch/variables/onebranch-variables.yml | Switch symbols group to symbols-variables-v3 with Ppe casing. |
| eng/pipelines/onebranch/sqlclient-non-official.yml | Use new SymbolsPublishServerPpe / SymbolsPublishTokenUriPpe. |
| eng/pipelines/onebranch/jobs/build-buildproj-job.yml | Restore dotnet local tools before build. |
| eng/pipelines/kerberos/sqlclient-kerberos.yml | Restore dotnet local tools; tighten coverage stage condition to succeeded(). |
4c5e6d7 to
b842414
Compare
|
Any specific reason for removing newtonsoft.json? |
@Wraith2 - We already have System.Text.Json with suitable functionality. I didn't see the need for a second JSON lib, and I also don't want anyone to get the impression that we support some specific functionality from a 3rd party lib (like a particular serialization implementation). |
- Migrate JsonBulkCopyTest and JsonStreamTest to System.Text.Json - Add JsonTestHelper with DeepEquals supporting net8.0+ and net9.0+ - Rewrite SqlExceptionTest to serialize key properties via STJ - Remove JSONSerializationTest (tested Newtonsoft-specific ISerializable) - Delete legacy AzureKeyVaultProviderLegacyExample_2_0.cs (marked TODO) - Remove Newtonsoft.Json from all csproj files and Directory.Packages.props Tracks: #4322
System.Text.Json serializes supplementary-plane characters (e.g. U+29E3D) as \uD867\uDE3D escape sequences, while SQL Server returns them as literal UTF-8. The DeepEqualsCore fallback compared GetRawText() which preserves escaping differences. Add explicit JsonValueKind.String case using GetString() which decodes both representations to the same .NET string.
312ca72 to
77fa2cf
Compare
- Wrap 'using System.Text.Json.Nodes' in #if NET9_0_OR_GREATER (only used in that branch) - Use UnsafeRelaxedJsonEscaping in JsonStreamTest and JsonBulkCopyTest to preserve intentional non-ASCII characters in test payloads - Remove unused System.IO and System.Runtime.Serialization.Formatters.Binary from SqlExceptionTest
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4323 +/- ##
==========================================
- Coverage 66.50% 64.11% -2.40%
==========================================
Files 285 280 -5
Lines 43311 66160 +22849
==========================================
+ Hits 28806 42420 +13614
- Misses 14505 23740 +9235
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Description
Removes the Newtonsoft.Json dependency from all test and sample projects, replacing usage with System.Text.Json. We don't need 2 JSON library dependencies.
Based on #4297 which already migrated
TestUtilities/Config.cs.Changes
System.Text.Json+ newJsonTestHelperJsonNode.DeepEqualson .NET 9+ and a recursiveJsonElementcomparison on .NET 8/462 (with max-depth guard)JSONSerializationTest(tested Newtonsoft-specificISerializabledeserialization)Newtonsoft.Jsonfrom ManualTests, FunctionalTests, Samples csprojsNewtonsoft.JsonPackageVersion entryFollow-up
Testing
SqlExceptionTestpasses on net8.0, net9.0, net10.0*.cs,*.csproj,*.props