using Newtonsoft.Json; using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using UnityEngine; using XRTool.Util; namespace PublicTools.XMLDataBase { /// /// Json数据编辑 /// public class JsonDataEditor : TableInterface { private string filePath; private FileStream fileStream; private StreamReader reader; private StreamWriter writer; public JsonDataEditor() { } public JsonDataEditor(string path) { this.filePath = path; } public void Close() { if (writer != null) { writer.Flush(); writer.Close(); } if (reader != null) { reader.Close(); } if (fileStream != null) { fileStream.Close(); } fileStream = null; reader = null; writer = null; filePath = null; } /// /// 创建一个Json类型的数据 /// /// /// public bool Create(string tableName) { return false; } public bool DeleteAllData() { throw new System.NotImplementedException(); } public bool DeleteData(string key) { throw new System.NotImplementedException(); } public List FindAllData() { throw new System.NotImplementedException(); } /// /// 查找所有对象 /// /// /// public List FindAllData(string data) { return null; } public List FindAllData(byte[] data) { throw new NotImplementedException(); } public T FindData(string key) { throw new System.NotImplementedException(); } public bool InsertData(T t) { string data = JsonConvert.SerializeObject(t); string prikey = data.Split(',')[0]; string jData = null; while (!string.IsNullOrEmpty(jData = reader.ReadLine())) { string tmpKey = jData.Split(',')[0]; if (tmpKey == prikey) { return UpdateData(t); } } writer.WriteLine(data); return false; } public bool InsertData(T data, string priKey) { throw new NotImplementedException(); } public bool Open() { return Open(filePath); } public bool Open(string path) { Close(); if (File.Exists(path)) { try { fileStream = new FileStream(path, FileMode.Open); reader = new StreamReader(fileStream, UnicodeEncoding.Default); writer = new StreamWriter(fileStream, UnicodeEncoding.Default); return fileStream.CanRead; } catch (Exception ex) { UnityLog.Instance.LogException(ex); UnityLog.Instance.LogError(path + ex.ToString()); } } return false; } public void Save() { throw new NotImplementedException(); } public bool UpdateData(T data) { return false; } public bool UpdateData(T data, string key) { throw new NotImplementedException(); } } }