Пример #1
0
        private object ReadBodyInternal(TLSerializationContext context, TLSerializationMode?itemsSerializationModeOverride = null)
        {
            Func <TLSerializationContext, TLSerializationMode?, object> read;

            if (IsItemsTypeObject)
            {
                read = (sc, m) => TLRig.Deserialize <T>(sc, m);
            }
            else
            {
                ITLSerializer serializer = GetSerializer(context);
                read = serializer.Read;
            }

            int length = context.Streamer.ReadInt32();
            var list   = (List <T>)Activator.CreateInstance(SupportedTypeInternal, length);

            for (int i = 0; i < length; i++)
            {
                var item = (T)read(context, itemsSerializationModeOverride);
                list.Add(item);
            }

            return(list);
        }
Пример #2
0
        private void WriteBodyInternal(object obj, TLSerializationContext context, TLSerializationMode?itemsSerializationModeOverride = null)
        {
            Action <object, TLSerializationContext, TLSerializationMode?> write;

            if (IsItemsTypeObject)
            {
                write = TLRig.Serialize;
            }
            else
            {
                ITLSerializer serializer = GetSerializer(context);
                write = serializer.Write;
            }

            var vector = obj as List <T>;

            if (vector == null)
            {
                // TODO: log wrong type.
                throw new InvalidOperationException("This serializer supports only List<> types.");
            }
            int length = vector.Count;

            // Length.
            context.Streamer.WriteInt32(length);

            // Child objects.
            for (int i = 0; i < length; i++)
            {
                write(vector[i], context, itemsSerializationModeOverride);
            }
        }
Пример #3
0
        private TLSerializationMode?GetVectorItemsSerializationModeOverride(ITLVectorSerializer vectorSerializer, PropertyInfo propertyInfo,
                                                                            TLSerializationContext context)
        {
            Type propType = propertyInfo.PropertyType;

            if (vectorSerializer.SupportedType != propType)
            {
                throw new NotSupportedException(string.Format("Current vector serializer doesn't support type: {0}. It supports: {1}", propType,
                                                              vectorSerializer.SupportedType));
            }

            TLSerializationMode?itemsSerializationModeOverride = TLSerializationMode.Bare;

            // Check for items serializer.
            // If items have multiple constructors or have a TLTypeAttribute (in other words it is TL type),
            // then items must be serialized as boxed.
            Type          itemsType            = vectorSerializer.ItemsType;
            ITLSerializer vectorItemSerializer = context.Rig.GetSerializerByObjectType(itemsType);

            if (vectorItemSerializer is ITLMultiConstructorSerializer || itemsType.GetTypeInfo().GetCustomAttribute <TLTypeAttribute>() != null)
            {
                itemsSerializationModeOverride = TLSerializationMode.Boxed;
            }
            else
            {
                // Check for TLVector attribute with items serialization mode override.
                var tlVectorAttribute = propertyInfo.GetCustomAttribute <TLVectorAttribute>();
                if (tlVectorAttribute != null)
                {
                    itemsSerializationModeOverride = tlVectorAttribute.ItemsModeOverride;
                }
            }
            return(itemsSerializationModeOverride);
        }
Пример #4
0
 private void IndexType(uint constructorNumber, ITLSerializer serializer)
 {
     if (!_constructorNumberSerializersIndex.ContainsKey(constructorNumber))
     {
         _constructorNumberSerializersIndex.Add(constructorNumber, serializer);
     }
 }
Пример #5
0
        /// <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));
        }
Пример #6
0
        protected override object ReadBody(TLSerializationContext context)
        {
            object obj = Activator.CreateInstance(_objectType);

            for (int i = 0; i < _properties.Length; i++)
            {
                TLPropertyInfo tlPropertyInfo = _properties[i];
                PropertyInfo   propertyInfo   = tlPropertyInfo.PropertyInfo;

                object propertyValue;

                Type propType = propertyInfo.PropertyType;

                ITLSerializer tlSerializer = context.Rig.GetSerializerByObjectType(propType);

                if (tlSerializer is ITLVectorSerializer)
                {
                    var vectorSerializer = tlSerializer as ITLVectorSerializer;
                    TLSerializationMode?itemsSerializationModeOverride = GetVectorItemsSerializationModeOverride(vectorSerializer, propertyInfo, context);
                    propertyValue = vectorSerializer.Read(context, tlPropertyInfo.SerializationModeOverride, itemsSerializationModeOverride);
                }
                else
                {
                    propertyValue = tlSerializer.Read(context, tlPropertyInfo.SerializationModeOverride);
                }
                tlPropertyInfo.PropertyInfo.SetValue(obj, propertyValue);
            }
            return(obj);
        }
Пример #7
0
        private static ITLSerializer GetSerializer(TLSerializationContext context)
        {
            ITLSerializer serializer = context.Rig.GetSerializer <T>();

            if (serializer == null)
            {
                throw new TLSerializerNotFoundException(string.Format("There is no serializer for a type: '{0}'.", ItemsTypeInternal.FullName));
            }
            return(serializer);
        }
Пример #8
0
        /// <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);
        }
Пример #9
0
        /// <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));
        }
Пример #10
0
        /// <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));
        }
Пример #11
0
        private static ITLPropertySerializationAgent[] CreateSerializationAgents(IEnumerable <TLPropertyInfo> tlPropertyInfos, TLSerializersBucket serializersBucket)
        {
            return(tlPropertyInfos.OrderBy(info => info.Order).Distinct().Select(
                       tlPropertyInfo =>
            {
                PropertyInfo propertyInfo = tlPropertyInfo.PropertyInfo;

                Type propType = propertyInfo.PropertyType;

                ITLPropertySerializationAgent serializationAgent;

                if (propType == typeof(object))
                {
                    /*
                     * https://core.telegram.org/mtproto/serialize#object-pseudotype
                     * Object Pseudotype
                     * The Object pseudotype is a “type” which can take on values that belong to any boxed type in the schema.
                     */
                    serializationAgent = new TLObjectPropertySerializationAgent(tlPropertyInfo);
                }
                else
                {
                    ITLSerializer tlSerializer = serializersBucket[propType];
                    Debug.Assert(tlSerializer != null);

                    var vectorSerializer = tlSerializer as ITLVectorSerializer;
                    if (vectorSerializer != null)
                    {
                        TLSerializationMode?itemsSerializationModeOverride = GetVectorItemsSerializationModeOverride(
                            vectorSerializer,
                            propertyInfo,
                            serializersBucket);
                        serializationAgent = new TLVectorPropertySerializationAgent(tlPropertyInfo, vectorSerializer, itemsSerializationModeOverride);
                    }
                    else
                    {
                        serializationAgent = new TLPropertySerializationAgent(tlPropertyInfo, tlSerializer);
                    }
                }
                return serializationAgent;
            }).ToArray());
        }
Пример #12
0
        /// <summary>
        ///     Adds serializer.
        /// </summary>
        /// <param name="serializer">TL serializer.</param>
        public void Add(ITLSerializer serializer)
        {
            Type type = serializer.SupportedType;

            if (!_serializersIndex.ContainsKey(type))
            {
                _serializersIndex.Add(type, serializer);

                var singleConstructorSerializer = serializer as ITLSingleConstructorSerializer;
                if (singleConstructorSerializer != null)
                {
                    IndexType(singleConstructorSerializer.ConstructorNumber, serializer);
                }
                var multipleConstructorSerializer = serializer as ITLMultiConstructorSerializer;
                if (multipleConstructorSerializer != null)
                {
                    foreach (uint constructorNumber in multipleConstructorSerializer.ConstructorNumbers)
                    {
                        IndexType(constructorNumber, serializer);
                    }
                }
            }
        }
Пример #13
0
        protected override void WriteBody(object obj, TLSerializationContext context)
        {
            for (int i = 0; i < _properties.Length; i++)
            {
                TLPropertyInfo tlPropertyInfo = _properties[i];
                PropertyInfo   propertyInfo   = tlPropertyInfo.PropertyInfo;

                Type   propType      = propertyInfo.PropertyType;
                object propertyValue = propertyInfo.GetValue(obj);

                ITLSerializer tlSerializer = context.Rig.GetSerializerByObjectType(propType);

                if (tlSerializer is ITLVectorSerializer)
                {
                    var vectorSerializer = tlSerializer as ITLVectorSerializer;
                    TLSerializationMode?itemsSerializationModeOverride = GetVectorItemsSerializationModeOverride(vectorSerializer, propertyInfo, context);
                    vectorSerializer.Write(propertyValue, context, tlPropertyInfo.SerializationModeOverride, itemsSerializationModeOverride);
                }
                else
                {
                    tlSerializer.Write(propertyValue, context, tlPropertyInfo.SerializationModeOverride);
                }
            }
        }
 public TLPropertySerializationAgent(TLPropertyInfo tlPropertyInfo, ITLSerializer serializer) : base(tlPropertyInfo)
 {
     _serializer = serializer;
 }
Пример #15
0
 public TLPropertySerializationAgent(TLPropertyInfo tlPropertyInfo, ITLSerializer serializer)
     : base(tlPropertyInfo)
 {
     _serializer = serializer;
 }