using System; using UnityEngine; public static class JsonHelper { /// <summary> /// Collect the array/list of objects in a single array Json /// </summary> /// <typeparam name="T">Should be a Primative, Non-Primatives have not been tested</typeparam> [Serializable] private class Wrapper<T> { public T[] Gifts; } /// <summary> /// Retrieves the items from a Json file /// </summary> /// <typeparam name="T">Should be a Primative, Non-Primatives have not been tested</typeparam> /// <param name="jsonPath">Path to the specific Json file</param> /// <returns>Collection of objects in an array within a Wrapper class</returns> public static T[] FromJson<T>(string jsonPath) { Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(jsonPath); return wrapper.Gifts; } /// <summary> /// Turns an array of Primatives into a Json file /// </summary> /// <typeparam name="T">Should be a Primative, Non-Primatives have not been tested</typeparam> /// <param name="array">The array of primatives</param> /// <param name="prettyPrint">Default is no manipulation of string, True leads to formatting of string</param> /// <returns>String representation of the Json file</returns> public static string ToJson<T>(T[] array, bool prettyPrint = false) { Wrapper<T> gift_Wrapper = new Wrapper<T>(); gift_Wrapper.Gifts = array; return JsonUtility.ToJson(gift_Wrapper, prettyPrint); } }