Deukhoofd 810cdbb15a
All checks were successful
continuous-integration/drone/push Build is passing
Move data and data loading to plugin libraries.
2025-05-16 13:01:23 +02:00

38 lines
1.4 KiB
C#

using System.Text.Json;
using System.Text.Json.Nodes;
using PkmnLib.Static.Utils;
namespace PkmnLib.Dynamic.Libraries.DataLoaders;
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();
}
}
}