Skip to content
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
2 changes: 2 additions & 0 deletions src/Fallout.Common/CI/GitHubActions/GitHubActions.Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ private JObject GetJobDetails(long runId)
.GetResponse()
.AssertSuccessfulStatusCode();

#pragma warning disable CS0618 // GetBodyAsJson(JObject) retires in v11; migrate to GetBodyAsJsonObject + System.Text.Json.Nodes.JsonObject access patterns then.
return response.GetBodyAsJson().GetAwaiter().GetResult()
#pragma warning restore CS0618
.GetChildren("jobs")
.Single(x => x.GetPropertyStringValue("name") == Job);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Fallout.Common/Tools/Mastodon/MastodonTasks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ async Task<string> PostMediaFile(string file)
.AddStreamContent("file", stream, Path.GetFileName(file)))
.GetResponseAsync();

#pragma warning disable CS0618 // GetBodyAsJson(JObject) retires in v11; migrate to GetBodyAsJsonObject + System.Text.Json.Nodes.JsonObject access patterns then.
var json = await response.GetBodyAsJson();
#pragma warning restore CS0618
return json["id"].NotNull().ToString();
}

Expand Down
2 changes: 2 additions & 0 deletions src/Fallout.Common/Tools/Slack/SlackTasks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ public static async Task<string> SendOrUpdateSlackMessage(Configure<SlackMessage
.WithJsonContent(message)
.GetResponseAsync();

#pragma warning disable CS0618 // GetBodyAsJson(JObject) retires in v11; migrate to GetBodyAsJsonObject + System.Text.Json.Nodes.JsonObject access patterns then.
var jobject = await response.GetBodyAsJson();
#pragma warning restore CS0618
var error = jobject.GetPropertyValueOrNull<string>("error");
Assert.True(error == null, error);

Expand Down
1 change: 1 addition & 0 deletions src/Fallout.Utilities.Net/Fallout.Utilities.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" />
<PackageReference Include="System.Net.Http" />
<PackageReference Include="System.Text.Json" />
</ItemGroup>

</Project>
17 changes: 14 additions & 3 deletions src/Fallout.Utilities.Net/HttpRequest.Content.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2026 Maintainers of Fallout.
// Copyright 2026 Maintainers of Fallout.
// Originally based on NUKE by Matthias Koch and contributors.
// Distributed under the MIT License.
// https://github.com/ChrisonSimtian/Fallout/blob/main/LICENSE
Expand All @@ -10,14 +10,15 @@
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using Newtonsoft.Json;

namespace Fallout.Common.Utilities.Net;

public static partial class HttpRequestExtensions
{
/// <summary>
/// Sets the JSON-serialized object as content via <see cref="JsonConvert.SerializeObject(object?)"/>.
/// Sets the JSON-serialized object as content via <see cref="Newtonsoft.Json.JsonConvert.SerializeObject(object?)"/>.
/// </summary>
public static HttpRequestBuilder WithJsonContent<T>(this HttpRequestBuilder builder, T obj)
{
Expand All @@ -26,14 +27,24 @@ public static HttpRequestBuilder WithJsonContent<T>(this HttpRequestBuilder buil
}

/// <summary>
/// Sets the JSON-serialized object as content via <see cref="JsonConvert.SerializeObject(object?)"/>.
/// Sets the JSON-serialized object as content via <see cref="Newtonsoft.Json.JsonConvert.SerializeObject(object?)"/>.
/// </summary>
[Obsolete("Use the JsonSerializerOptions overload instead. Newtonsoft.Json.JsonSerializerSettings is scheduled for removal in v11 as part of the System.Text.Json migration (#83).")]
public static HttpRequestBuilder WithJsonContent<T>(this HttpRequestBuilder builder, T obj, JsonSerializerSettings settings)
{
var content = JsonConvert.SerializeObject(obj, settings);
return builder.WithStringContent(content, "application/json");
}

/// <summary>
/// Sets the JSON-serialized object as content via <see cref="System.Text.Json.JsonSerializer.Serialize{TValue}(TValue, JsonSerializerOptions?)"/>.
/// </summary>
public static HttpRequestBuilder WithJsonContent<T>(this HttpRequestBuilder builder, T obj, JsonSerializerOptions options)
{
var content = System.Text.Json.JsonSerializer.Serialize(obj, options);
return builder.WithStringContent(content, "application/json");
}

/// <summary>
/// Sets the string as content.
/// </summary>
Expand Down
42 changes: 37 additions & 5 deletions src/Fallout.Utilities.Net/HttpResponse.Body.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// Copyright 2026 Maintainers of Fallout.
// Copyright 2026 Maintainers of Fallout.
// Originally based on NUKE by Matthias Koch and contributors.
// Distributed under the MIT License.
// https://github.com/ChrisonSimtian/Fallout/blob/main/LICENSE

using System;
using System.IO;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
Expand All @@ -15,35 +18,64 @@ namespace Fallout.Common.Utilities.Net;
public static partial class HttpResponseExtensions
{
/// <summary>
/// Reads the HTTP response body as JSON.
/// Reads the HTTP response body as JSON via <see cref="Newtonsoft.Json.JsonConvert.DeserializeObject{T}(string)"/>.
/// </summary>
public static async Task<T> GetBodyAsJson<T>(this HttpResponseInspector inspector)
{
return JsonConvert.DeserializeObject<T>(await inspector.GetBodyAsync());
}

/// <summary>
/// Reads the HTTP response body as JSON.
/// Reads the HTTP response body as a Newtonsoft <see cref="JObject"/>.
/// </summary>
[Obsolete("Use GetBodyAsJsonObject (returns System.Text.Json.Nodes.JsonObject) instead. Newtonsoft.Json.Linq.JObject is scheduled for removal in v11 as part of the System.Text.Json migration (#83).")]
public static async Task<JObject> GetBodyAsJson(this HttpResponseInspector inspector)
{
return await inspector.GetBodyAsJson<JObject>();
}

/// <summary>
/// Reads the HTTP response body as JSON.
/// Reads the HTTP response body as JSON via <see cref="Newtonsoft.Json.JsonConvert.DeserializeObject{T}(string, JsonSerializerSettings)"/>.
/// </summary>
[Obsolete("Use the JsonSerializerOptions overload instead. Newtonsoft.Json.JsonSerializerSettings is scheduled for removal in v11 as part of the System.Text.Json migration (#83).")]
public static async Task<T> GetBodyAsJson<T>(this HttpResponseInspector inspector, JsonSerializerSettings settings)
{
return JsonConvert.DeserializeObject<T>(await inspector.GetBodyAsync(), settings);
}

/// <summary>
/// Reads the HTTP response body as JSON.
/// Reads the HTTP response body as a Newtonsoft <see cref="JObject"/> using the given <paramref name="settings"/>.
/// </summary>
[Obsolete("Use GetBodyAsJsonObject with JsonSerializerOptions instead. Newtonsoft types are scheduled for removal in v11 as part of the System.Text.Json migration (#83).")]
public static async Task<JObject> GetBodyAsJson(this HttpResponseInspector inspector, JsonSerializerSettings settings)
{
#pragma warning disable CS0618 // Self-call into the obsolete settings-taking generic overload; both retire together in v11.
return await inspector.GetBodyAsJson<JObject>(settings);
#pragma warning restore CS0618
}

/// <summary>
/// Reads the HTTP response body as JSON via <see cref="System.Text.Json.JsonSerializer.Deserialize{TValue}(string, JsonSerializerOptions?)"/>.
/// </summary>
public static async Task<T> GetBodyAsJson<T>(this HttpResponseInspector inspector, JsonSerializerOptions options)
{
return System.Text.Json.JsonSerializer.Deserialize<T>(await inspector.GetBodyAsync(), options);
}

/// <summary>
/// Reads the HTTP response body as a System.Text.Json <see cref="JsonObject"/>.
/// </summary>
public static async Task<JsonObject> GetBodyAsJsonObject(this HttpResponseInspector inspector)
{
return JsonNode.Parse(await inspector.GetBodyAsync())?.AsObject();
}

/// <summary>
/// Reads the HTTP response body as a System.Text.Json <see cref="JsonObject"/> using the given <paramref name="options"/>.
/// </summary>
public static async Task<JsonObject> GetBodyAsJsonObject(this HttpResponseInspector inspector, JsonNodeOptions options)
{
return JsonNode.Parse(await inspector.GetBodyAsync(), nodeOptions: options)?.AsObject();
}

public static async Task WriteToFile(this HttpResponseInspector inspector, AbsolutePath path, FileMode mode = FileMode.CreateNew)
Expand Down
Loading