Esempio n. 1
0
        public T Deserialize <T>(TextReader stream, out SerializationResult Results) where T : new()
        {
            T Object = new T();

            Results = new SerializationResult();
            var data = ObjectInspector.ReadObject <T>(Object, Options);

            ObjectReadComplete?.Invoke(this, data);

            int maxFieldLen = data.Max(o => o.Length);

            if (FieldSeparator != null)
            {
                maxFieldLen = Math.Max(maxFieldLen, FieldSeparator.Length);
            }
            char[] buffer = new char[maxFieldLen];

            for (int i = 0; i < data.Length; i++)
            {
                var val = data[i];
                try
                {
                    int len   = stream.ReadBlock(buffer, 0, val.Length);
                    var block = new string(buffer, 0, len);

                    if (val.Formatter.Deserialize(block, val))
                    {
                        typeof(T).GetProperty(val.Name).SetValue(Object, val.Object);
                    }
                    else
                    {
                        Results.Errors.Add(new Exception("Failed to deserialize field " + val.Name));
                    }

                    bool checkSeparator = !string.IsNullOrEmpty(FieldSeparator);
                    if (!LastFieldHadSeparator && i == data.Length - 1)
                    {
                        checkSeparator = false;
                    }

                    if (checkSeparator)
                    {
                        stream.ReadBlock(buffer, 0, FieldSeparator.Length);
                        block = new String(buffer, 0, FieldSeparator.Length);

                        if (block != FieldSeparator)
                        {
                            Results.Errors.Add(new Exception("Missing field separator" + val.Name));
                        }
                    }
                }
                catch (Exception ex)
                {
                    Results.Errors.Add(new Exception("Error serializing field " + val.Name, ex));
                }
            }
            return(Object);
        }
        public T Deserialize <T>(string line, out SerializationResult Results) where T : new()
        {
            var bs = new BlockSerializer(Options);

            if (ObjectReadComplete != null)
            {
                bs.ObjectReadComplete += ObjectReadComplete;
            }

            var regSize = ObjectInspector.GetRegistrySizeAttribute <T>();

            if (regSize != null)
            {
                if (line.Length != regSize.Length)
                {
                    throw new InvalidOperationException($"Deserialization cancelled due to Length mismatch. Line-Lenght: {line.Length} Expected: {regSize.Length}");
                }
            }

            return(bs.Deserialize <T>(new StringReader(line), out Results));
        }
        public T Deserialize <T>(StreamReader stream, out SerializationResult Results) where T : new()
        {
            var line = stream.ReadLine();

            return(Deserialize <T>(line, out Results));
        }