C# JsonHelper类

记录操作Json的方法

class JsonHelper
    {
        /// 将对象序列化为json文件
        /// 
        /// 类型
        /// 实例
        /// 存放路径
        public static void ObjectToJson<T>(T t, string path) where T : class
        {
            DataContractJsonSerializer formatter = new DataContractJsonSerializer(typeof(T));
            using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate))
            {
                formatter.WriteObject(stream, t);
            }
        }

        /// 
        /// json字符串转成对象
        /// 
        /// 类型
        /// json格式字符串
        /// 对象
        public static T JsonToObject<T>(T t, string path) where T : class
        {
            DataContractJsonSerializer formatter = new DataContractJsonSerializer(typeof(T));
            using (FileStream stream = new FileStream(path, FileMode.Open))
            {
                T result = formatter.ReadObject(stream) as T;
                return result;
            }
        }

        /// 
        /// 读取文件
        /// 
        /// 
        /// 
        private static string GetFileContent(string filePath)
        {
            string result = "";
            if (File.Exists(filePath))
            {
                result = File.ReadAllText(filePath);
            }
            return result;
        }

        /// 
        /// 反序列化
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T Deserialize<T>(T t, string path)
        {
            string fileContent = GetFileContent(path);
            if (!string.IsNullOrWhiteSpace(fileContent))
            {
                //反序列化
                t = JsonConvert.DeserializeObject<T>(fileContent);
            }
            return t;
        }

        /// 
        /// 读取JSON文件
        /// 
        /// 
        /// 
        public static T ReadJSON<T>(string path)
        {
            try
            {
                StreamReader streamReader = new StreamReader(path);
                dynamic jsonObj = JsonConvert.DeserializeObject<dynamic>(streamReader.ReadToEnd());
                streamReader.Close();
                return (T)jsonObj;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return default;
        }

        /// 
        /// 写入JSON文件
        /// 
        /// 
        /// 
        /// 
        public static void WriteJSON<T>(string path, T value)
        {
            try
            {
                //序列化为json时过滤掉NULL
                var jsonSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
                string output = JsonConvert.SerializeObject(value, Formatting.Indented, jsonSetting);
                File.WriteAllText(path, output);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// 
        /// 修改JSON文件
        /// 
        /// 
        /// 
        /// 
        /// 
        public static void EditJSON<T>(string path, string key, T value)
        {
            try
            {
                //序列化为json时过滤掉NULL
                var jsonSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
                //读取文件
                StreamReader streamReader = new StreamReader(path);
                dynamic jsonObj = JsonConvert.DeserializeObject<dynamic>(streamReader.ReadToEnd());
                //修改值
                jsonObj[key] = value;
                streamReader.Close();
                string output = JsonConvert.SerializeObject(jsonObj, Formatting.Indented, jsonSetting);
                File.WriteAllText(path, output);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }

你可能感兴趣的:(笔记,c#,json)