/// <summary> /// Deserialize an object from TL serialization context. /// </summary> /// <remarks> /// Constructor number for the object is automatically determined by reading the first number from the streamer, /// hence object within the context streamer must be serialized as boxed type. /// </remarks> /// <param name="context">TL serialization context.</param> /// <returns>Deserialized object.</returns> /// <exception cref="TLSerializerNotFoundException">When serializer not found.</exception> public static object Deserialize(TLSerializationContext context) { // Here streamer's position must point to a boxed TL type. TLStreamer streamer = context.Streamer; // Read a constructor number and restore the streamer position. streamer.PushPosition(); uint constructorNumber = streamer.ReadUInt32(); //Console.WriteLine("Retrived constructor number " + constructorNumber); streamer.PopPosition(); ITLSerializer serializer = context.Rig.GetSerializerByConstructorNumber(constructorNumber); //Console.WriteLine("Got serializer for above constructor number" + serializer.SupportedType); if (serializer == null) { throw new TLSerializerNotFoundException( string.Format("Constructor number: 0x{0:X8} is not supported by any registered serializer.", constructorNumber)); } return(serializer.Read(context, TLSerializationMode.Boxed)); }
/// <summary> /// Serialize an object. /// </summary> /// <param name="obj">The object.</param> /// <param name="context">TL serialization context.</param> /// <param name="modeOverride">Serialization mode override.</param> /// <exception cref="TLSerializerNotFoundException">When serializer not found.</exception> public static void Serialize(object obj, TLSerializationContext context, TLSerializationMode?modeOverride = null) { var objType = obj.GetType(); ITLSerializer serializer = context.Rig.GetSerializerByObjectType(objType); if (serializer == null) { throw new TLSerializerNotFoundException(string.Format("There is no serializer for a type: '{0}'.", objType.FullName)); } serializer.Write(obj, context, modeOverride); }
/// <summary> /// Deserialize an object from TL serialization context. /// </summary> /// <param name="objType">Type of the object.</param> /// <param name="context">TL serialization context.</param> /// <param name="modeOverride">Serialization mode override.</param> /// <returns>Deserialized object.</returns> /// <exception cref="TLSerializerNotFoundException">When serializer not found.</exception> public static object Deserialize(Type objType, TLSerializationContext context, TLSerializationMode?modeOverride = null) { if (objType == typeof(object)) { return(Deserialize(context)); } ITLSerializer serializer = context.Rig.GetSerializerByObjectType(objType); if (serializer == null) { throw new TLSerializerNotFoundException(string.Format("There is no serializer for a type: '{0}'.", objType.FullName)); } return(serializer.Read(context, modeOverride)); }
/// <summary> /// Deserialize an object from TL serialization context. /// </summary> /// <remarks> /// Constructor number for the object is automatically determined by reading the first number from the streamer, /// hence object within the context streamer must be serialized as boxed type. /// </remarks> /// <param name="context">TL serialization context.</param> /// <returns>Deserialized object.</returns> /// <exception cref="TLSerializerNotFoundException">When serializer not found.</exception> public static object Deserialize(TLSerializationContext context) { TLStreamer streamer = context.Streamer; // Read a constructor number. uint constructorNumber = streamer.ReadUInt32(); ITLSerializer serializer = context.Rig.GetSerializerByConstructorNumber(constructorNumber); if (serializer == null) { throw new TLSerializerNotFoundException(string.Format("Constructor number: 0x{0:X8} is not supported by any registered serializer.", constructorNumber)); } // Bare because construction number has already been read. return(serializer.Read(context, TLSerializationMode.Bare)); }
/// <summary> /// Deserialize an object from TL serialization context. /// </summary> /// <typeparam name="T">Type of the object.</typeparam> /// <param name="context">TL serialization context.</param> /// <param name="modeOverride">Serialization mode override.</param> /// <returns>Deserialized object.</returns> public static T Deserialize <T>(TLSerializationContext context, TLSerializationMode?modeOverride = null) { return((T)Deserialize(typeof(T), context, modeOverride)); }