public void SerializeInstance(SlimWriter writer, TypeRegistry registry, RefPool refs, object instance, StreamingContext streamingContext) { if (m_MethodsOnSerializing != null) { invokeAttributedMethods(m_MethodsOnSerializing, instance, streamingContext); } if (m_Serialize != null) { m_Serialize(Schema, writer, registry, refs, instance, streamingContext); } else { ISerializable isz = instance as ISerializable; var info = new SerializationInfo(Type, new FormatterConverter()); isz.GetObjectData(info, streamingContext); serializeInfo(writer, registry, refs, info, streamingContext); } if (m_MethodsOnSerialized != null) { invokeAttributedMethods(m_MethodsOnSerialized, instance, streamingContext); } }
public void DeserializeInstance(SlimReader reader, TypeRegistry registry, RefPool refs, ref object instance, StreamingContext streamingContext) { if (m_MethodsOnDeserializing != null) { invokeAttributedMethods(m_MethodsOnDeserializing, instance, streamingContext); } if (m_Deserialize != null) { m_Deserialize(Schema, reader, registry, refs, ref instance, streamingContext); } else { var info = deserializeInfo(reader, registry, refs, streamingContext); //20171223 DKh Handle SerializationInfo.SetType() redefinition of serialized type if (instance.GetType() != info.ObjectType) { instance = FormatterServices.GetUninitializedObject(info.ObjectType);//ref instance is re-allocated with a different type } refs.AddISerializableFixup(instance, info); } if (m_MethodsOnDeserialized != null) { refs.AddOnDeserializedCallback(instance, this); } }
public void writeRefMetaHandle(SlimWriter writer, TypeRegistry registry, RefPool refs, object instance, StreamingContext streamingContext) { Type tInstance; var mh = refs.GetHandle(instance, registry, Format, out tInstance); writer.Write(mh); if (mh.IsInlinedValueType) { var wa = Format.GetWriteActionForType(tInstance); if (wa != null) { wa(writer, instance); } else { this.Serialize(writer, registry, refs, instance, streamingContext); } } else if (mh.IsInlinedRefType) { var wa = Format.GetWriteActionForRefType(tInstance); if (wa != null) { wa(writer, instance); } else { throw new SlimSerializationException("Internal error writeRefMetaHandle: no write action for ref type, but ref mhandle is inlined"); } } }
private static RefPool reservePool(SerializationOperation mode) { RefPool result = null; if (ts_pools == null) { ts_pools = new RefPool[8]; for (var i = 0; i < ts_pools.Length; i++) { ts_pools[i] = new RefPool(); } ts_poolFreeIdx = 0; } if (ts_poolFreeIdx < ts_pools.Length) { result = ts_pools[ts_poolFreeIdx]; ts_poolFreeIdx++; } else { result = new RefPool(); } result.Acquire(mode); return(result); }
private static void releasePool(RefPool pool) { if (ts_poolFreeIdx == 0) { return; } pool.Release(); ts_poolFreeIdx--; ts_pools[ts_poolFreeIdx] = pool; }
private void serializeInfo(SlimWriter writer, TypeRegistry registry, RefPool refs, SerializationInfo info, StreamingContext streamingContext) { writer.Write(registry.GetTypeHandle(info.ObjectType));//20171223 DKh writer.Write(info.MemberCount); var senum = info.GetEnumerator(); while (senum.MoveNext()) { writer.Write(senum.Name); writer.Write(registry.GetTypeHandle(senum.ObjectType)); Schema.Serialize(writer, registry, refs, senum.Value, streamingContext); } }
public object readRefMetaHandle(SlimReader reader, TypeRegistry registry, RefPool refs, StreamingContext streamingContext) { var mh = reader.ReadMetaHandle(); if (mh.IsInlinedValueType) { var tboxed = registry[mh.Metadata.Value];//adding this type to registry if it is not there yet var ra = Format.GetReadActionForType(tboxed); if (ra != null) { return(ra(reader)); } else { return(this.Deserialize(reader, registry, refs, streamingContext)); } } return(refs.HandleToReference(mh, registry, Format, reader)); }
public void Serialize(SlimWriter writer, TypeRegistry registry, RefPool refs, object instance, StreamingContext streamingContext, Type valueType = null) { Type type = valueType; VarIntStr typeHandle = new VarIntStr(0); if (type == null) { if (instance == null) { writer.Write(TypeRegistry.NULL_HANDLE);//object type null return; } type = instance.GetType(); //Write type name. Full or compressed. Full Type names are assembly-qualified strings, compressed are string in form of // $<name_table_index> i.e. $1 <--- get string[1] typeHandle = registry.GetTypeHandle(type); writer.Write(typeHandle); } //we get here if we have a boxed value of directly-handled type var wa = Format.GetWriteActionForType(type) ?? Format.GetWriteActionForRefType(type);//20150503 DKh fixed root byte[] slow if (wa != null) { wa(writer, instance); return; } TypeDescriptor td = getTypeDescriptorCachedOrMake(type); if (td.IsArray) //need to write array dimensions { writer.Write(Arrays.ArrayToDescriptor((Array)instance, type, typeHandle)); } td.SerializeInstance(writer, registry, refs, instance, streamingContext); }
public void DeserializeRefTypeInstance(object instance, SlimReader reader, TypeRegistry registry, RefPool refs, StreamingContext streamingContext) { if (instance == null) { throw new SlimDeserializationException("DeserRefType(null)"); } var type = instance.GetType(); reader.ReadVarIntStr();//skip type as we already know it from prior-allocated metahandle TypeDescriptor td = getTypeDescriptorCachedOrMake(type); if (type.IsArray) { reader.ReadString();//skip array descriptor as we already know it from prior-allocated metahandle } td.DeserializeInstance(reader, registry, refs, ref instance, streamingContext); }
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); }
public object Deserialize(SlimReader reader, TypeRegistry registry, RefPool refs, StreamingContext streamingContext, Type valueType = null) { return(DeserializeRootOrInner(reader, registry, refs, streamingContext, false, valueType)); }
private SerializationInfo deserializeInfo(SlimReader reader, TypeRegistry registry, RefPool refs, StreamingContext streamingContext) { //20171223 DKh var visInfo = reader.ReadVarIntStr(); var tInfo = registry[visInfo]; var info = new SerializationInfo(tInfo, new FormatterConverter()); //20171223 DKh //var info = new SerializationInfo(Type, new FormatterConverter()); var cnt = reader.ReadInt(); for (int i = 0; i < cnt; i++) { var name = reader.ReadString(); var vis = reader.ReadVarIntStr(); var type = registry[vis]; var obj = Schema.Deserialize(reader, registry, refs, streamingContext); info.AddValue(name, obj, type); } return(info); }
private object deserialize(SlimReader reader, RefPool pool) { object root = null; var scontext = new StreamingContext(); var registry = (m_TypeMode == TypeRegistryMode.PerCall) ? new TypeRegistry(m_GlobalTypes) : m_BatchTypeRegistry; { var rcount = registry.Count; m_BatchTypeRegistryPriorCount = rcount; readHeader(reader); if (!m_SkipTypeRegistryCrosschecks) { if (reader.ReadUInt() != rcount) { throw new SlimDeserializationException(StringConsts.SLIM_TREG_COUNT_ERROR); } if (reader.ReadULong() != registry.CSum) { throw new SlimDeserializationException(StringConsts.SLIM_TREG_CSUM_ERROR); } } //Read root //Deser will add root to pool[1] if its ref-typed //------------------------------------------------ root = m_Format.TypeSchema.DeserializeRootOrInner(reader, registry, pool, scontext, root: true); if (root == null) { return(null); } if (root is rootTypeBox) { return(((rootTypeBox)root).TypeValue); } var type = root.GetType(); var isValType = type.IsValueType; var i = 1; if (!isValType) { i++; } //Read all the rest of objects. The upper bound of this loop may increase as objects are read and their references added to pool //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.DeserializeRefTypeInstance(instance, reader, registry, pool, scontext); } } } //perform fixups for ISerializable //--------------------------------------------- var fxps = pool.Fixups; for (var i = 0; i < fxps.Count; i++) { var fixup = fxps[i]; var t = fixup.Instance.GetType(); var ctor = SerializationUtils.GetISerializableCtorInfo(t); if (ctor == null) { continue; //20171223 DKh ISerializable does not mandate the .ctor(info, context), } //for example, in net-core they use info.SetType() to redefine comparers //so the actual comparer does not have a .ctor at all (which is very odd) //Before 20171223 DKh change //if (ctor==null) // throw new SlimDeserializationException(StringConsts.SLIM_ISERIALIZABLE_MISSING_CTOR_ERROR + t.FullName); ctor.Invoke(fixup.Instance, new object[] { fixup.Info, scontext }); } //20150214 DD - fixing deserialization problem of Dictionary(InvariantStringComparer) //before 20150214 this was AFTER OnDeserialization //invoke OnDeserialized-decorated methods //-------------------------------------------- var odc = pool.OnDeserializedCallbacks; for (int i = 0; i < odc.Count; i++) { var cb = odc[i]; cb.Descriptor.InvokeOnDeserializedCallbak(cb.Instance, scontext); } //before 20150214 this was BEFORE OnDeserializedCallbacks //invoke IDeserializationCallback //--------------------------------------------- for (int i = 1; i < pool.Count; i++)//[0]=null { var dc = pool[i] as IDeserializationCallback; if (dc != null) { try { dc.OnDeserialization(this); } catch (Exception error) { throw new SlimDeserializationException(StringConsts.SLIM_DESERIALIZE_CALLBACK_ERROR + error.ToMessageWithType(), error); } } } return(root); }
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); } } }