Skip to content

Use Newtonsoft.Json.Bson except in NetCore projects #370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<OutputPath>$(OutputPath)NetStandard\</OutputPath>
<DocumentationFile>$(OutputPath)$(AssemblyName).xml</DocumentationFile>
<RunCodeAnalysis>false</RunCodeAnalysis>
<DefineConstants>$(DefineConstants);ASPNETMVC;NEWTONSOFTJSON10</DefineConstants>
<DefineConstants>$(DefineConstants);ASPNETMVC</DefineConstants>
<NoWarn>1591</NoWarn>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<Configurations>$(Configurations);CodeAnalysis</Configurations>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
<SpecificVersion>False</SpecificVersion>
<Private>False</Private>
</Reference>
<Reference Include="Newtonsoft.Json.Bson, Version=1.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\..\packages\Newtonsoft.Json.Bson.1.0.2\lib\net45\Newtonsoft.Json.Bson.dll</HintPath>
<SpecificVersion>False</SpecificVersion>
<Private>False</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
Expand Down
1 change: 1 addition & 0 deletions src/System.Net.Http.Formatting/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="net45" />
<package id="Newtonsoft.Json.Bson" version="1.0.2" targetFramework="net45" />
</packages>
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<RootNamespace>System.Net.Http</RootNamespace>
<AssemblyName>System.Net.Http.Formatting.NetStandard.Test</AssemblyName>
<OutputPath>..\..\bin\$(Configuration)\Test\</OutputPath>
<DefineConstants>$(DefineConstants);NEWTONSOFTJSON10</DefineConstants>
<Configurations>$(Configurations);CodeAnalysis</Configurations>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Comment on lines -265 to +268

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to confirm, this inversion in logic was intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. We have 3 Http.Formatting projects in src: NetCore for Profile259 (which is supposed to be similar to netstandard1.3 but is actually a superset in some ways), NetStandard for netstandard2.0, and "regular" for .NET Framework. Previously only the NetStandard project defined NEWTONSOFTJSON10 and used the Json.Bson package. Now all but the NetCore project uses Json.Bson. That project defines NETFX_CORE.

#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}'.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
<HintPath>..\..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json.Bson, Version=1.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\..\packages\Newtonsoft.Json.Bson.1.0.2\lib\net45\Newtonsoft.Json.Bson.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Data" />
Expand Down
1 change: 1 addition & 0 deletions test/System.Net.Http.Formatting.Test/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<package id="Castle.Core" version="4.2.1" targetFramework="net452" />
<package id="Moq" version="4.7.142" targetFramework="net452" />
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="net452" />
<package id="Newtonsoft.Json.Bson" version="1.0.2" targetFramework="net452" />
<package id="xunit" version="2.3.0" targetFramework="net452" />
<package id="xunit.abstractions" version="2.0.1" targetFramework="net452" />
<package id="xunit.analyzers" version="0.7.0" targetFramework="net452" />
Expand Down