Esempio n. 1
0
 internal XTJsonRoot(string path, XTJsonDict jdict, Encoding enc, List <XTJsonComment> doc = null)
     : base(jdict)
 {
     this.m_path     = path;
     this.m_encoding = enc;
     this.m_doc      = doc;
 }
Esempio n. 2
0
        private void WriteDict(XTJsonDict jdict)
        {
            this.m_currWarp += 1;
            bool isWarp = this.m_currWarp <= this.m_warps;

            this.m_tw.Write('{');
            int count = jdict.Count;
            int index = 0;

            foreach (DictItem item in jdict)
            {
                if (isWarp)
                {
                    this.m_tw.Write('\n');
                    this.WriteWarpSpace(this.m_currWarp);
                }
                this.WriteJsonData(item.Key);
                this.m_tw.Write(": ");
                this.WriteJsonData(item.Value);
                if (++index < count)
                {
                    this.m_tw.Write(isWarp ? "," : ", ");
                }
            }
            if (isWarp)
            {
                this.m_tw.Write('\n');
                this.WriteWarpSpace(this.m_currWarp - 1);
            }
            this.m_tw.Write('}');
            this.m_currWarp -= 1;
        }
Esempio n. 3
0
        // ----------------------------------------------------------
        // 指定编码打开一个 JSON 文件
        // 参数:
        //     path    : 打开的 JSON 文件路径
        //     enc     : JSON 文件编码方式
        //     ignorDoc: 是否忽略配置前的注释文档,如果忽略注释文档,则下次保存时,文档将会丢失
        //     isCache : 是否作缓存处理(如果为 true,则下次打开时,不需要解释)
        //               如果要去掉缓存,调用 Purge()
        // 异常:
        //     XTJsonReadIOExcetion
        //     XTJsonEmptyException
        //     XTJsonInvalidSourceException
        // ----------------------------------------------------------
        public static XTJsonRoot Open(string path, Encoding enc, bool ignorDoc = true, bool isCache = true)
        {
            path = ExPath.NormalizePath(path);
            if (sm_caches.ContainsKey(path))
            {
                return(sm_caches[path]);
            }

            XTJsonDict           jdict = null;
            List <XTJsonComment> doc   = null;

            if (!ignorDoc)
            {
                doc = new List <XTJsonComment>();
            }
            FileStream   fs = null;
            StreamReader sr = null;

            try
            {
                fs = new FileStream(path, FileMode.OpenOrCreate);
                sr = new StreamReader(fs, enc);
            }
            catch (Exception ex)
            {
                throw new XTJsonReadIOExcetion(ex, path);
            }
            try
            {
                new XTJsonReader(path, sr).Parse(out jdict, doc);
            }
            catch (XTJsonEmptyException)
            {
                return(new XTJsonRoot(path, enc));
            }
            catch (XTJsonParseException)
            {
                throw;
            }
            finally
            {
                if (sr != null)
                {
                    sr.Close();
                }
                if (fs != null)
                {
                    fs.Close();
                }
            }
            XTJsonRoot jroot = new XTJsonRoot(path, jdict, enc, doc);

            if (isCache)
            {
                sm_caches[path] = jroot;
            }
            return(jroot);
        }
Esempio n. 4
0
 internal XTJsonDict(XTJsonDict dict)
 {
     if (dict == null)
     {
         this.m_datas = new InnerDict();
     }
     else
     {
         this.m_datas = dict.m_datas;
     }
 }
Esempio n. 5
0
        public void Parse(out XTJsonDict jdict, List <XTJsonComment> doc = null)
        {
            this.EmptyCheck();
            XTJsonCommentParser.Parse(this, doc);
            XTJsonData dict = XTJsonDictParser.Parse(this);

            if (dict == null)
            {
                this.RaiseInvalidException();
            }
            jdict = (XTJsonDict)dict;
        }
Esempio n. 6
0
        // ----------------------------------------------------------
        // 保存一个 JSON 文件
        // 参数:
        //     path   : 要保存的路径
        //     jdict  : JSON 字典
        //     enc    : 写出 JSON 文件的编码格式
        //     warps  : 表示字典的自动换行层级数
        //     doc    : JSON 文件的开始注释文档(不需要带注释符号)
        //     isCache: 是否放到缓存
        // 异常:
        //     XTJsonWriteIOExcetion: XTJsonException
        //     XTJsonDataException: XTJsonException
        // ----------------------------------------------------------
        internal static void Write(string path, XTJsonDict jdict, Encoding enc,
                                   uint warps = 1, List <XTJsonComment> doc = null, bool isCache = true)
        {
            FileStream   fs = null;
            StreamWriter sw = null;

            try
            {
                fs = new FileStream(path, FileMode.Create);
                sw = new StreamWriter(fs, enc);
                (new XTJsonWriter(sw, warps)).Write(jdict, doc);
            }
            catch (XTJsonException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new XTJsonWriteIOExcetion(ex, path);
            }
            finally
            {
                if (sw != null)
                {
                    sw.Close();
                }
                if (fs != null)
                {
                    fs.Close();
                }
            }
            if (isCache)
            {
                Purge(path);
                sm_caches[ExPath.NormalizePath(path)] = new XTJsonRoot(path, jdict, enc, doc);
            }
        }
Esempio n. 7
0
 // ---------------------------------------------------------------
 // public
 // ---------------------------------------------------------------
 public void Write(XTJsonDict jdict, List <XTJsonComment> doc)
 {
     this.WriteDoc(doc);
     this.WriteDict(jdict);
 }
Esempio n. 8
0
 // ----------------------------------------------------------
 // 以 UTF8 的格式保存一个 JSON 文件
 // 参数:
 //     path   : 要保存的路径
 //     jdict  : JSON 字典
 //     warps  : 表示字典的自动换行层级数
 //     doc    : JSON 文件的开始注释文档(不需要带注释符号)
 //     isCache: 是否放到缓存
 // 异常:
 //     XTJsonWriteIOExcetion: XTJsonException
 //     XTJsonDataException: XTJsonException
 // ----------------------------------------------------------
 internal static void Write(string path, XTJsonDict jdict,
                            uint warps = 1, List <XTJsonComment> doc = null, bool isCache = true)
 {
     Write(path, jdict, Encoding.UTF8, warps, doc, isCache);
 }