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
8 changes: 3 additions & 5 deletions src/Fallout.Common/CI/GitHubActions/GitHubActions.Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

using System.Linq;
using System.Net.Http;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Fallout.Common.Utilities;
using Fallout.Common.Utilities.Net;

Expand All @@ -22,16 +22,14 @@ await _httpClient.Value
.GetResponseAsync();
}

private JObject GetJobDetails(long runId)
private JsonObject GetJobDetails(long runId)
{
var response = _httpClient.Value
.CreateRequest(HttpMethod.Get, $"repos/{Repository}/actions/runs/{runId}/jobs")
.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
return response.GetBodyAsJsonObject().GetAwaiter().GetResult()
.GetChildren("jobs")
.Single(x => x.GetPropertyStringValue("name") == Job);
}
Expand Down
7 changes: 3 additions & 4 deletions src/Fallout.Common/Tools/Mastodon/MastodonTasks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Net.Http;
using System.Threading.Tasks;
using Fallout.Common.Tooling;
using Fallout.Common.Utilities;
using Fallout.Common.Utilities.Net;

namespace Fallout.Common.Tools.Mastodon;
Expand Down Expand Up @@ -37,10 +38,8 @@ 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();
var json = await response.GetBodyAsJsonObject();
return json.GetPropertyStringValue("id");
}

var mediaTasks = status.MediaFiles.Select(PostMediaFile).ToArray();
Expand Down
4 changes: 1 addition & 3 deletions src/Fallout.Common/Tools/Slack/SlackTasks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ 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 jobject = await response.GetBodyAsJsonObject();
var error = jobject.GetPropertyValueOrNull<string>("error");
Assert.True(error == null, error);

Expand Down
31 changes: 31 additions & 0 deletions src/Fallout.Utilities.Text.Json/JsonObject.GetChildren.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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.Collections.Generic;
using System.Linq;
using System.Text.Json.Nodes;

namespace Fallout.Common.Utilities;

public static partial class JsonNodeExtensions
{
/// <summary>
/// Reads property <paramref name="name"/> as a <see cref="JsonArray"/> and returns its elements typed as <typeparamref name="T"/>.
/// </summary>
public static IEnumerable<T> GetChildren<T>(this JsonObject jsonObject, string name)
where T : JsonNode
{
var array = jsonObject[name] as JsonArray;
return array?.OfType<T>() ?? Enumerable.Empty<T>();
}

/// <summary>
/// Reads property <paramref name="name"/> as a <see cref="JsonArray"/> of <see cref="JsonObject"/> children.
/// </summary>
public static IEnumerable<JsonObject> GetChildren(this JsonObject jsonObject, string name)
{
return jsonObject.GetChildren<JsonObject>(name);
}
}
43 changes: 43 additions & 0 deletions src/Fallout.Utilities.Text.Json/JsonObject.GetPropertyValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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.Text.Json.Nodes;

namespace Fallout.Common.Utilities;

public static partial class JsonNodeExtensions
{
/// <summary>
/// Reads property <paramref name="name"/> as <typeparamref name="T"/>, returning <c>default</c> if absent.
/// </summary>
public static T GetPropertyValueOrNull<T>(this JsonObject jsonObject, string name)
{
return jsonObject[name] is JsonNode node ? node.GetValue<T>() : default;
}

/// <summary>
/// Reads property <paramref name="name"/> as <typeparamref name="T"/>, throwing if absent.
/// </summary>
public static T GetPropertyValue<T>(this JsonObject jsonObject, string name)
{
return jsonObject[name].NotNull($"Property '{name}' not found").GetValue<T>();
}

/// <summary>
/// Reads property <paramref name="name"/> as a nested <see cref="JsonObject"/>, throwing if absent.
/// </summary>
public static JsonObject GetPropertyValue(this JsonObject jsonObject, string name)
{
return jsonObject[name].NotNull($"Property '{name}' not found").AsObject();
}

/// <summary>
/// Reads property <paramref name="name"/> as a string, throwing if absent.
/// </summary>
public static string GetPropertyStringValue(this JsonObject jsonObject, string name)
{
return jsonObject.GetPropertyValue<string>(name);
}
}
Loading