Beispiel #1
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);
        }
Beispiel #2
0
        private void serialize(SlimWriter writer, object root, RefPool pool)
        {
            if (root is Type)
            {
                root = new rootTypeBox {
                    TypeValue = (Type)root
                }
            }
            ;

            var scontext = new StreamingContext();
            var registry = (m_TypeMode == TypeRegistryMode.PerCall) ? new TypeRegistry(m_GlobalTypes) : m_BatchTypeRegistry;
            var type     = root != null?root.GetType() : typeof(object);

            var isValType = type.IsValueType;


            writeHeader(writer);
            var rcount = registry.Count;

            m_BatchTypeRegistryPriorCount = rcount;

            if (!m_SkipTypeRegistryCrosschecks)
            {
                writer.Write((uint)rcount);
                writer.Write(registry.CSum);
            }


            //Write root in pool if it is reference type
            if (!isValType && root != null)
            {
                pool.Add(root);
            }

            m_Format.TypeSchema.Serialize(writer, registry, pool, root, scontext);


            if (root == null)
            {
                return;
            }

            var i = 1;

            if (!isValType)
            {
                i++;
            }

            //Write all the rest of objects. The upper bound of this loop may increase as objects are written
            //0 = NULL
            //1 = root IF root is ref type
            var ts = m_Format.TypeSchema;

            for (; i < pool.Count; i++)
            {
                var instance = pool[i];
                var tinst    = instance.GetType();
                if (!m_Format.IsRefTypeSupported(tinst))
                {
                    ts.Serialize(writer, registry, pool, instance, scontext);
                }
            }
        }