From 2cdd25bd49bb8bbb76f13c21606328ac3d2481fb Mon Sep 17 00:00:00 2001 From: Doug Bunting <6431421+dougbu@users.noreply.github.com> Date: Thu, 23 Jun 2022 18:10:16 -0700 Subject: [PATCH] Use Newtonsoft.Json.Bson except in NetCore projects - avoid obsolete `BsonReader` and `BsonWriter` classes - use `NETFX_CORE` for remaining legacy case (for now) - remove `NEWTONSOFTJSON10` define; need only `NETFX_CORE` now --- .../System.Net.Http.Formatting.NetStandard.csproj | 2 +- .../Formatting/BsonMediaTypeFormatter.cs | 14 +++++++------- .../System.Net.Http.Formatting.csproj | 5 +++++ src/System.Net.Http.Formatting/packages.config | 1 + ...tem.Net.Http.Formatting.NetStandard.Test.csproj | 1 - .../Formatting/BsonMediaTypeFormatterTests.cs | 6 +++--- .../System.Net.Http.Formatting.Test.csproj | 4 ++++ .../packages.config | 1 + 8 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/System.Net.Http.Formatting.NetStandard/System.Net.Http.Formatting.NetStandard.csproj b/src/System.Net.Http.Formatting.NetStandard/System.Net.Http.Formatting.NetStandard.csproj index 369ae9fec..2021cff78 100644 --- a/src/System.Net.Http.Formatting.NetStandard/System.Net.Http.Formatting.NetStandard.csproj +++ b/src/System.Net.Http.Formatting.NetStandard/System.Net.Http.Formatting.NetStandard.csproj @@ -7,7 +7,7 @@ $(OutputPath)NetStandard\ $(OutputPath)$(AssemblyName).xml false - $(DefineConstants);ASPNETMVC;NEWTONSOFTJSON10 + $(DefineConstants);ASPNETMVC 1591 false $(Configurations);CodeAnalysis diff --git a/src/System.Net.Http.Formatting/Formatting/BsonMediaTypeFormatter.cs b/src/System.Net.Http.Formatting/Formatting/BsonMediaTypeFormatter.cs index 0e7e2a1f2..99b8cca5b 100644 --- a/src/System.Net.Http.Formatting/Formatting/BsonMediaTypeFormatter.cs +++ b/src/System.Net.Http.Formatting/Formatting/BsonMediaTypeFormatter.cs @@ -11,11 +11,11 @@ using System.Threading.Tasks; using System.Web.Http; using Newtonsoft.Json; -#if NEWTONSOFTJSON10 +#if NETFX_CORE +using Newtonsoft.Json.Bson; +#else using BsonReader = Newtonsoft.Json.Bson.BsonDataReader; using BsonWriter = Newtonsoft.Json.Bson.BsonDataWriter; -#else -using Newtonsoft.Json.Bson; #endif namespace System.Net.Http.Formatting @@ -200,11 +200,11 @@ public override JsonReader CreateJsonReader(Type type, Stream readStream, Encodi throw Error.ArgumentNull("effectiveEncoding"); } -#if !NEWTONSOFTJSON10 +#if NETFX_CORE #pragma warning disable CS0618 // Type or member is obsolete #endif BsonReader reader = new BsonReader(new BinaryReader(readStream, effectiveEncoding)); -#if !NEWTONSOFTJSON10 +#if NETFX_CORE #pragma warning restore CS0618 // Type or member is obsolete #endif @@ -303,11 +303,11 @@ public override JsonWriter CreateJsonWriter(Type type, Stream writeStream, Encod throw Error.ArgumentNull("effectiveEncoding"); } -#if !NEWTONSOFTJSON10 +#if NETFX_CORE #pragma warning disable CS0618 // Type or member is obsolete #endif return new BsonWriter(new BinaryWriter(writeStream, effectiveEncoding)); -#if !NEWTONSOFTJSON10 +#if NETFX_CORE #pragma warning restore CS0618 // Type or member is obsolete #endif } diff --git a/src/System.Net.Http.Formatting/System.Net.Http.Formatting.csproj b/src/System.Net.Http.Formatting/System.Net.Http.Formatting.csproj index f7989e19f..793e175a8 100644 --- a/src/System.Net.Http.Formatting/System.Net.Http.Formatting.csproj +++ b/src/System.Net.Http.Formatting/System.Net.Http.Formatting.csproj @@ -20,6 +20,11 @@ False False + + ..\..\packages\Newtonsoft.Json.Bson.1.0.2\lib\net45\Newtonsoft.Json.Bson.dll + False + False + diff --git a/src/System.Net.Http.Formatting/packages.config b/src/System.Net.Http.Formatting/packages.config index 093dcd239..509dcb7d1 100644 --- a/src/System.Net.Http.Formatting/packages.config +++ b/src/System.Net.Http.Formatting/packages.config @@ -1,4 +1,5 @@  + \ No newline at end of file diff --git a/test/System.Net.Http.Formatting.NetStandard.Test/System.Net.Http.Formatting.NetStandard.Test.csproj b/test/System.Net.Http.Formatting.NetStandard.Test/System.Net.Http.Formatting.NetStandard.Test.csproj index ce3d3a1e7..0b89690d5 100644 --- a/test/System.Net.Http.Formatting.NetStandard.Test/System.Net.Http.Formatting.NetStandard.Test.csproj +++ b/test/System.Net.Http.Formatting.NetStandard.Test/System.Net.Http.Formatting.NetStandard.Test.csproj @@ -5,7 +5,6 @@ System.Net.Http System.Net.Http.Formatting.NetStandard.Test ..\..\bin\$(Configuration)\Test\ - $(DefineConstants);NEWTONSOFTJSON10 $(Configurations);CodeAnalysis false diff --git a/test/System.Net.Http.Formatting.Test/Formatting/BsonMediaTypeFormatterTests.cs b/test/System.Net.Http.Formatting.Test/Formatting/BsonMediaTypeFormatterTests.cs index 806a7249a..e28096df8 100644 --- a/test/System.Net.Http.Formatting.Test/Formatting/BsonMediaTypeFormatterTests.cs +++ b/test/System.Net.Http.Formatting.Test/Formatting/BsonMediaTypeFormatterTests.cs @@ -262,10 +262,10 @@ public async Task FormatterThrowsOnWriteWithInvalidContent() BsonMediaTypeFormatter formatter = new BsonMediaTypeFormatter(); HttpContent content = new StringContent(String.Empty); MemoryStream stream = new MemoryStream(); -#if NEWTONSOFTJSON10 // Json.NET 10's Bson package calculates the path in some exceptions differently. - string expectedPath = "Value"; -#else +#if NETFX_CORE // Separate Bson package (not yet used in NETCore project) calculates the path in exceptions differently string expectedPath = string.Empty; +#else + string expectedPath = "Value"; #endif string expectedMessage = string.Format( "Value is too large to fit in a signed 32 bit integer. BSON does not support unsigned values. Path '{0}'.", diff --git a/test/System.Net.Http.Formatting.Test/System.Net.Http.Formatting.Test.csproj b/test/System.Net.Http.Formatting.Test/System.Net.Http.Formatting.Test.csproj index a3ee8efc8..caec66151 100644 --- a/test/System.Net.Http.Formatting.Test/System.Net.Http.Formatting.Test.csproj +++ b/test/System.Net.Http.Formatting.Test/System.Net.Http.Formatting.Test.csproj @@ -26,6 +26,10 @@ ..\..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll True + + ..\..\packages\Newtonsoft.Json.Bson.1.0.2\lib\net45\Newtonsoft.Json.Bson.dll + True + diff --git a/test/System.Net.Http.Formatting.Test/packages.config b/test/System.Net.Http.Formatting.Test/packages.config index 6e4cf51b5..7025597c7 100644 --- a/test/System.Net.Http.Formatting.Test/packages.config +++ b/test/System.Net.Http.Formatting.Test/packages.config @@ -3,6 +3,7 @@ +