using System.Collections.Generic; using System.Linq; using Newtonsoft.Json.Linq; namespace PSProxmoxVE.Core.Utilities { /// /// Provides helper methods for converting Newtonsoft.Json types to native .NET types. /// public static class JsonHelper { /// /// Converts a JToken to a native .NET type recursively. /// JObject becomes Dictionary<string, object?>, JArray becomes List<object?>, JValue becomes its primitive value. /// 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(); } } /// /// Converts a JObject to a Dictionary<string, object?>. /// public static Dictionary ToDictionary(JObject? obj) { if (obj == null) return new Dictionary(); return obj.Properties().ToDictionary( p => p.Name, p => ToNative(p.Value)); } /// /// Converts a JArray to a List of Dictionaries. /// Each element should be a JObject; non-object elements are skipped. /// public static List> ToListOfDictionaries(JArray? arr) { if (arr == null) return new List>(); return arr.OfType().Select(ToDictionary).ToList(); } } }