You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
1.1 KiB
32 lines
1.1 KiB
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<long>(key);
|
|
}
|
|
|
|
public static float GetFloatValue(this JObject json, string key)
|
|
{
|
|
AssertTokenType(json, key, JTokenType.Float);
|
|
return json.Value<float>(key);
|
|
}
|
|
|
|
public static string GetStringValue(this JObject json, string key)
|
|
{
|
|
AssertTokenType(json, key, JTokenType.String);
|
|
return json.Value<string>(key)!;
|
|
}
|
|
} |