123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- 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
- {
- /// <summary>
- /// Json数据编辑
- /// </summary>
- 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;
- }
- /// <summary>
- /// 创建一个Json类型的数据
- /// </summary>
- /// <param name="tableName"></param>
- /// <returns></returns>
- public bool Create(string tableName)
- {
- return false;
- }
- public bool DeleteAllData<T>()
- {
- throw new System.NotImplementedException();
- }
- public bool DeleteData<T>(string key)
- {
- throw new System.NotImplementedException();
- }
- public List<T> FindAllData<T>()
- {
- throw new System.NotImplementedException();
- }
- /// <summary>
- /// 查找所有对象
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <returns></returns>
- public List<T> FindAllData<T>(string data)
- {
- return null;
- }
- public List<T> FindAllData<T>(byte[] data)
- {
- throw new NotImplementedException();
- }
- public T FindData<T>(string key)
- {
- throw new System.NotImplementedException();
- }
- public bool InsertData<T>(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>(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>(T data)
- {
- return false;
- }
- public bool UpdateData<T>(T data, string key)
- {
- throw new NotImplementedException();
- }
- }
- }
|