public static List <T> GetListFromFilepath <T>(string filepath)
    {
        List <T> output       = new List <T>();
        string   contentsJson = "";

        try {
            StreamReader reader = new StreamReader(filepath, Encoding.Default);
            using (reader) {
                contentsJson = reader.ReadToEnd();
                reader.Close();
            }
        } catch (IOException e) {
            Debug.LogError(e.Message);
        } catch (OutOfMemoryException e) {
            Debug.LogError(e.Message);
        } catch (Exception e) {
            Debug.LogError(e.Message);
        }

        T[] contentArray = JsonArrayHelper.FromJson <T>(contentsJson);
        foreach (T obj in contentArray)
        {
            output.Add(obj);
        }

        return(output);
    }
    // TODO: Consider moving this to JsonListHelper or other utility class
    private void ReadAndConstructDictionary()
    {
        languageDictionary = new Dictionary <string, string>();
        string rawJson  = "";
        string filepath = Application.dataPath + folderPath + "test_" + language + ".json";

        try {
            StreamReader reader = new StreamReader(filepath, Encoding.Default);
            using (reader) {
                rawJson = reader.ReadToEnd();
                reader.Close();
            }
        } catch (IOException e) {
            Debug.LogError(e.Message);
        } catch (OutOfMemoryException e) {
            Debug.LogError(e.Message);
        } catch (Exception e) {
            Debug.LogError(e.Message);
        }

        TranslatedString[] arrayContents = JsonArrayHelper.FromJson <TranslatedString>(rawJson);
        foreach (TranslatedString pair in arrayContents)
        {
            languageDictionary.Add(pair.key, pair.value);
        }
    }
    public static string ToJson <T>(T[][] array, bool prettyPrint)
    {
        string[] rows = new string[array.GetLength(0)];
        for (int i = 0; i < array.GetLength(0); i++)
        {
            rows[i] = JsonArrayHelper.ToJson(array[i], prettyPrint);
        }

        return(JsonArrayHelper.ToJson(rows, prettyPrint));
    }
示例#4
0
    public static List <GameListResource> GetStagedGames()
    {
        string       reply;
        const string urlSuffix = GAME_SUFFIX + "/staged";
        var          code      = Get(urlSuffix, out reply);

        HandleReturnCode(code);

        if (code == 204)
        {        //HTTP code for "No Content"
            return(new List <GameListResource>(0));
        }

        Debug.Log(reply);
        return(JsonArrayHelper.getJsonList <GameListResource>(reply));
    }
    public static T[][] FromJson <T>(string json)
    {
        string[] rows   = JsonArrayHelper.FromJson <string>(json);
        T[][]    result = new T[rows.GetLength(0)][];

        for (int i = 0; i < rows.GetLength(0); i++)
        {
            Debug.Log("JSON row(" + i + "): " + rows[i]);

            result[i] = JsonArrayHelper.FromJson <T>(rows[i]);

            Debug.Log("Deserialized row(" + i + "): " + result[i]);
        }

        return(result);
    }
示例#6
0
    private void LoadColors()
    {
        string response = Rest.GetColors();

        Debug.Log(response);
        List <CustomColor> customColors = JsonArrayHelper.getJsonList <CustomColor>(response);

        if (ColorNames == null || ColorNames.Count > 0)
        {
            ColorNames = new Dictionary <string, string>();
        }

        foreach (var customColor in customColors)
        {
            ColorNames.Add(customColor.color, customColor.name);
        }
    }