Skip to content

Commit

Permalink
fix: replace all newtonsoft.json usages (#3478)
Browse files Browse the repository at this point in the history
  • Loading branch information
ness001 authored Jul 29, 2022
1 parent aa153c1 commit 82e041c
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 73 deletions.
2 changes: 1 addition & 1 deletion build/Common.nonprod.props
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@
<MicrosoftExtensionsLoggingPkgVer>[6.0.0,)</MicrosoftExtensionsLoggingPkgVer>
<MicrosoftExtensionsLoggingAbstractionsPkgVer>[6.0.0,)</MicrosoftExtensionsLoggingAbstractionsPkgVer>
<MicrosoftNETTestSdkPkgVer>[16.10.0]</MicrosoftNETTestSdkPkgVer>
<NewtonsoftJsonPkgVer>[13.0.1,14.0)</NewtonsoftJsonPkgVer>
<MoqPkgVer>[4.14.5,5.0)</MoqPkgVer>
<RabbitMQClientPkgVer>[6.1.0,7.0)</RabbitMQClientPkgVer>
<RuntimeInstrumentationPkgVer>[1.0.0-rc.2,2.0)</RuntimeInstrumentationPkgVer>
<SwashbuckleAspNetCorePkgVer>[6.2.3]</SwashbuckleAspNetCorePkgVer>
<SystemTextJsonPkgVer>6.0.5</SystemTextJsonPkgVer>
<XUnitRunnerVisualStudioPkgVer>[2.4.3,3.0)</XUnitRunnerVisualStudioPkgVer>
<XUnitPkgVer>[2.4.1,3.0)</XUnitPkgVer>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using Newtonsoft.Json;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Instrumentation.AspNetCore.Implementation;
using OpenTelemetry.Tests;
Expand Down Expand Up @@ -364,7 +364,7 @@ public async Task ExtractContextIrrespectiveOfSamplingDecision(SamplingDecision
// Test TraceContext Propagation
var request = new HttpRequestMessage(HttpMethod.Get, "/api/GetChildActivityTraceContext");
var response = await client.SendAsync(request);
var childActivityTraceContext = JsonConvert.DeserializeObject<Dictionary<string, string>>(response.Content.ReadAsStringAsync().Result);
var childActivityTraceContext = JsonSerializer.Deserialize<Dictionary<string, string>>(response.Content.ReadAsStringAsync().Result);

response.EnsureSuccessStatusCode();

Expand All @@ -376,7 +376,7 @@ public async Task ExtractContextIrrespectiveOfSamplingDecision(SamplingDecision
request = new HttpRequestMessage(HttpMethod.Get, "/api/GetChildActivityBaggageContext");

response = await client.SendAsync(request);
var childActivityBaggageContext = JsonConvert.DeserializeObject<IReadOnlyDictionary<string, string>>(response.Content.ReadAsStringAsync().Result);
var childActivityBaggageContext = JsonSerializer.Deserialize<IReadOnlyDictionary<string, string>>(response.Content.ReadAsStringAsync().Result);

response.EnsureSuccessStatusCode();

Expand Down Expand Up @@ -431,7 +431,7 @@ public async Task ExtractContextIrrespectiveOfTheFilterApplied()
// Ensure that filter was called
Assert.True(isFilterCalled);

var childActivityTraceContext = JsonConvert.DeserializeObject<Dictionary<string, string>>(response.Content.ReadAsStringAsync().Result);
var childActivityTraceContext = JsonSerializer.Deserialize<Dictionary<string, string>>(response.Content.ReadAsStringAsync().Result);

response.EnsureSuccessStatusCode();

Expand All @@ -443,7 +443,7 @@ public async Task ExtractContextIrrespectiveOfTheFilterApplied()
request = new HttpRequestMessage(HttpMethod.Get, "/api/GetChildActivityBaggageContext");

response = await client.SendAsync(request);
var childActivityBaggageContext = JsonConvert.DeserializeObject<IReadOnlyDictionary<string, string>>(response.Content.ReadAsStringAsync().Result);
var childActivityBaggageContext = JsonSerializer.Deserialize<IReadOnlyDictionary<string, string>>(response.Content.ReadAsStringAsync().Result);

response.EnsureSuccessStatusCode();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Text.Json;
using System.Threading.Tasks;
using Moq;
using Newtonsoft.Json;
using OpenTelemetry.Metrics;
using OpenTelemetry.Tests;
using OpenTelemetry.Trace;
Expand Down Expand Up @@ -185,29 +185,30 @@ public async Task HttpOutCallsAreCollectedSuccessfullyAsync(HttpTestData.HttpOut
[Fact]
public async Task DebugIndividualTestAsync()
{
var serializer = new JsonSerializer();
var input = serializer.Deserialize<HttpTestData.HttpOutTestCase[]>(new JsonTextReader(new StringReader(@"
[
{
""name"": ""Response code: 399"",
""method"": ""GET"",
""url"": ""http://{host}:{port}/"",
""responseCode"": 399,
""responseExpected"": true,
""spanName"": ""HTTP GET"",
""spanStatus"": ""UNSET"",
""spanKind"": ""Client"",
""spanAttributes"": {
""http.scheme"": ""http"",
""http.method"": ""GET"",
""http.host"": ""{host}:{port}"",
""http.status_code"": ""399"",
""http.flavor"": ""2.0"",
""http.url"": ""http://{host}:{port}/""
}
}
]
")));
var input = JsonSerializer.Deserialize<HttpTestData.HttpOutTestCase[]>(
@"
[
{
""name"": ""Response code: 399"",
""method"": ""GET"",
""url"": ""http://{host}:{port}/"",
""responseCode"": 399,
""responseExpected"": true,
""spanName"": ""HTTP GET"",
""spanStatus"": ""UNSET"",
""spanKind"": ""Client"",
""spanAttributes"": {
""http.scheme"": ""http"",
""http.method"": ""GET"",
""http.host"": ""{host}:{port}"",
""http.status_code"": ""399"",
""http.flavor"": ""2.0"",
""http.url"": ""http://{host}:{port}/""
}
}
]
",
new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });

var t = (Task)this.GetType().InvokeMember(nameof(this.HttpOutCallsAreCollectedSuccessfullyAsync), BindingFlags.InvokeMethod, null, this, HttpTestData.GetArgumentsFromTestCaseObject(input).First());
await t;
Expand Down
8 changes: 3 additions & 5 deletions test/OpenTelemetry.Instrumentation.Http.Tests/HttpTestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
// limitations under the License.
// </copyright>
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Newtonsoft.Json;
using System.Text.Json;

namespace OpenTelemetry.Instrumentation.Http.Tests
{
Expand All @@ -25,9 +24,8 @@ public static class HttpTestData
public static IEnumerable<object[]> ReadTestCases()
{
var assembly = Assembly.GetExecutingAssembly();
var serializer = new JsonSerializer();
var input = serializer.Deserialize<HttpOutTestCase[]>(new JsonTextReader(new StreamReader(assembly.GetManifestResourceStream("OpenTelemetry.Instrumentation.Http.Tests.http-out-test-cases.json"))));

var input = JsonSerializer.Deserialize<HttpOutTestCase[]>(
assembly.GetManifestResourceStream("OpenTelemetry.Instrumentation.Http.Tests.http-out-test-cases.json"), new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
return GetArgumentsFromTestCaseObject(input);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
using System.IO;
using System.Linq;
using System.Net;
using System.Text.Json;
using Moq;
using Newtonsoft.Json;
using OpenTelemetry.Tests;
using OpenTelemetry.Trace;
using Xunit;
Expand Down Expand Up @@ -137,26 +137,27 @@ public void HttpOutCallsAreCollectedSuccessfully(HttpTestData.HttpOutTestCase tc
[Fact]
public void DebugIndividualTest()
{
var serializer = new JsonSerializer();
var input = serializer.Deserialize<HttpTestData.HttpOutTestCase>(new JsonTextReader(new StringReader(@"
{
""name"": ""Http version attribute populated"",
""method"": ""GET"",
""url"": ""http://{host}:{port}/"",
""responseCode"": 200,
""spanName"": ""HTTP GET"",
""spanStatus"": ""UNSET"",
""spanKind"": ""Client"",
""setHttpFlavor"": true,
""spanAttributes"": {
""http.method"": ""GET"",
""http.host"": ""{host}:{port}"",
""http.flavor"": ""2.0"",
""http.status_code"": 200,
""http.url"": ""http://{host}:{port}/""
}
}
")));
var input = JsonSerializer.Deserialize<HttpTestData.HttpOutTestCase>(
@"
{
""name"": ""Http version attribute populated"",
""method"": ""GET"",
""url"": ""http://{host}:{port}/"",
""responseCode"": 200,
""spanName"": ""HTTP GET"",
""spanStatus"": ""UNSET"",
""spanKind"": ""Client"",
""setHttpFlavor"": true,
""spanAttributes"": {
""http.method"": ""GET"",
""http.host"": ""{host}:{port}"",
""http.flavor"": ""2.0"",
""http.status_code"": ""200"",
""http.url"": ""http://{host}:{port}/""
}
}
",
new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
this.HttpOutCallsAreCollectedSuccessfully(input);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="$(NewtonsoftJsonPkgVer)" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNETTestSdkPkgVer)" />
<PackageReference Include="Moq" Version="$(MoqPkgVer)" />
<PackageReference Include="System.Text.Json" Version="$(SystemTextJsonPkgVer)" />
<PackageReference Include="xunit" Version="$(XUnitPkgVer)" />
<PackageReference Include="xunit.runner.visualstudio" Version="$(XUnitRunnerVisualStudioPkgVer)">
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"http.method": "GET",
"http.host": "{host}:{port}",
"http.flavor": "2.0",
"http.status_code": 200,
"http.status_code": "200",
"http.url": "http://{host}:{port}/"
}
},
Expand All @@ -27,7 +27,7 @@
"http.method": "POST",
"http.host": "{host}:{port}",
"http.flavor": "2.0",
"http.status_code": 200,
"http.status_code": "200",
"http.url": "http://{host}:{port}/"
}
},
Expand All @@ -44,7 +44,7 @@
"http.method": "GET",
"http.host": "{host}:{port}",
"http.flavor": "2.0",
"http.status_code": 200,
"http.status_code": "200",
"http.url": "http://{host}:{port}/path/to/resource/"
}
},
Expand All @@ -61,7 +61,7 @@
"http.method": "GET",
"http.host": "{host}:{port}",
"http.flavor": "2.0",
"http.status_code": 200,
"http.status_code": "200",
"http.url": "http://{host}:{port}/path/to/resource#fragment"
}
},
Expand All @@ -78,7 +78,7 @@
"http.method": "GET",
"http.host": "{host}:{port}",
"http.flavor": "2.0",
"http.status_code": 200,
"http.status_code": "200",
"http.url": "http://{host}:{port}/path/to/resource#fragment"
}
},
Expand Down Expand Up @@ -129,7 +129,7 @@
"http.method": "GET",
"http.host": "{host}:{port}",
"http.flavor": "2.0",
"http.status_code": 200,
"http.status_code": "200",
"http.url": "http://{host}:{port}/"
}
},
Expand All @@ -146,7 +146,7 @@
"http.method": "GET",
"http.host": "{host}:{port}",
"http.flavor": "2.0",
"http.status_code": 200,
"http.status_code": "200",
"http.url": "http://{host}:{port}/"
}
},
Expand Down Expand Up @@ -333,7 +333,7 @@
"http.method": "GET",
"http.host": "{host}:{port}",
"http.flavor": "2.0",
"http.status_code": 200,
"http.status_code": "200",
"http.url": "http://{host}:{port}/"
}
}
Expand Down
9 changes: 5 additions & 4 deletions test/TestApp.AspNetCore.3.1/Controllers/ForwardController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
// </copyright>
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace TestApp.AspNetCore._3._1.Controllers
{
Expand All @@ -44,7 +45,7 @@ public async Task<string> Post([FromBody] Data[] data)
var request = new HttpRequestMessage(HttpMethod.Post, argument.Url)
{
Content = new StringContent(
JsonConvert.SerializeObject(argument.Arguments),
JsonSerializer.Serialize(argument.Arguments),
Encoding.UTF8,
"application/json"),
};
Expand All @@ -61,10 +62,10 @@ public async Task<string> Post([FromBody] Data[] data)

public class Data
{
[JsonProperty("url")]
[JsonPropertyName("url")]
public string Url { get; set; }

[JsonProperty("arguments")]
[JsonPropertyName("arguments")]
public Data[] Arguments { get; set; }
}
}
Expand Down
9 changes: 5 additions & 4 deletions test/TestApp.AspNetCore.6.0/Controllers/ForwardController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@

using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace TestApp.AspNetCore._6._0.Controllers
{
Expand All @@ -45,7 +46,7 @@ public async Task<string> Post([FromBody] Data[] data)
var request = new HttpRequestMessage(HttpMethod.Post, argument.Url)
{
Content = new StringContent(
JsonConvert.SerializeObject(argument.Arguments),
JsonSerializer.Serialize(argument.Arguments),
Encoding.UTF8,
"application/json"),
};
Expand All @@ -62,10 +63,10 @@ public async Task<string> Post([FromBody] Data[] data)

public class Data
{
[JsonProperty("url")]
[JsonPropertyName("url")]
public string Url { get; set; }

[JsonProperty("arguments")]
[JsonPropertyName("arguments")]
public Data[] Arguments { get; set; }
}
}
Expand Down

0 comments on commit 82e041c

Please sign in to comment.