Exemple #1
0
 internal SlimSerializer(TypeRegistry global, SlimFormat format) : this(format)
 {
     __globalTypeRegistry          = global;
     m_GlobalTypes                 = new IEnumerable <Type>[] { global.ToArray() };
     m_SkipTypeRegistryCrosschecks = true;
 }
Exemple #2
0
 public object Deserialize(SlimReader reader, TypeRegistry registry, RefPool refs, StreamingContext streamingContext, Type valueType = null)
 {
     return(DeserializeRootOrInner(reader, registry, refs, streamingContext, false, valueType));
 }
Exemple #3
0
        public object DeserializeRootOrInner(SlimReader reader, TypeRegistry registry, RefPool refs, StreamingContext streamingContext, bool root, Type valueType = null)
        {
            Type type = valueType;

            if (type == null)
            {
                var thandle = reader.ReadVarIntStr();
                if (thandle.StringValue != null)               //need to search for possible array descriptor
                {
                    var ip = thandle.StringValue.IndexOf('|'); //array descriptor start
                    if (ip > 0)
                    {
                        var tname = thandle.StringValue.Substring(0, ip);
                        if (TypeRegistry.IsNullHandle(tname))
                        {
                            return(null);
                        }
                        type = registry[tname];
                    }
                    else
                    {
                        if (TypeRegistry.IsNullHandle(thandle))
                        {
                            return(null);
                        }
                        type = registry[thandle];
                    }
                }
                else
                {
                    if (TypeRegistry.IsNullHandle(thandle))
                    {
                        return(null);
                    }
                    type = registry[thandle];
                }
            }

            //we get here if we have a boxed value of directly-handled type
            var ra = Format.GetReadActionForType(type) ?? Format.GetReadActionForRefType(type);//20150503 DKh fixed root byte[] slow

            if (ra != null)
            {
                return(ra(reader));
            }


            TypeDescriptor td = getTypeDescriptorCachedOrMake(type);

            object instance = null;

            if (td.IsArray)
            {
                instance = Arrays.DescriptorToArray(reader.ReadString(), type);
            }
            else
            {
                instance = SerializationUtils.MakeNewObjectInstance(type);
            }

            if (root)
            {
                if (!type.IsValueType)//if this is a reference type
                {
                    refs.Add(instance);
                }
            }

            td.DeserializeInstance(reader, registry, refs, ref instance, streamingContext);


            return(instance);
        }
Exemple #4
0
        /// <summary>
        /// Returns object reference for supplied metahandle
        /// </summary>
        public object HandleToReference(MetaHandle handle, TypeRegistry treg, SlimFormat format, SlimReader reader)
        {
            Debug.Assert(m_Mode == SerializationOperation.Deserializing, "HandleToReference() called while serializing", DebugAction.Throw);

            if (handle.IsInlinedString)
            {
                return(handle.Metadata.Value.StringValue);
            }
            if (handle.IsInlinedTypeValue)
            {
                var tref = treg[handle.Metadata.Value];//adding this type to registry if it is not there yet
                return(tref);
            }

            if (handle.IsInlinedRefType)
            {
                var tref = treg[handle.Metadata.Value];//adding this type to registry if it is not there yet
                var ra   = format.GetReadActionForRefType(tref);
                if (ra != null)
                {
                    var inst = ra(reader);
                    m_List.Add(inst);
                    return(inst);
                }
                else
                {
                    throw new SlimDeserializationException("Internal error HandleToReference: no read action for ref type, but ref mhandle is inlined");
                }
            }


            int idx = (int)handle.Handle;

            if (idx < m_List.Count)
            {
                return(m_List[idx]);
            }

            if (!handle.Metadata.HasValue)
            {
                throw new SlimDeserializationException(StringConsts.SLIM_HNDLTOREF_MISSING_TYPE_NAME_ERROR + handle.ToString());
            }

            Type type;
            var  metadata = handle.Metadata.Value;

            if (metadata.StringValue != null)               //need to search for possible array descriptor
            {
                var ip = metadata.StringValue.IndexOf('|'); //array descriptor start
                if (ip > 0)
                {
                    var tname = metadata.StringValue.Substring(0, ip);
                    if (TypeRegistry.IsNullHandle(tname))
                    {
                        return(null);
                    }
                    type = treg[tname];
                }
                else
                {
                    if (TypeRegistry.IsNullHandle(metadata))
                    {
                        return(null);
                    }
                    type = treg[metadata];
                }
            }
            else
            {
                if (TypeRegistry.IsNullHandle(metadata))
                {
                    return(null);
                }
                type = treg[metadata];
            }

            object instance = null;

            if (type.IsArray)
            {
                //DKh 20130712 Removed repetitive code that was refactored into Arrays class
                instance = Arrays.DescriptorToArray(metadata.StringValue, type);
            }
            else
            {
                //20130715 DKh
                instance = SerializationUtils.MakeNewObjectInstance(type);
            }

            m_List.Add(instance);
            return(instance);
        }