Skip to content

Commit 6d92113

Browse files
authored
Updated solution to remove unnecessary dependencies on Newtonsoft.Json (#1524)
* Updated solution to remove unnecessary dependencies on Newtonsoft.Json in favor of just using System.Text.Json throughout for JSON serialization.
1 parent f588665 commit 6d92113

File tree

5 files changed

+31
-32
lines changed

5 files changed

+31
-32
lines changed

Directory.Packages.props

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
4141
<PackageVersion Include="MinVer" Version="2.3.0" />
4242
<PackageVersion Include="Moq" Version="4.20.72" />
43-
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
4443
<PackageVersion Include="protobuf-net.Grpc.AspNetCore" Version="1.2.2" />
4544
<PackageVersion Include="Serilog.AspNetCore" Version="6.1.0" />
4645
<PackageVersion Include="Serilog.Sinks.Console" Version="4.1.0" />

test/Dapr.Actors.Test/DaprFormatTimeSpanTests.cs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@
1111
// limitations under the License.
1212
// ------------------------------------------------------------------------
1313

14+
using System.Text.Json;
15+
1416
#pragma warning disable 0618
1517
namespace Dapr.Actors.Test;
1618

1719
using System;
18-
using System.Collections.Generic;
19-
using System.IO;
20-
using System.Threading.Tasks;
20+
using System.Collections.Generic;using System.Threading.Tasks;
2121
using Dapr.Actors.Runtime;
22-
using Newtonsoft.Json;
2322
using Xunit;
2423

2524
public class DaprFormatTimeSpanTests
@@ -28,12 +27,12 @@ public class DaprFormatTimeSpanTests
2827
{
2928
new object[]
3029
{
31-
"{\"dueTime\":\"4h15m50s60ms\"",
30+
"4h15m50s60ms",
3231
new TimeSpan(0, 4, 15, 50, 60),
3332
},
3433
new object[]
3534
{
36-
"{\"dueTime\":\"0h35m10s12ms\"",
35+
"0h35m10s12ms",
3736
new TimeSpan(0, 0, 35, 10, 12),
3837
},
3938
};
@@ -90,17 +89,8 @@ public class DaprFormatTimeSpanTests
9089

9190
[Theory]
9291
[MemberData(nameof(DaprFormatTimeSpanJsonStringsAndExpectedDeserializedValues))]
93-
public void DaprFormat_TimeSpan_Parsing(string daprFormatTimeSpanJsonString, TimeSpan expectedDeserializedValue)
92+
public void DaprFormat_TimeSpan_Parsing(string timespanString, TimeSpan expectedDeserializedValue)
9493
{
95-
using var textReader = new StringReader(daprFormatTimeSpanJsonString);
96-
using var jsonTextReader = new JsonTextReader(textReader);
97-
98-
while (jsonTextReader.TokenType != JsonToken.String)
99-
{
100-
jsonTextReader.Read();
101-
}
102-
103-
var timespanString = (string)jsonTextReader.Value;
10494
var deserializedTimeSpan = ConverterUtils.ConvertTimeSpanFromDaprFormat(timespanString);
10595

10696
Assert.Equal(expectedDeserializedValue, deserializedTimeSpan);
@@ -116,7 +106,7 @@ static Task<string> SerializeAsync(TimeSpan dueTime, TimeSpan period)
116106
state: null,
117107
dueTime: dueTime,
118108
period: period);
119-
return Task.FromResult(System.Text.Json.JsonSerializer.Serialize<TimerInfo>(timerInfo));
109+
return Task.FromResult(JsonSerializer.Serialize<TimerInfo>(timerInfo));
120110
}
121111

122112
var inTheFuture = TimeSpan.FromMilliseconds(20);

test/Dapr.AspNetCore.IntegrationTest.App/Widget.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@
1111
// limitations under the License.
1212
// ------------------------------------------------------------------------
1313

14+
using System.Text.Json.Serialization;
15+
1416
namespace Dapr.AspNetCore.IntegrationTest.App;
1517

1618
public class Widget
1719
{
20+
[JsonPropertyName("size")]
1821
public string Size { get; set; }
1922

23+
[JsonPropertyName("count")]
2024
public int Count { get; set; }
21-
}
25+
}

test/Dapr.AspNetCore.IntegrationTest/AuthenticationTest.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,21 @@
1414
using System.Net;
1515
using System.Net.Http;
1616
using System.Text;
17+
using System.Text.Json;
1718
using System.Threading.Tasks;
1819
using Dapr.AspNetCore.IntegrationTest.App;
1920
using Shouldly;
20-
using Newtonsoft.Json;
2121
using Xunit;
2222

2323
namespace Dapr.AspNetCore.IntegrationTest;
2424

2525
public class AuthenticationTest
2626
{
27+
private JsonSerializerOptions serializerOptions = new JsonSerializerOptions
28+
{
29+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
30+
};
31+
2732
[Fact]
2833
public async Task ValidToken_ShouldBeAuthenticatedAndAuthorized()
2934
{
@@ -36,13 +41,13 @@ public async Task ValidToken_ShouldBeAuthenticatedAndAuthorized()
3641
var httpClient = factory.CreateClient(new Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactoryClientOptions { HandleCookies = false });
3742
var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/requires-api-token")
3843
{
39-
Content = new StringContent(JsonConvert.SerializeObject(userInfo), Encoding.UTF8, "application/json")
44+
Content = new StringContent(JsonSerializer.Serialize(userInfo), Encoding.UTF8, "application/json")
4045
};
4146
request.Headers.Add("Dapr-Api-Token", "abcdefg");
4247
var response = await httpClient.SendAsync(request);
4348
response.EnsureSuccessStatusCode();
4449
var responseContent = await response.Content.ReadAsStringAsync();
45-
var responseUserInfo = JsonConvert.DeserializeObject<UserInfo>(responseContent);
50+
var responseUserInfo = JsonSerializer.Deserialize<UserInfo>(responseContent, serializerOptions);
4651
responseUserInfo.Name.ShouldBe(userInfo.Name);
4752
}
4853
}
@@ -59,12 +64,12 @@ public async Task InvalidToken_ShouldBeUnauthorized()
5964
var httpClient = factory.CreateClient(new Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactoryClientOptions { HandleCookies = false });
6065
var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/requires-api-token")
6166
{
62-
Content = new StringContent(JsonConvert.SerializeObject(userInfo), Encoding.UTF8, "application/json")
67+
Content = new StringContent(JsonSerializer.Serialize(userInfo, serializerOptions), Encoding.UTF8, "application/json")
6368
};
6469
request.Headers.Add("Dapr-Api-Token", "asdfgh");
6570
var response = await httpClient.SendAsync(request);
6671

6772
response.StatusCode.ShouldBe(HttpStatusCode.Unauthorized);
6873
}
6974
}
70-
}
75+
}

test/Dapr.AspNetCore.IntegrationTest/ControllerIntegrationTest.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
// limitations under the License.
1212
// ------------------------------------------------------------------------
1313

14-
namespace Dapr.AspNetCore.IntegrationTest;
15-
14+
using System.Text.Json;
1615
using System.Net.Http;
1716
using System.Threading.Tasks;
1817
using Dapr.AspNetCore.IntegrationTest.App;
1918
using Shouldly;
20-
using Newtonsoft.Json;
2119
using Xunit;
2220

21+
namespace Dapr.AspNetCore.IntegrationTest;
22+
2323
public class ControllerIntegrationTest
2424
{
2525
[Fact]
@@ -55,7 +55,7 @@ public async Task ModelBinder_GetFromStateEntryWithKeyPresentInStateStore_Return
5555
var response = await httpClient.SendAsync(request);
5656
response.EnsureSuccessStatusCode();
5757
var responseContent = await response.Content.ReadAsStringAsync();
58-
var responseWidget = JsonConvert.DeserializeObject<Widget>(responseContent);
58+
var responseWidget = JsonSerializer.Deserialize<Widget>(responseContent);
5959
responseWidget.Size.ShouldBe(widget.Size);
6060
responseWidget.Count.ShouldBe(widget.Count);
6161
}
@@ -72,7 +72,8 @@ public async Task ModelBinder_GetFromStateEntryWithKeyNotInStateStore_ReturnsNul
7272
var response = await httpClient.SendAsync(request);
7373
response.EnsureSuccessStatusCode();
7474
var responseContent = await response.Content.ReadAsStringAsync();
75-
var responseWidget = JsonConvert.DeserializeObject<Widget>(responseContent);
75+
var responseWidget =
76+
!string.IsNullOrWhiteSpace(responseContent) ? JsonSerializer.Deserialize<Widget>(responseContent) : null;
7677
Assert.Null(responseWidget);
7778
}
7879
}
@@ -129,7 +130,7 @@ public async Task ModelBinder_GetFromStateEntryWithStateEntry_WithKeyPresentInSt
129130
var response = await httpClient.SendAsync(request);
130131
response.EnsureSuccessStatusCode();
131132
var responseContent = await response.Content.ReadAsStringAsync();
132-
var responseWidget = JsonConvert.DeserializeObject<Widget>(responseContent);
133+
var responseWidget = JsonSerializer.Deserialize<Widget>(responseContent);
133134
responseWidget.Size.ShouldBe(widget.Size);
134135
responseWidget.Count.ShouldBe(widget.Count);
135136
}
@@ -146,7 +147,7 @@ public async Task ModelBinder_GetFromStateEntryWithStateEntry_WithKeyNotInStateS
146147
var response = await httpClient.SendAsync(request);
147148
response.EnsureSuccessStatusCode();
148149
var responseContent = await response.Content.ReadAsStringAsync();
149-
var responseWidget = JsonConvert.DeserializeObject<Widget>(responseContent);
150+
var responseWidget = !string.IsNullOrWhiteSpace(responseContent) ? JsonSerializer.Deserialize<Widget>(responseContent) : null;
150151
Assert.Null(responseWidget);
151152
}
152153
}
@@ -163,4 +164,4 @@ public async Task ModelBinder_CanGetOutOfTheWayWhenTheresNoBinding()
163164
response.EnsureSuccessStatusCode();
164165
}
165166
}
166-
}
167+
}

0 commit comments

Comments
 (0)