using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Windows.Forms;
namespace tool
{
    //json文件操作类  暂时只有读取字段功能 后续可拓展
    class json_file_operation : Singleton<json_file_operation>
    {
        private json_file_operation()
        {
        }
        //初始化--传入json文件路径
        public void json_file_operation_init(string jsonPath="./settings.json")
        {
            this.jsonPath = jsonPath;
            //string jsonArrayText1 = "[{'communication_bind':'127.0.0.1'} ,{'communication_bind':'127.0.0.2'} ]";
            //JArray ja = (JArray)JsonConvert.DeserializeObject(jsonArrayText1);
            //string ja1a = ja[1]["communication_bind"].ToString();
            //Console.WriteLine(ja1a);

        }
        //读取json文件指定字段 返回string
        public JArray read_json_jarray(string key)
        {
            try
            {
                using (System.IO.StreamReader file = System.IO.File.OpenText(jsonPath))
                {
                    try
                    {
                        using (JsonTextReader reader = new JsonTextReader(file))
                        {
                            JObject o = (JObject)JToken.ReadFrom(reader);
                            string jsonArrayText1 = o[key].ToString();
                            JArray ja = (JArray)JsonConvert.DeserializeObject(jsonArrayText1);
                            return ja;
                        }
                    }
                    catch (Exception ex)
                    {
                        string tip = key + "字段不存在!";
                        MessageBox.Show(tip + " "+ ex.StackTrace);
                        return null;

                    }
                }
            }
            catch (Exception ex)
            {
                string tip = jsonPath + "  文件路径不存在！";
                MessageBox.Show(tip + " " + ex.StackTrace);
                return null;
            }

        }
        //读取json文件指定字段 返回string
        public string read_json_string(string key)
        {
            try
            {
                using (System.IO.StreamReader file = System.IO.File.OpenText(jsonPath))
                {
                    try
                    {
                        using (JsonTextReader reader = new JsonTextReader(file))
                        {
                            JObject o = (JObject)JToken.ReadFrom(reader);
                            var value = o[key].ToString();
                            return value;
                        }
                    }
                    catch (Exception ex)
                    {
                        string tip = key + "字段不存在!";
                        MessageBox.Show(tip + " " + ex.StackTrace);
                        return null;

                    }
                }
            }
            catch (Exception ex)
            {
                string tip = jsonPath + "  文件路径不存在！";
                MessageBox.Show(tip + " " + ex.StackTrace);
                return null;
            }

        }
        protected string jsonPath;
    }
}
