Пример #1
0
        public static ESRFileBody FromJsonString(string json)
        {
            ESRFileBody fileBody = null;

            fileBody = JsonConvert.DeserializeObject <ESRFileBody>(json, new JsonSerializerSettings()
            {
                NullValueHandling              = NullValueHandling.Ignore,
                MissingMemberHandling          = MissingMemberHandling.Ignore,
                TypeNameHandling               = TypeNameHandling.Auto,
                TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Full,
            });

            return(fileBody);
        }
Пример #2
0
        public bool LoadFile()
        {
            int ret = 0;

            using (FileStream fs = File.OpenRead(this.FileName))
            {
                //File Keyword
                byte[] fileKeyword = new byte[ESRFileHeader.ESRFileKeyword.Length];

                ret = fs.Read(fileKeyword, 0, fileKeyword.Length);
                if (ret <= 0 || ret != fileKeyword.Length || Encoding.ASCII.GetString(fileKeyword) != ESRFileHeader.ESRFileKeyword)
                {
                    ALog.Debug($"ESRFile LoadFile Error - File Keyword");
                    return(false);
                }

                //Header
                Array.Clear(this.FileHeaderRaw, 0, this.FileHeaderRaw.Length);
                ret = fs.Read(this.FileHeaderRaw, 0, this.FileHeaderRaw.Length);
                if (ret <= 0 || ret != this.FileHeaderRaw.Length)
                {
                    ALog.Debug($"ESRFile LoadFile Error - Header");
                    return(false);
                }

                try
                {
                    string strHeader = Encoding.UTF8.GetString(this.FileHeaderRaw);
                    this.FileHeader = ESRFileHeader.FromJsonString(strHeader);
                }
                catch (Exception ex)
                {
                    ALog.Debug($"ESRFile LoadFile Error - FileHeader! ({ex.Message})");
                    return(false);
                }

                //Body
                long remainLength = fs.Length - fs.Position;
                if (remainLength <= 0)
                {
                    ALog.Debug($"ESRFile LoadFile Error - Body");
                    return(false);
                }

                byte[] bodyRaw = new byte[remainLength];
                ret = fs.Read(bodyRaw, 0, bodyRaw.Length);
                if (ret <= 0 || ret != bodyRaw.Length)
                {
                    ALog.Debug($"ESRFile LoadFile Error - Body");
                    return(false);
                }

                try
                {
                    string strBody = Encoding.UTF8.GetString(bodyRaw);
                    this.FileBody = ESRFileBody.FromJsonString(strBody);
                }
                catch (Exception ex)
                {
                    ALog.Debug($"ESRFile LoadFile Error - FileBody! ({ex.Message})");
                    return(false);
                }
            }
            return(true);
        }