数据持久化(保存Json文件)---Newtonsoft Json

Newtonsoft Json 库

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;
using System.Text;
using GFW;
using Newtonsoft.Json;

//public class UserConfigData
//{
//    public Dictionary configDataDic = new Dictionary();
//}
public class UserPersonData
{
    public string curUserID;
    public Dictionary curUserDataDic = new Dictionary();
}

public class GSetting {
    static string curUserID = "data";
    static string _saveDataPath = Application.dataPath + "/../WriteablePath/json/_current_userdata.json";
    // ----SetConfig
    public static void SetValue(string dataKey, object dataValue)
    {
        if (string.IsNullOrEmpty(dataKey))
        {
            Debug.Log(" is null !");
            return;
        }
        if (RefreshData(dataKey,dataValue))
        {
            return;
        }
        UserPersonData _newUserConfigData = new UserPersonData();
        _newUserConfigData.curUserDataDic[dataKey] = dataValue;

        string _saveData = JsonConvert.SerializeObject(_newUserConfigData);
        if (!string.IsNullOrEmpty(_saveData))
        {
            _saveData = _saveData + "\r\n";
            GFileUtil.CreateDir(GFileUtil.GetDirPath(_saveDataPath));
            File.AppendAllText(_saveDataPath, _saveData, Encoding.UTF8);
            Debug.Log(" Save Success !");
        }
    }
    public static object GetValue(string dataKey, object dataValue = null)
    {
        if (GFileUtil.IsFileExists(_saveDataPath))
        {
            try
            {
                StreamReader _readData = new StreamReader(_saveDataPath);
                string _tempData = _readData.ReadLine();
                while (_tempData != null)
                {
                    UserPersonData _jsonData = JsonConvert.DeserializeObject(_tempData);
                    if (_jsonData.curUserDataDic.ContainsKey(dataKey))
                    {
                        dataValue = _jsonData.curUserDataDic[dataKey];
                        break;
                    }
                    _tempData = _readData.ReadLine();
                }
                _readData.Close();
            }
            catch (Exception e)
            {
                Debug.Log(e.ToString());
            }
        }
        return dataValue;
    }
    // ----SetUserData
    public static void SetUserData(string dataKey, object dataValue)
    {
        if (string.IsNullOrEmpty(dataKey))
        {
            Debug.Log(" null ");
            return;
        }
        if (RefreshData(dataKey, dataValue))
        {
            return;
        }
        UserPersonData _newUserPersonData = new UserPersonData();
        _newUserPersonData.curUserID = curUserID;
        _newUserPersonData.curUserDataDic[dataKey] = dataValue;
        string _saveData = JsonConvert.SerializeObject(_newUserPersonData);
        if (!string.IsNullOrEmpty(_saveData))
        {
            _saveData = _saveData + "\r\n";
            GFileUtil.CreateDir(GFileUtil.GetDirPath(_saveDataPath));
            File.AppendAllText(_saveDataPath, _saveData, Encoding.UTF8);
            Debug.Log(" Save Success !");
        }
    }
    public static object GetUserData(string dataKey, object dataValue = null)
    {
        if (GFileUtil.IsFileExists(_saveDataPath))
        {
            try
            {
                StreamReader _readData = new StreamReader(_saveDataPath);
                string _tempData = _readData.ReadLine();
                while (_tempData != null)
                {
                    UserPersonData _jsonData = JsonConvert.DeserializeObject(_tempData);
                    if (_jsonData.curUserID == curUserID && _jsonData.curUserDataDic.ContainsKey(dataKey))
                    {
                        dataValue = _jsonData.curUserDataDic[dataKey];
                        break;
                    }
                    _tempData = _readData.ReadLine();
                }
                _readData.Close();
            }
            catch (Exception e)
            {
                Debug.Log(e.ToString());
            }
        }
        return dataValue;
    }
    // ----Refresh
    public static bool RefreshData(string dataKey, object dataValue)
    {
        List _userPersonDataList = new List();
        if (GFileUtil.IsFileExists(_saveDataPath))
        {
            try
            {
                StreamReader _readData = new StreamReader(_saveDataPath);
                string _tempData = _readData.ReadLine();
                while (_tempData != null)
                {
                    UserPersonData _jsonData = JsonConvert.DeserializeObject(_tempData);
                    _userPersonDataList.Add(_jsonData);
                    _tempData = _readData.ReadLine();
                }
                _readData.Close();
            }
            catch (Exception e)
            {
                Debug.Log(e.ToString());
            }
            string refContent = "";
            bool isRepet = false;
            foreach (UserPersonData item in _userPersonDataList)
            {
                string _tempTxt = "";
                _tempTxt = JsonConvert.SerializeObject(item);
                if (item.curUserID == null && item.curUserDataDic.ContainsKey(dataKey))
                {
                    UserPersonData _newData = new UserPersonData();
                    _newData.curUserDataDic[dataKey] = dataValue;
                    _tempTxt = JsonConvert.SerializeObject(_newData);
                    isRepet = true;
                }
                else if (item.curUserID == curUserID && item.curUserDataDic.ContainsKey(dataKey))
                {
                    UserPersonData _newData = new UserPersonData();
                    _newData.curUserID = curUserID;
                    _newData.curUserDataDic[dataKey] = dataValue;
                    _tempTxt = JsonConvert.SerializeObject(_newData);
                    isRepet = true;
                }
                refContent += _tempTxt + "\r\n";
            }
            if (!string.IsNullOrEmpty(refContent) && isRepet)
            {
                GFileUtil.CreateDir(GFileUtil.GetDirPath(_saveDataPath));
                File.WriteAllText(_saveDataPath, refContent, Encoding.UTF8);
                Debug.Log(" Refresh Success !");
                return true;
            }
        }
        return false;
    }
    // ----DeleteAll
    public static void DeleteAll()
    {
        if (GFileUtil.IsFileExists(_saveDataPath))
        {
            string deleteData = "";
            File.WriteAllText(_saveDataPath, deleteData, Encoding.UTF8);
        }
    }
    // ==------------------------------------
    
}


你可能感兴趣的:(C#)