Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Newtonsoft.Json.Linq;
- using UnityEngine;
- public class JSONtil {
- public static T Get<T>(JObject def, string key) {
- if (def == null || def[key] == null) {
- throw new System.Exception("Missing required key `" + key + "` in JSON: `" + def + "`");
- }
- return def[key].Value<T>();
- }
- public static T OrDefault<T>(JObject def, string key, T defaultVal) {
- if (def == null || def[key] == null) {
- return defaultVal;
- }
- return def[key].Value<T>();
- }
- public static Vector3 OrDefault(JObject def, string key, Vector3 defaultVal) {
- if (def[key] == null) {
- return defaultVal;
- }
- return VectorText.Deserialize(def.Value<string>(key));
- }
- public static void AddIfNonNegative(JObject def, string key, float val) {
- if (val >= 0) {
- def[key] = val;
- }
- }
- public static void AddIfNonEmpty(JObject def, string key, string val){
- if (val != "") {
- def[key] = val;
- }
- }
- public static void AddIfTrue(JObject def, string key, bool val) {
- if (val) {
- def[key] = val;
- }
- }
- public static void AddIfFalse(JObject def, string key, bool val) {
- if (!val) {
- def[key] = val;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement