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"); //if (!token.HasValues) throw new MalformedJsonException(json, key, "has no value"); 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(key); } public static int GetIntValue(this JObject json, string key) { AssertTokenType(json, key, JTokenType.Integer); return json.Value(key); } public static float GetFloatValue(this JObject json, string key) { AssertTokenType(json, key, JTokenType.Float); return json.Value(key); } public static JObject GetObject(this JObject json, string key) { AssertTokenType(json, key, JTokenType.Object); return json.Value(key)!; } public static string GetStringValue(this JObject json, string key) { AssertTokenType(json, key, JTokenType.String); return json.Value(key)!; } }