Advertisement
Guest User

Untitled

a guest
Oct 1st, 2024
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.34 KB | None | 0 0
  1. using Newtonsoft.Json.Linq;
  2. using UnityEngine;
  3.  
  4. public class JSONtil {
  5.  
  6.     public static T Get<T>(JObject def, string key) {
  7.         if (def == null || def[key] == null) {
  8.             throw new System.Exception("Missing required key `" + key + "` in JSON: `" + def + "`");
  9.         }
  10.         return def[key].Value<T>();
  11.     }
  12.  
  13.     public static T OrDefault<T>(JObject def, string key, T defaultVal) {
  14.         if (def == null || def[key] == null) {
  15.             return defaultVal;
  16.         }
  17.         return def[key].Value<T>();
  18.     }
  19.  
  20.     public static Vector3 OrDefault(JObject def, string key, Vector3 defaultVal) {
  21.         if (def[key] == null) {
  22.             return defaultVal;
  23.         }
  24.         return VectorText.Deserialize(def.Value<string>(key));
  25.     }
  26.  
  27.     public static void AddIfNonNegative(JObject def, string key, float val) {
  28.         if (val >= 0) {
  29.             def[key] = val;
  30.         }
  31.     }
  32.  
  33.     public static void AddIfNonEmpty(JObject def, string key, string val){
  34.         if (val != "") {
  35.             def[key] = val;
  36.         }
  37.     }
  38.  
  39.     public static void AddIfTrue(JObject def, string key, bool val) {
  40.         if (val) {
  41.             def[key] = val;
  42.         }
  43.     }
  44.  
  45.     public static void AddIfFalse(JObject def, string key, bool val) {
  46.         if (!val) {
  47.             def[key] = val;
  48.         }
  49.     }
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement