Skip to content

Commit dbf5315

Browse files
Remove Newtonsoft.Json
Remove dependency on Newtonsoft.Json. Relates to #441.
1 parent 7bc9473 commit dbf5315

14 files changed

+72
-155
lines changed

Directory.Packages.props

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
1313
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
1414
<PackageVersion Include="Moq" Version="4.18.2" />
15-
<PackageVersion Include="Newtonsoft.Json" Version="9.0.1" />
1615
<PackageVersion Include="Newtonsoft.Json.Schema" Version="3.0.14" />
1716
<PackageVersion Include="Polly" Version="7.2.3" />
1817
<PackageVersion Include="Refit" Version="6.3.2" />
@@ -27,13 +26,11 @@
2726
<PackageVersion Include="System.Net.Http" Version="4.0.0" />
2827
</ItemGroup>
2928
<PackageVersion Update="Microsoft.AspNetCore.WebUtilities" Version="2.2.0" />
30-
<PackageVersion Update="Newtonsoft.Json" Version="12.0.3" />
3129
<PackageVersion Update="System.Text.Json" Version="5.0.0" />
3230
<ItemGroup Condition=" '$(AssemblyName)' == 'JustEat.HttpClientInterception' and '$(TargetFramework)' == 'net6.0' ">
3331
</ItemGroup>
3432
<ItemGroup Condition=" '$(AssemblyName)' != 'JustEat.HttpClientInterception' ">
3533
<PackageVersion Update="Microsoft.AspNetCore.WebUtilities" Version="2.2.0" />
36-
<PackageVersion Update="Newtonsoft.Json" Version="13.0.1" />
3734
<PackageVersion Update="System.Text.Json" Version="6.0.6" />
3835
</ItemGroup>
3936
<ItemGroup>

src/HttpClientInterception/Bundles/Bundle.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Just Eat, 2017. All rights reserved.
22
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.
33

4-
using Newtonsoft.Json;
4+
using System.Text.Json.Serialization;
55

66
namespace JustEat.HttpClientInterception.Bundles;
77

@@ -15,24 +15,30 @@ internal sealed class Bundle
1515
/// <summary>
1616
/// Gets or sets the bundle's Id.
1717
/// </summary>
18-
[JsonProperty("id", NullValueHandling = NullValueHandling.Ignore)]
18+
[JsonPropertyName("id")]
19+
#if NET6_0_OR_GREATER
20+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
21+
#endif
1922
public string? Id { get; set; }
2023

2124
/// <summary>
2225
/// Gets or sets the bundle's comment.
2326
/// </summary>
24-
[JsonProperty("comment", NullValueHandling = NullValueHandling.Ignore)]
27+
[JsonPropertyName("comment")]
28+
#if NET6_0_OR_GREATER
29+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
30+
#endif
2531
public string? Comment { get; set; }
2632

2733
/// <summary>
2834
/// Gets or sets the bundle version.
2935
/// </summary>
30-
[JsonProperty("version")]
36+
[JsonPropertyName("version")]
3137
public int Version { get; set; } = 1;
3238

3339
/// <summary>
3440
/// Gets or sets the items in the bundle.
3541
/// </summary>
36-
[JsonProperty("items")]
42+
[JsonPropertyName("items")]
3743
public IList<BundleItem>? Items { get; set; }
3844
}

src/HttpClientInterception/Bundles/BundleFactory.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Just Eat, 2017. All rights reserved.
22
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.
33

4-
using Newtonsoft.Json;
4+
using System.Text.Json;
55

66
namespace JustEat.HttpClientInterception.Bundles;
77

@@ -13,7 +13,12 @@ internal static class BundleFactory
1313
/// <summary>
1414
/// Gets the JSON serializer settings to use.
1515
/// </summary>
16-
private static JsonSerializerSettings Settings { get; } = new JsonSerializerSettings();
16+
private static JsonSerializerOptions Settings { get; } =
17+
#if NET7_0_OR_GREATER
18+
JsonSerializerOptions.Default;
19+
#else
20+
new();
21+
#endif
1722

1823
/// <summary>
1924
/// Creates a <see cref="Bundle"/> from the specified JSON file.
@@ -25,6 +30,6 @@ internal static class BundleFactory
2530
public static Bundle Create(string path)
2631
{
2732
string json = File.ReadAllText(path);
28-
return JsonConvert.DeserializeObject<Bundle>(json, Settings)!;
33+
return JsonSerializer.Deserialize<Bundle>(json, Settings);
2934
}
3035
}
Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright (c) Just Eat, 2017. All rights reserved.
22
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.
33

4-
using Newtonsoft.Json;
5-
using Newtonsoft.Json.Linq;
4+
using System.Text.Json;
5+
using System.Text.Json.Serialization;
66

77
namespace JustEat.HttpClientInterception.Bundles;
88

@@ -16,102 +16,129 @@ internal sealed class BundleItem
1616
/// <summary>
1717
/// Gets or sets the optional Id of the item.
1818
/// </summary>
19-
[JsonProperty("id", NullValueHandling = NullValueHandling.Ignore)]
19+
[JsonPropertyName("id")]
20+
#if NET6_0_OR_GREATER
21+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
22+
#endif
2023
public string? Id { get; set; }
2124

2225
/// <summary>
2326
/// Gets or sets the optional comment for the item.
2427
/// </summary>
25-
[JsonProperty("comment", NullValueHandling = NullValueHandling.Ignore)]
28+
[JsonPropertyName("comment")]
29+
#if NET6_0_OR_GREATER
30+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
31+
#endif
2632
public string? Comment { get; set; }
2733

2834
/// <summary>
2935
/// Gets or sets the optional HTTP version for the item.
3036
/// </summary>
31-
[JsonProperty("version", NullValueHandling = NullValueHandling.Ignore)]
37+
[JsonPropertyName("version")]
38+
#if NET6_0_OR_GREATER
39+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
40+
#endif
3241
public string? Version { get; set; }
3342

3443
/// <summary>
3544
/// Gets or sets the default HTTP method for the item.
3645
/// </summary>
37-
[JsonProperty("method")]
46+
[JsonPropertyName("method")]
3847
public string? Method { get; set; }
3948

4049
/// <summary>
4150
/// Gets or sets the request URI for the item.
4251
/// </summary>
43-
[JsonProperty("uri")]
52+
[JsonPropertyName("uri")]
4453
public string? Uri { get; set; }
4554

4655
/// <summary>
4756
/// Gets or sets the optional priority for the item.
4857
/// </summary>
49-
[JsonProperty("priority", NullValueHandling = NullValueHandling.Ignore)]
58+
[JsonPropertyName("priority")]
59+
#if NET6_0_OR_GREATER
60+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
61+
#endif
5062
public int? Priority { get; set; }
5163

5264
/// <summary>
5365
/// Gets or sets the HTTP status code for the response for item.
5466
/// </summary>
55-
[JsonProperty("status", NullValueHandling = NullValueHandling.Ignore)]
67+
[JsonPropertyName("status")]
68+
#if NET6_0_OR_GREATER
69+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
70+
#endif
5671
public string? Status { get; set; }
5772

5873
/// <summary>
5974
/// Gets or sets the optional request headers for the item.
6075
/// </summary>
61-
[JsonProperty("requestHeaders", NullValueHandling = NullValueHandling.Ignore)]
76+
[JsonPropertyName("requestHeaders")]
77+
#if NET6_0_OR_GREATER
78+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
79+
#endif
6280
public IDictionary<string, ICollection<string>>? RequestHeaders { get; set; }
6381

6482
/// <summary>
6583
/// Gets or sets the optional response headers for the item.
6684
/// </summary>
67-
[JsonProperty("responseHeaders", NullValueHandling = NullValueHandling.Ignore)]
85+
[JsonPropertyName("responseHeaders")]
86+
#if NET6_0_OR_GREATER
87+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
88+
#endif
6889
public IDictionary<string, ICollection<string>>? ResponseHeaders { get; set; }
6990

7091
/// <summary>
7192
/// Gets or sets the optional content headers for the item.
7293
/// </summary>
73-
[JsonProperty("contentHeaders", NullValueHandling = NullValueHandling.Ignore)]
94+
[JsonPropertyName("contentHeaders")]
95+
#if NET6_0_OR_GREATER
96+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
97+
#endif
7498
public IDictionary<string, ICollection<string>>? ContentHeaders { get; set; }
7599

76100
/// <summary>
77101
/// Gets or sets the optional content format of the item.
78102
/// </summary>
79-
[JsonProperty("contentFormat")]
103+
[JsonPropertyName("contentFormat")]
80104
public string? ContentFormat { get; set; }
81105

82106
/// <summary>
83107
/// Gets or sets the content of the item as JSON.
84108
/// </summary>
85-
[JsonProperty("contentJson")]
86-
public JToken? ContentJson { get; set; }
109+
[JsonPropertyName("contentJson")]
110+
public JsonElement ContentJson { get; set; }
87111

88112
/// <summary>
89113
/// Gets or sets the content of the item as a string.
90114
/// </summary>
91-
[JsonProperty("contentString")]
115+
[JsonPropertyName("contentString")]
92116
public string? ContentString { get; set; }
93117

94118
/// <summary>
95119
/// Gets or sets the optional templating values.
96120
/// </summary>
97-
[JsonProperty("templateValues", NullValueHandling = NullValueHandling.Ignore)]
121+
[JsonPropertyName("templateValues")]
122+
#if NET6_0_OR_GREATER
123+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
124+
#endif
98125
public IDictionary<string, string>? TemplateValues { get; set; }
99126

100127
/// <summary>
101128
/// Gets or sets a value indicating whether to ignore the URI's path.
102129
/// </summary>
103-
[JsonProperty("ignorePath", NullValueHandling = NullValueHandling.Ignore)]
130+
[JsonPropertyName("ignorePath")]
104131
public bool IgnorePath { get; set; }
105132

106133
/// <summary>
107134
/// Gets or sets a value indicating whether to ignore the URI's query string.
108135
/// </summary>
109-
[JsonProperty("ignoreQuery", NullValueHandling = NullValueHandling.Ignore)]
136+
[JsonPropertyName("ignoreQuery")]
110137
public bool IgnoreQuery { get; set; }
111138

112139
/// <summary>
113140
/// Gets or sets a value indicating whether to skip the item.
114141
/// </summary>
115-
[JsonProperty("skip", NullValueHandling = NullValueHandling.Ignore)]
142+
[JsonPropertyName("skip")]
116143
public bool Skip { get; set; }
117144
}

src/HttpClientInterception/Bundles/BundleItemConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ private static HttpRequestInterceptionBuilder SetContent(this HttpRequestInterce
100100
return Encoding.UTF8.GetString(decoded);
101101

102102
case "JSON":
103-
return item.ContentJson!.ToString();
103+
return item.ContentJson.ToString();
104104

105105
case null:
106106
case "":

src/HttpClientInterception/HttpClientInterceptorOptionsExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System.ComponentModel;
55
using System.Net;
66
using System.Text;
7-
using Newtonsoft.Json;
7+
using System.Text.Json;
88

99
namespace JustEat.HttpClientInterception;
1010

@@ -202,7 +202,7 @@ public static HttpClientInterceptorOptions RegisterGetJson(
202202

203203
byte[] ContentFactory()
204204
{
205-
string json = JsonConvert.SerializeObject(content);
205+
string json = JsonSerializer.Serialize(content);
206206
return Encoding.UTF8.GetBytes(json);
207207
}
208208

src/HttpClientInterception/HttpRequestInterceptionBuilderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public static HttpRequestInterceptionBuilder WithJsonContent(
207207
this HttpRequestInterceptionBuilder builder,
208208
object content)
209209
{
210-
return builder.WithNewtonsoftJsonContent(content);
210+
return builder.WithSystemTextJsonContent(content);
211211
}
212212

213213
/// <summary>

src/HttpClientInterception/JustEat.HttpClientInterception.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
<ItemGroup>
2222
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" PrivateAssets="All" />
2323
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" />
24-
<PackageReference Include="Newtonsoft.Json" />
2524
<PackageReference Include="System.Text.Json" />
2625
</ItemGroup>
2726
<ItemGroup Condition=" '$(TargetFramework)' == 'net472' ">

src/HttpClientInterception/NewtonsoftJsonExtensions.cs

Lines changed: 0 additions & 54 deletions
This file was deleted.

src/HttpClientInterception/PublicAPI.Shipped.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ JustEat.HttpClientInterception.HttpRequestNotInterceptedException.Request.get ->
7878
JustEat.HttpClientInterception.InterceptingHttpMessageHandler
7979
JustEat.HttpClientInterception.InterceptingHttpMessageHandler.InterceptingHttpMessageHandler(JustEat.HttpClientInterception.HttpClientInterceptorOptions! options) -> void
8080
JustEat.HttpClientInterception.InterceptingHttpMessageHandler.InterceptingHttpMessageHandler(JustEat.HttpClientInterception.HttpClientInterceptorOptions! options, System.Net.Http.HttpMessageHandler! innerHandler) -> void
81-
JustEat.HttpClientInterception.NewtonsoftJsonExtensions
8281
JustEat.HttpClientInterception.SystemTextJsonExtensions
8382
override JustEat.HttpClientInterception.InterceptingHttpMessageHandler.SendAsync(System.Net.Http.HttpRequestMessage! request, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage!>!
8483
static JustEat.HttpClientInterception.BundleExtensions.RegisterBundle(this JustEat.HttpClientInterception.HttpClientInterceptorOptions! options, string! path) -> JustEat.HttpClientInterception.HttpClientInterceptorOptions!
@@ -108,7 +107,6 @@ static JustEat.HttpClientInterception.HttpRequestInterceptionBuilderExtensions.R
108107
static JustEat.HttpClientInterception.HttpRequestInterceptionBuilderExtensions.WithContent(this JustEat.HttpClientInterception.HttpRequestInterceptionBuilder! builder, string! content) -> JustEat.HttpClientInterception.HttpRequestInterceptionBuilder!
109108
static JustEat.HttpClientInterception.HttpRequestInterceptionBuilderExtensions.WithFormContent(this JustEat.HttpClientInterception.HttpRequestInterceptionBuilder! builder, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string!, string!>>! parameters) -> JustEat.HttpClientInterception.HttpRequestInterceptionBuilder!
110109
static JustEat.HttpClientInterception.HttpRequestInterceptionBuilderExtensions.WithJsonContent(this JustEat.HttpClientInterception.HttpRequestInterceptionBuilder! builder, object! content) -> JustEat.HttpClientInterception.HttpRequestInterceptionBuilder!
111-
static JustEat.HttpClientInterception.NewtonsoftJsonExtensions.WithNewtonsoftJsonContent(this JustEat.HttpClientInterception.HttpRequestInterceptionBuilder! builder, object! content, Newtonsoft.Json.JsonSerializerSettings? settings = null) -> JustEat.HttpClientInterception.HttpRequestInterceptionBuilder!
112110
static JustEat.HttpClientInterception.SystemTextJsonExtensions.WithSystemTextJsonContent(this JustEat.HttpClientInterception.HttpRequestInterceptionBuilder! builder, object! content, System.Text.Json.JsonSerializerOptions? options = null) -> JustEat.HttpClientInterception.HttpRequestInterceptionBuilder!
113111
virtual JustEat.HttpClientInterception.HttpClientInterceptorOptions.CreateHttpClient(System.Net.Http.HttpMessageHandler? innerHandler = null) -> System.Net.Http.HttpClient!
114112
virtual JustEat.HttpClientInterception.HttpClientInterceptorOptions.CreateHttpMessageHandler() -> System.Net.Http.DelegatingHandler!

0 commit comments

Comments
 (0)