Move data and data loading to plugin libraries.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
38
PkmnLib.Dynamic/Libraries/DataLoaders/JsonParameterLoader.cs
Normal file
38
PkmnLib.Dynamic/Libraries/DataLoaders/JsonParameterLoader.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user