コード例 #1
0
        void WriteValueContents(Type knownType, object value)
        {
            if (value == null)
            {
                CurrentGroup.Add(new XAttribute("type", "null"));
            }
            else
            {
                Type type = value.GetType();

                if (type.DerivesFrom <Type>())
                {
                    // Treat RuntimeType as if it's Type
                    type = typeof(Type);
                }

                var serializer = SerializerRegistry.GetSerializer(type);
                serializer.SerializeConstructor(ref value, this);
                serializer.SerializeContents(ref value, this);
                serializer.SerializeBacklinks(ref value, this);

                if (type != knownType)
                {
                    CurrentGroup.Add(new XAttribute("type", SerializerRegistry.GetSerializedTypeName(type)));
                }
            }
        }
コード例 #2
0
        object ReadValueContents(Type knownType)
        {
            int typeIndex = _binaryReader.ReadInt32();

            if (typeIndex == -1)
            {
                return(null);
            }

            Type type;

            if (typeIndex == -2)
            {
                type = knownType;
            }
            else
            {
                type = SerializerRegistry.ReadSerializedTypeId(typeIndex, _binaryReader);
            }

            var serializer = SerializerRegistry.GetSerializer(type);

            object value;

            serializer.DeserializeConstructor(out value, _version, this);
            serializer.DeserializeContents(ref value, _version, this);
            serializer.DeserializeBacklinks(ref value, _version, this);
            return(value);
        }
コード例 #3
0
        object ReadValueContents(Type knownType)
        {
            Type type;
            var  typeAttrib = CurrentGroup.Attribute("type");

            if (typeAttrib == null)
            {
                type = knownType;
            }
            else if (typeAttrib.Value == "null")
            {
                return(null);
            }
            else
            {
                type = SerializerRegistry.ParseSerializedType(typeAttrib.Value);
            }

            var serializer = SerializerRegistry.GetSerializer(type);

            object value;

            serializer.DeserializeConstructor(out value, _version, this);
            serializer.DeserializeContents(ref value, _version, this);
            serializer.DeserializeBacklinks(ref value, _version, this);
            return(value);
        }
コード例 #4
0
        void WriteValueContents(Type knownType, object value)
        {
            if (value == null)
            {
                _currentPass.Write((int)-1);
            }
            else
            {
                Type type = value.GetType();

                if (type.DerivesFrom <Type>())
                {
                    // Treat RuntimeType as if it's Type
                    type = typeof(Type);
                }

                if (knownType == type)
                {
                    _currentPass.Write((int)-2);
                }
                else
                {
                    SerializerRegistry.WriteSerializedTypeId(type, _currentPass);
                }

                var serializer = SerializerRegistry.GetSerializer(type);
                serializer.SerializeConstructor(ref value, this);
                serializer.SerializeContents(ref value, this);
                serializer.SerializeBacklinks(ref value, this);
            }
        }
コード例 #5
0
        public XElement WriteXml(string rootName, object rootReference)
        {
            Assert.That(!_hasWritten);
            _hasWritten = true;

            IncludeReference(rootReference);

            Assert.That(_groups.Count == 0);

            bool firstTime;
            long rootId = _objectIdGenerator.GetId(rootReference, out firstTime);

            Assert.That(_references.ContainsKey(rootId), "root object not included");

            while (_pending.Count > 0)
            {
                long   id         = _pending.Dequeue();
                object value      = _references[id];
                Type   type       = value.GetType();
                var    serializer = SerializerRegistry.GetSerializer(type);

                try
                {
                    _groups.Push(_lookup[id]);
                    serializer.SerializeConstructor(ref value, this);
                    serializer.SerializeContents(ref value, this);
                    serializer.SerializeBacklinks(ref value, this);
                    _groups.Pop();
                }
                catch (Exception e)
                {
                    throw new SerializationException(
                              "Error occurred while serializing value of type '" + type.Name() + "'", e);
                }
            }

            Assert.That(_groups.Count == 0);

            XElement rootElement = new XElement(rootName);

            rootElement.Add(new XAttribute("id", rootId));
            rootElement.Add(new XAttribute("version", SerializationVersion.Value));
            foreach (var cur in _lookup.Values)
            {
                rootElement.Add(cur);
            }
            return(rootElement);
        }
コード例 #6
0
        public object ReadXml()
        {
            var  root   = _doc.Root;
            long rootId = (long)root.GetAttribute("id");

            _version = (int)root.GetAttribute("version");

            Assert.That(_rootReferences.IsEmpty());

            foreach (var element in root.Elements())
            {
                Assert.That(element.Name.LocalName == "Reference");

                var id       = (long)element.GetAttribute("id");
                var typeName = (string)element.GetAttribute("type");

                var info = new ReadInfo();

                info.Element        = element;
                info.SerializedType = SerializerRegistry.ParseSerializedType(typeName);
                info.Serializer     = SerializerRegistry.GetSerializer(info.SerializedType);

                _rootReferences.Add(id, info);
            }

            foreach (var info in _rootReferences.Values)
            {
                DeserializeConstructor(info);
                DeserializeContents(info, false);
            }

            foreach (var info in _rootReferences.Values)
            {
                _groups.Push(info.Element);
                info.Serializer.DeserializeBacklinks(ref info.Value, _version, this);
                _groups.Pop();
            }

            foreach (var info in _rootReferences.Values)
            {
                info.Serializer.DeserializationComplete(ref info.Value, _version);
            }

            return(_rootReferences[rootId].Value);
        }
コード例 #7
0
        public object ReadFromBinary()
        {
            _version = _binaryReader.ReadInt32();

            List <ReadInfo> rootReferencesInOrder = new List <ReadInfo>();

            while (true)
            {
                long id = _binaryReader.ReadInt64();
                if (id == 0)
                {
                    break;
                }

                var info = new ReadInfo();
                info.Id = id;
                info.ConstructorPosition = _binaryReader.ReadInt64();
                info.ContentsPosition    = _binaryReader.ReadInt64();
                info.BacklinkPosition    = _binaryReader.ReadInt64();
                info.SerializedType      = SerializerRegistry.ReadSerializedTypeId(_binaryReader);
                info.Serializer          = SerializerRegistry.GetSerializer(info.SerializedType);

                _rootReferences.Add(id, info);
                rootReferencesInOrder.Add(info);
            }

            long rootId = _binaryReader.ReadInt64();
            long constructorSeekOffset = _binaryReader.ReadInt64();
            long contentsSeekOffset    = _binaryReader.ReadInt64();
            long backlinkSeekOffset    = _binaryReader.ReadInt64();

            // endSeekOffset
            _binaryReader.ReadInt64();

            foreach (var info in rootReferencesInOrder)
            {
                info.ConstructorPosition += constructorSeekOffset;
                info.ContentsPosition    += contentsSeekOffset;
                info.BacklinkPosition    += backlinkSeekOffset;
            }

            foreach (var info in rootReferencesInOrder)
            {
                DeserializeConstructor(info);
                DeserializeContents(info, false);
            }

            _binaryReader.BaseStream.Seek(backlinkSeekOffset, SeekOrigin.Begin);

            foreach (var info in rootReferencesInOrder)
            {
                info.Serializer.DeserializeBacklinks(ref info.Value, _version, this);
            }

            foreach (var info in _rootReferences.Values)
            {
                info.Serializer.DeserializationComplete(ref info.Value, _version);
            }

            return(_rootReferences[rootId].Value);
        }
コード例 #8
0
        public void WriteToBinary(Stream stream, object rootReference)
        {
            using (MemoryStream constructorBuffer = new MemoryStream())
                using (MemoryStream contentsBuffer = new MemoryStream())
                    using (MemoryStream backlinkBuffer = new MemoryStream())
                    {
                        BinaryWriter constructorWriter = new BinaryWriter(constructorBuffer);
                        BinaryWriter contentsWriter    = new BinaryWriter(contentsBuffer);
                        BinaryWriter backlinkWriter    = new BinaryWriter(backlinkBuffer);

                        BinaryWriter headerWriter = new BinaryWriter(stream);
                        headerWriter.Write(SerializationVersion.Value);

                        IncludeReference(rootReference);

                        bool firstTime;
                        long rootId = _objectIdGenerator.GetId(rootReference, out firstTime);
                        Assert.That(_references.ContainsKey(rootId), "root object not included");

                        while (_pending.Count > 0)
                        {
                            long id = _pending.Dequeue();
                            Assert.That(id != 0);

                            object value      = _references[id];
                            Type   type       = value.GetType();
                            var    serializer = SerializerRegistry.GetSerializer(type);

                            headerWriter.Write(id);
                            headerWriter.Write(constructorWriter.BaseStream.Position);
                            headerWriter.Write(contentsWriter.BaseStream.Position);
                            headerWriter.Write(backlinkWriter.BaseStream.Position);
                            SerializerRegistry.WriteSerializedTypeId(type, headerWriter);

                            try
                            {
                                _currentPass = constructorWriter;
                                serializer.SerializeConstructor(ref value, this);

                                _currentPass = contentsWriter;
                                serializer.SerializeContents(ref value, this);

                                _currentPass = backlinkWriter;
                                serializer.SerializeBacklinks(ref value, this);

                                _currentPass = null;
                            }
                            catch (Exception e)
                            {
                                throw new SerializationException(
                                          "Error occurred while serializing value of type '" + type.Name() + "'", e);
                            }
                        }

                        headerWriter.Write((long)0);
                        headerWriter.Write(rootId);

                        constructorWriter.Flush();
                        contentsWriter.Flush();
                        backlinkWriter.Flush();

                        long headerOffsetsLength = 8 * 4;
                        long constructorsOffset  = headerWriter.BaseStream.Position + headerOffsetsLength;
                        long contentsOffset      = constructorsOffset + constructorBuffer.Length;
                        long backlinkOffset      = contentsOffset + contentsBuffer.Length;
                        long endOffset           = contentsOffset + contentsBuffer.Length;

                        headerWriter.Write(constructorsOffset);
                        headerWriter.Write(contentsOffset);
                        headerWriter.Write(backlinkOffset);
                        headerWriter.Write(endOffset);

                        constructorBuffer.WriteTo(stream);
                        contentsBuffer.WriteTo(stream);
                        backlinkBuffer.WriteTo(stream);
                    }
        }