mirror of
https://github.com/GoodOlClint/PSProxmoxVE.git
synced 2026-07-26 07:58:14 +00:00
4a63055c10
Co-authored-by: GoodOlClint <151449+GoodOlClint@users.noreply.github.com> Agent-Logs-Url: https://github.com/GoodOlClint/PSProxmoxVE/sessions/2feecd30-2ba1-40bc-b1b5-c1fcc485a319
61 lines
2.0 KiB
C#
61 lines
2.0 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
namespace PSProxmoxVE.Core.Utilities
|
|
{
|
|
/// <summary>
|
|
/// Provides helper methods for converting Newtonsoft.Json types to native .NET types.
|
|
/// </summary>
|
|
public static class JsonHelper
|
|
{
|
|
/// <summary>
|
|
/// Converts a JToken to a native .NET type recursively.
|
|
/// JObject becomes Dictionary<string, object?>, JArray becomes List<object?>, JValue becomes its primitive value.
|
|
/// </summary>
|
|
public static object? ToNative(JToken? token)
|
|
{
|
|
if (token == null || token.Type == JTokenType.Null)
|
|
return null;
|
|
|
|
switch (token)
|
|
{
|
|
case JObject obj:
|
|
return obj.Properties().ToDictionary(
|
|
p => p.Name,
|
|
p => ToNative(p.Value));
|
|
|
|
case JArray arr:
|
|
return arr.Select(ToNative).ToList();
|
|
|
|
case JValue val:
|
|
return val.Value;
|
|
|
|
default:
|
|
return token.ToString();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts a JObject to a Dictionary<string, object?>.
|
|
/// </summary>
|
|
public static Dictionary<string, object?> ToDictionary(JObject? obj)
|
|
{
|
|
if (obj == null) return new Dictionary<string, object?>();
|
|
return obj.Properties().ToDictionary(
|
|
p => p.Name,
|
|
p => ToNative(p.Value));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts a JArray to a List of Dictionaries.
|
|
/// Each element should be a JObject; non-object elements are skipped.
|
|
/// </summary>
|
|
public static List<Dictionary<string, object?>> ToListOfDictionaries(JArray? arr)
|
|
{
|
|
if (arr == null) return new List<Dictionary<string, object?>>();
|
|
return arr.OfType<JObject>().Select(ToDictionary).ToList();
|
|
}
|
|
}
|
|
}
|