Пример #1
0
        public void Deserialize(byte[] buffer, ref int offset, ref T value)
        {
            var objId = SerializerBinary.ReadUInt32Bias(buffer, ref offset, Bias);

            if (objId == Null)
            {
                // Null

                // Ok the data tells us that value should be null.
                // But maybe we're recycling an object and it still contains an instance.
                // Lets return it to the user
                if (value != null)
                {
                    _ceras.DiscardObjectMethod?.Invoke(value);
                }

                value = default;
                return;
            }

            if (objId == InlineType)
            {
                Type type = null;
                _typeFormatter.Deserialize(buffer, ref offset, ref type);
                value = (T)(object)type;                 // This is ugly, but there's no way to prevent it, right?
                return;
            }

            if (objId >= 0)
            {
                // Something we already know
                value = _ceras.InstanceData.ObjectCache.GetExistingObject <T>(objId);
                return;
            }

            if (objId == ExternalObject)
            {
                // External object, let the user resolve!
                int externalId = SerializerBinary.ReadInt32(buffer, ref offset);

                // Let the user resolve
                _ceras.Config.ExternalObjectResolver.Resolve(externalId, out value);
                return;
            }


            // New object, see Note#1
            Type specificType = null;

            if (objId == NewValue)
            {
                // == NewValue (EmbeddedType)
                _typeFormatter.Deserialize(buffer, ref offset, ref specificType);
            }
            else
            {
                // == NewValueSameType
                specificType = typeof(T);
            }


            var entry = GetOrCreateEntry(specificType);

            // At this point we know that the 'value' will not be 'null', so if 'value' (the variable) is null we need to create an instance
            if (!entry.IsValueType)             // still possible that we're dealing with a boxed value;
            {
                // Do we already have an object?
                if (value != null)
                {
                    // Yes, then maybe we can overwrite its values (works for objects and collections)
                    // But only if it's the right type!

                    if (value.GetType() != specificType)
                    {
                        // Discard the old value
                        _ceras.DiscardObjectMethod?.Invoke(value);

                        // Create instance of the right type
                        value = (T)entry.Constructor();
                    }
                    else
                    {
                        // Existing object is the right type
                    }
                }
                else
                {
                    // Instance is null, create one
                    value = (T)entry.Constructor();
                }
            }
            else
            {
                // Not a reference type. So it doesn't matter anyway.
            }


            if (!_allowReferences)
            {
                entry.CurrentDeserializeDispatcher(buffer, ref offset, ref value);
                return;
            }

            //
            // Deserialize the object
            // 1. First generate a proxy so we can do lookups
            var objectProxy = _ceras.InstanceData.ObjectCache.CreateDeserializationProxy <T>();

            // 2. Make sure that the deserializer can make use of an already existing object (if there is one)
            objectProxy.Value = value;

            // 3. Actually read the object
            entry.CurrentDeserializeDispatcher(buffer, ref offset, ref objectProxy.Value);

            // 4. Write back the actual value, which instantly resolves all references
            value = objectProxy.Value;
        }
Пример #2
0
        public void Deserialize(byte[] buffer, ref int offset, ref T value)
        {
            var objId = SerializerBinary.ReadUInt32Bias(buffer, ref offset, Bias);

            if (objId == Null)
            {
                // Null

                // Ok the data tells us that value should be null.
                // But maybe we're recycling an object and it still contains an instance.
                // Lets return it to the user
                if (value != null)
                {
                    _serializer.Config.DiscardObjectMethod?.Invoke(value);
                }

                value = default;
                return;
            }

            if (objId == InlineType)
            {
                Type type = null;
                _typeFormatter.Deserialize(buffer, ref offset, ref type);
                value = (T)(object)type;                 // This is ugly, but there's no way to prevent it, right?
                return;
            }

            if (objId >= 0)
            {
                // Something we already know
                value = _serializer.InstanceData.ObjectCache.GetExistingObject <T>(objId);
                return;
            }

            if (objId == ExternalObject)
            {
                // External object, let the user resolve!
                int externalId = SerializerBinary.ReadInt32(buffer, ref offset);

                // Let the user resolve
                _serializer.Config.ExternalObjectResolver.Resolve(externalId, out value);
                return;
            }


            // New object, see Note#1
            Type specificType = null;

            if (objId == NewValue)
            {
                _typeFormatter.Deserialize(buffer, ref offset, ref specificType);
            }
            else             // if (objId == NewValueSameType) commented out, its the only possible remaining case
            {
                specificType = typeof(T);
            }


            // At this point we know that the 'value' will not be 'null', so if 'value' (the variable) is null we need to create an instance
            bool isRefType = !specificType.IsValueType;

            if (isRefType)
            {
                // Do we already have an object?
                if (value != null)
                {
                    // Yes, then maybe we can overwrite its values (works for objects and collections)
                    // But only if it's the right type!

                    // todo: types using a SerializationCtor (in the future) are handled in a different ReferenceFormatter
                    //		 where we first read all members into local variables, then create the object (passing some of them into the constructor), and then writing the remaining as usual
                    if (value.GetType() != specificType)
                    {
                        // Discard the old value
                        _serializer.Config.DiscardObjectMethod?.Invoke(value);

                        // Create instance of the right type
                        value = CreateInstance(specificType);
                    }
                    else
                    {
                        // Existing object is the right type
                    }
                }
                else
                {
                    // Instance is null, create one
                    // Note: that we *could* check if the type is one of the types that we cannot instantiate (String, Type, MemberInfo, ...) and then
                    //       just not call CreateInstance, but the check itself would be expensive as well (HashSet look up?), so what's the point of complicating the code more?
                    //       CreateInstance will do a dictionary lookup for us and simply return null for those types.
                    value = CreateInstance(specificType);
                }
            }
            else
            {
                // Not a reference type. So it doesn't matter.
            }



            //
            // Deserialize the object
            // 1. First generate a proxy so we can do lookups
            var objectProxy = _serializer.InstanceData.ObjectCache.CreateDeserializationProxy <T>();

            // 2. Make sure that the deserializer can make use of an already existing object (if there is one)
            objectProxy.Value = value;

            // 3. Actually read the object
            GetSpecificDeserializerDispatcher(specificType)(buffer, ref offset, ref objectProxy.Value);

            // 4. Write back the actual value, which instantly resolves all references
            value = objectProxy.Value;
        }