스토리지

[3.25] json 파일을 싱글턴 패턴으로 읽어오기 본문

Unity/수업내용(C#)

[3.25] json 파일을 싱글턴 패턴으로 읽어오기

ljw4104 2021. 3. 25. 12:19

song_data.json
background_data.json

 

 

using System;

namespace Study11
{
    public class SongData
    {
        public int songId;
        public string songName;
        public string composer;
        public int pastDifficulty;
        public int presentDifficulty;
        public float futureDifficulty;
        public bool haveBeyond;
        public float beyondDifficulty;
        public int backgroundId;

        public override string ToString()
        {
            //문자열 보간 사용
            return String.Format("{0 ,-2} {1, -28} {2, -35} {3} {4, 3} {5, 5} {6, 5} {7, 5} {8, 20}",
                this.songId, this.songName, this.composer, this.pastDifficulty, 
                this.presentDifficulty, this.futureDifficulty, this.haveBeyond, 
                this.beyondDifficulty, DataManager.GetInstance().dicBackgroundData[this.backgroundId]);
        }
    }
}

 

using System;

namespace Study11
{
    public class BackgroundData
    {
        public int backgroundId;
        public string backgroundName;

        public override string ToString()
        {
            return String.Format("{0}",this.backgroundName);
        }
    }
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json;

namespace Study11
{
    public class DataManager
    {
        private static DataManager instance;
        public Dictionary<int, SongData> dicSongData;
        public Dictionary<int, BackgroundData> dicBackgroundData;

        private DataManager()
        {

        }

        public static DataManager GetInstance()
        {
            if (DataManager.instance == null)
            {
                DataManager.instance = new DataManager();
            }
            return DataManager.instance;
        }

        public void LoadSongData(string path)
        {
            var json = File.ReadAllText(path);
            var arr = JsonConvert.DeserializeObject<SongData[]>(json);
            this.dicSongData = arr.ToDictionary(x => x.songId);
        }

        public void LoadBackgroundData(string path)
        {
            var json = File.ReadAllText(path);
            var arr = JsonConvert.DeserializeObject<BackgroundData[]>(json);
            this.dicBackgroundData = arr.ToDictionary(x => x.backgroundId);
        }

        public void PrintData()
        {
            Console.WriteLine("Data Num : {0}\n", dicSongData.Count);
            foreach(var item in dicSongData)
            {
                Console.WriteLine(item.Value);
            }
            Console.WriteLine();
        }
    }
}
using System;

namespace Study11
{
    public class App
    {
        public App()
        {
            string path = "./song_data.json";
            DataManager.GetInstance().LoadSongData(path);

            path = "./background_data.json";
            DataManager.GetInstance().LoadBackgroundData(path);

            DataManager.GetInstance().PrintData();
        }
    }
}
Comments