public TLCustomObjectSerializer( uint constructorNumber, Type objectType, IEnumerable<TLPropertyInfo> properties, TLSerializersBucket serializersBucket, TLSerializationMode serializationMode = TLSerializationMode.Boxed) : base(constructorNumber) { _objectType = objectType; _serializationAgents = CreateSerializationAgents(properties, serializersBucket); SerializationMode = serializationMode; }
public TLCustomObjectSerializer( uint constructorNumber, Type objectType, IEnumerable <TLPropertyInfo> properties, TLSerializersBucket serializersBucket, TLSerializationMode serializationMode = TLSerializationMode.Boxed) : base(constructorNumber) { _objectType = objectType; _serializationAgents = CreateSerializationAgents(properties, serializersBucket); SerializationMode = serializationMode; }
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(); }
private static TLSerializationMode?GetVectorItemsSerializationModeOverride( ITLVectorSerializer vectorSerializer, PropertyInfo propertyInfo, TLSerializersBucket serializersBucket) { 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 = serializersBucket[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); }
private static TLSerializationMode? GetVectorItemsSerializationModeOverride( ITLVectorSerializer vectorSerializer, PropertyInfo propertyInfo, TLSerializersBucket serializersBucket) { 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 = serializersBucket[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; }
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()); }