Example #1
0
    public void Save()
    {
//        Debug.Log("SAVE");

        List <string> dataClassKeys = new List <string>(allDataDict.Keys);//List of All Keys of Dictionary

        List <CountryData> tempDataClassList = new List <CountryData>();

        //adding all the keys into the temp list
        foreach (string key in dataClassKeys)
        {
            tempDataClassList.Add(allDataDict[key]);
        }

        AllDataClass listClassObj = new AllDataClass()
        {
            structList = tempDataClassList
        };

        //creating a file for saving purpose
        FileStream file = File.Create(filePath);

        //creating JSON from customised object(allDataClass
        string json = JsonUtility.ToJson(listClassObj);

        file.Dispose();
        //String the JSON data to the file accessing by file path
        File.WriteAllText(filePath, json);
        file.Close();
    }
Example #2
0
    public void Load()
    {
//		Debug.Log("LOAD_  " + filePath);

        //alldataClass is list of objects which I stored
        //If not initiated creating a new object
        if (allDataClassObject == null)
        {
            allDataClassObject = new AllDataClass();
        }
//        else
//        {
//			//clearing the dataClass list for reuse
//            if (allDataClassObject.structList != null)
//            {
//                allDataClassObject.structList.Clear();
//            }
//        }

        //clearing the dictionary for fresh use
        allDataDict.Clear();

        //Checking the existance for previuos file
        if (!File.Exists(filePath))
        {
            Debug.Log("No Previous session");
            return;
        }

        //reading the data from JSON file in string format
        string json = File.ReadAllText(filePath);

        allDataClassObject = (AllDataClass)JsonUtility.FromJson(json, typeof(AllDataClass));

        //putting the dictionary data to newly created list
        foreach (CountryData cd in allDataClassObject.structList)
        {
            allDataDict.Add(cd._Cntname, cd);// using title as key
        }
    }