using System; using System.Linq; using System.Text.Json; using System.Text.Json.Nodes; using PkmnLib.Static.Utils; namespace PkmnLib.Dataloader; internal static class JsonParameterLoader { internal static object? ToParameter(this JsonNode node) { switch (node.GetValueKind()) { case JsonValueKind.Undefined: throw new InvalidOperationException("Undefined value."); case JsonValueKind.Object: return node.AsObject().ToDictionary(x => (StringKey)x.Key, x => x.Value?.ToParameter()); case JsonValueKind.Array: return node.AsArray().Select(x => x?.ToParameter()).ToList(); case JsonValueKind.String: return node.GetValue<string>(); case JsonValueKind.Number: var element = node.GetValue<JsonElement>(); if (element.TryGetInt32(out var v)) return v; if (element.TryGetSingle(out var f)) return f; throw new InvalidOperationException("Number is not an integer or a float."); case JsonValueKind.True: return true; case JsonValueKind.False: return false; case JsonValueKind.Null: return null; default: throw new ArgumentOutOfRangeException(); } } }