Files
Babushka/scripts/CSharp/GameEntity/LoadSave/EntityLoadSaveUtil.cs
T

44 lines
1.4 KiB
C#
Raw Normal View History

2025-12-18 14:27:27 +01:00
using Newtonsoft.Json.Linq;
namespace Babushka.scripts.CSharp.GameEntity.LoadSave;
public static class EntityLoadSaveUtil
{
private static void AssertTokenType(this JObject json, string key, JTokenType type)
{
var token = json[key];
if (token == null) throw new MalformedJsonException(json, key, "does not exist");
2026-02-03 17:30:35 +01:00
//if (!token.HasValues) throw new MalformedJsonException(json, key, "has no value");
2025-12-18 14:27:27 +01:00
if (token.Type != type) throw new MalformedJsonException(json, key, $"is not of type {type}");
}
public static long GetLongValue(this JObject json, string key)
{
AssertTokenType(json, key, JTokenType.Integer);
return json.Value<long>(key);
}
2026-01-29 17:42:32 +01:00
public static int GetIntValue(this JObject json, string key)
{
AssertTokenType(json, key, JTokenType.Integer);
return json.Value<int>(key);
}
2025-12-18 14:27:27 +01:00
public static float GetFloatValue(this JObject json, string key)
{
AssertTokenType(json, key, JTokenType.Float);
return json.Value<float>(key);
}
2026-01-29 17:42:32 +01:00
public static JObject GetObject(this JObject json, string key)
{
AssertTokenType(json, key, JTokenType.Object);
return json.Value<JObject>(key)!;
}
2025-12-18 14:27:27 +01:00
public static string GetStringValue(this JObject json, string key)
{
AssertTokenType(json, key, JTokenType.String);
return json.Value<string>(key)!;
}
}