JsonVersion
using UnityEngine; using System; using System.IO; using System.Text; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Serialization; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; [Serializable] public class Data { public Inner iNner; public Inner ind; } [Serializable] public class Inner { public int level; public int hp; } public class Test : MonoBehaviour { // Use this for initialization void Start () { Data testData = new Data(); //Inner inner = new Inner(); //inner.level = 3; //inner.hp = 888; //testData.iNner = inner; //오브젝트 암호화 저장 //obj_encrypt_Save(testData, Application.dataPath + "/En/","En_Json.txt"); //암호화 저장된 데이터 오브젝트에 Deserialize!! //decrypt_load(testData, Application.dataPath + "/En/", "En_Json"); //Debug.Log("testData" + testData.iNner.hp); //복호화가 잘 되었는지 파일로 내려서 확인 //CreateJsonFile(Application.dataPath, "복호화확인" , ObjectToJson(testData)); } //오브젝트를 암호화 하여 text로 저장 public void obj_encrypt_Save(object obj,string filePath,string fileName) { string json = ObjectToJson(obj); string enc = DataEncryptDecrypt.encrystringData(json); CreateJsonFile(filePath, fileName, enc); } //text를 불러와 복호화 후 오브젝트에 Deserailize public void decrypt_load<T>(T obj, string En_filePath, string fileName) { FileStream filestream = new FileStream(string.Format("{0}/{1}.json", En_filePath, fileName), FileMode.Open); byte[] data = new byte[filestream.Length]; filestream.Read(data, 0, data.Length); filestream.Close(); string jsonData = Encoding.UTF8.GetString(data); string de_data = DataEncryptDecrypt.Decrypt(jsonData); JsonUtility.FromJsonOverwrite(de_data, obj); } string ObjectToJson(object obj) { return JsonUtility.ToJson(obj); } T JsonToObject<T>(string jsonData) { return JsonUtility.FromJson<T>(jsonData); } void CreateJsonFile(string createPath,string fileName,string jsonData) { FileStream fileStream = new FileStream(string.Format("{0}/{1}.json", createPath, fileName), FileMode.CreateNew); byte[] data = Encoding.UTF8.GetBytes(jsonData); fileStream.Write(data, 0, data.Length); fileStream.Close(); } //T LoadJsonFile<T>(string loadPath, string fileName) //{ // FileStream filestream = new FileStream(string.Format("{0}/{1}.json", loadPath, fileName), FileMode.Open); // byte[] data = new byte[filestream.Length]; // filestream.Read(data, 0, data.Length); // filestream.Close(); // string jsonData = Encoding.UTF8.GetString(data); // return JsonUtility.FromJson<T>(jsonData); //} }
댓글 없음:
댓글 쓰기