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); }
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); }
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); }
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); } }
protected override void WriteBody(object obj, TLSerializationContext context) { for (int i = 0; i < _serializationAgents.Length; i++) { ITLPropertySerializationAgent agent = _serializationAgents[i]; agent.Write(obj, context); } }
/// <summary> /// Writes a header with constructor number. /// </summary> /// <param name="context">Serialization context.</param> /// <param name="modeOverride">Serialization mode override.</param> protected virtual void WriteHeader(TLSerializationContext context, TLSerializationMode? modeOverride = null) { if ((!modeOverride.HasValue && SerializationMode != TLSerializationMode.Bare) || (modeOverride.HasValue && modeOverride.Value == TLSerializationMode.Boxed)) { // If type is boxed (not bare) then write type constructor number. context.Streamer.WriteUInt32(ConstructorNumber); } }
protected override void WriteBody(object obj, TLSerializationContext context) { for (int i = 0; i < _serializationAgents.Length; i++) { ITLPropertySerializationAgent agent = _serializationAgents[i]; agent.Write(obj, context); } }
/// <summary> /// Writes a header with constructor number. /// </summary> /// <param name="context">Serialization context.</param> /// <param name="modeOverride">Serialization mode override.</param> protected virtual void WriteHeader(TLSerializationContext context, TLSerializationMode?modeOverride = null) { if ((!modeOverride.HasValue && SerializationMode != TLSerializationMode.Bare) || (modeOverride.HasValue && modeOverride.Value == TLSerializationMode.Boxed)) { // If type is boxed (not bare) then write type constructor number. context.Streamer.WriteUInt32(ConstructorNumber); } }
protected override void WriteBody(object obj, TLSerializationContext context) { var str = (string)obj; TLStreamer streamer = context.Streamer; byte[] bytes = Encoding.GetBytes(str); streamer.WriteTLBytes(bytes); }
protected override void WriteBody(object obj, TLSerializationContext context) { var str = (string) obj; TLStreamer streamer = context.Streamer; byte[] bytes = Encoding.GetBytes(str); streamer.WriteTLBytes(bytes); }
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); }
protected override object ReadBody(TLSerializationContext context) { object obj = Activator.CreateInstance(_objectType); for (int i = 0; i < _serializationAgents.Length; i++) { ITLPropertySerializationAgent agent = _serializationAgents[i]; agent.Read(obj, context); } return obj; }
protected override object ReadBody(TLSerializationContext context) { object obj = Activator.CreateInstance(_objectType); for (int i = 0; i < _serializationAgents.Length; i++) { ITLPropertySerializationAgent agent = _serializationAgents[i]; agent.Read(obj, context); } return(obj); }
public void Write(object obj, TLSerializationContext context, TLSerializationMode? modeOverride = null) { Type objType = obj.GetType(); ITLSingleConstructorSerializer serializer; if (!_serializersTypeIndex.TryGetValue(objType, out serializer)) { throw new NotSupportedException(string.Format("Object type '{0}' is not supported by this serializer.", objType)); } TLRig.Serialize(obj, context, modeOverride); }
public void Write(object obj, TLSerializationContext context, TLSerializationMode?modeOverride = null) { Type objType = obj.GetType(); ITLSingleConstructorSerializer serializer; if (!_serializersTypeIndex.TryGetValue(objType, out serializer)) { throw new NotSupportedException(string.Format("Object type '{0}' is not supported by this serializer.", objType)); } TLRig.Serialize(obj, context, modeOverride); }
private object ReadBodyInternal(TLSerializationContext context, TLSerializationMode?itemsSerializationModeOverride = null) { int length = context.Streamer.ReadInt32(); var list = (List <T>)Activator.CreateInstance(SupportedTypeInternal, length); for (int i = 0; i < length; i++) { var item = TLRig.Deserialize <T>(context, itemsSerializationModeOverride); list.Add(item); } return(list); }
/// <summary> /// Reads and checks constructor number. /// </summary> /// <param name="context">Context.</param> /// <param name="modeOverride">Mode override.</param> /// <exception cref="InvalidTLConstructorNumberException">When actual constructor number is not as expected.</exception> protected void ReadAndCheckConstructorNumber(TLSerializationContext context, TLSerializationMode? modeOverride = null) { if ((!modeOverride.HasValue && SerializationMode != TLSerializationMode.Bare) || (modeOverride.HasValue && modeOverride.Value == TLSerializationMode.Boxed)) { // If type is boxed (not bare) then read type constructor number and check for supporting. uint constructorNumber = context.Streamer.ReadUInt32(); if (constructorNumber != ConstructorNumber) { throw new InvalidTLConstructorNumberException(string.Format("Invalid TL constructor number. Expected: {0}, actual: {1}.", ConstructorNumber, constructorNumber)); } } }
/// <summary> /// Reads and checks constructor number. /// </summary> /// <param name="context">Context.</param> /// <param name="modeOverride">Mode override.</param> /// <exception cref="InvalidTLConstructorNumberException">When actual constructor number is not as expected.</exception> protected void ReadAndCheckConstructorNumber(TLSerializationContext context, TLSerializationMode?modeOverride = null) { if ((!modeOverride.HasValue && SerializationMode != TLSerializationMode.Bare) || (modeOverride.HasValue && modeOverride.Value == TLSerializationMode.Boxed)) { // If type is boxed (not bare) then read type constructor number and check for supporting. uint constructorNumber = context.Streamer.ReadUInt32(); if (constructorNumber != ConstructorNumber) { throw new InvalidTLConstructorNumberException(string.Format("Invalid TL constructor number. Expected: {0}, actual: {1}.", ConstructorNumber, constructorNumber)); } } }
protected override object ReadBody(TLSerializationContext context) { bool hasFlags = false; uint flagsValue = 0; object obj = Activator.CreateInstance(_objectType); hasFlags = CheckForFlags(obj); if (hasFlags) { var properties = obj.GetType().GetProperties(); ITLPropertySerializationAgent flagAgent = _serializationAgents[0]; //deserialize anf read the flags flagAgent.Read(obj, context); flagsValue = (uint)properties[0].GetValue(obj); //convert it to binary string binary = Convert.ToString(flagsValue, 2); //the length of the binary is the maximum index of flasg in the message, //anything greater than this index, is useless and should directly exit var maxIndex = binary.Length - 1; for (int i = 1; i < _serializationAgents.Length; i++) { var currentPropertyInfo = properties[i]; var propInfo = currentPropertyInfo.GetCustomAttribute <TLPropertyAttribute>(); //convert the flags to binary if (propInfo.IsFlag) { if (maxIndex < propInfo.Flag) { //since the length is smaller than the flag index, it means this flag was not sent in the msg continue; } if (binary[(int)(maxIndex - propInfo.Flag)] == '0') { //if the flag is set to zero continue; } } ITLPropertySerializationAgent agent = _serializationAgents[i]; agent.Read(obj, context); } } else { for (int i = 0; i < _serializationAgents.Length; i++) { ITLPropertySerializationAgent agent = _serializationAgents[i]; agent.Read(obj, context); } } return(obj); }
/// <summary> /// Base serializer writes only header with type id. Then calls <see cref="WriteBody" />. /// </summary> /// <param name="obj">Object to be serialized.</param> /// <param name="context">Serialization context.</param> /// <param name="modeOverride">Override of default type serialization mode.</param> public virtual void Write([NotNull] object obj, TLSerializationContext context, TLSerializationMode?modeOverride = null) { if (obj == null) { throw new ArgumentNullException("obj"); } if (obj.GetType() != SupportedType) { throw new TLSerializationException(string.Format("Expected object of type {0}, actual object type {1}.", SupportedType, obj.GetType())); } WriteHeader(context, modeOverride); WriteBody(obj, context); }
/// <summary> /// Base serializer writes only header with type id. Then calls <see cref="WriteBody" />. /// </summary> /// <param name="obj">Object to be serialized.</param> /// <param name="context">Serialization context.</param> /// <param name="modeOverride">Override of default type serialization mode.</param> public virtual void Write([NotNull] object obj, TLSerializationContext context, TLSerializationMode? modeOverride = null) { if (obj == null) { throw new ArgumentNullException("obj"); } if (obj.GetType() != SupportedType) { throw new TLSerializationException(string.Format("Expected object of type {0}, actual object type {1}.", SupportedType, obj.GetType())); } WriteHeader(context, modeOverride); WriteBody(obj, context); }
public object Read(TLSerializationContext context, TLSerializationMode? modeOverride = null) { if (modeOverride.HasValue && modeOverride.Value == TLSerializationMode.Bare) { throw new InvalidOperationException("TLMultiConstructorObjectSerializer doesn't support bare type deserialization."); } uint constructorNumber = context.Streamer.ReadUInt32(); ITLSingleConstructorSerializer serializer; if (!_serializersConstructorNumberIndex.TryGetValue(constructorNumber, out serializer)) { throw new NotSupportedException(string.Format("Construction number 0x{0:X} is not supported by this serializer.", constructorNumber)); } return serializer.Read(context, TLSerializationMode.Bare); }
public object Read(TLSerializationContext context, TLSerializationMode?modeOverride = null) { if (modeOverride.HasValue && modeOverride.Value == TLSerializationMode.Bare) { throw new InvalidOperationException("TLMultiConstructorObjectSerializer doesn't support bare type deserialization."); } uint constructorNumber = context.Streamer.ReadUInt32(); ITLSingleConstructorSerializer serializer; if (!_serializersConstructorNumberIndex.TryGetValue(constructorNumber, out serializer)) { throw new NotSupportedException(string.Format("Construction number 0x{0:X} is not supported by this serializer.", constructorNumber)); } return(serializer.Read(context, TLSerializationMode.Bare)); }
protected override Message ReadTypedBody(TLSerializationContext context) { TLStreamer streamer = context.Streamer; ulong msgId = streamer.ReadUInt64(); uint seqNo = streamer.ReadUInt32(); int bodyLength = streamer.ReadInt32(); if (streamer.BytesTillEnd < bodyLength) { throw new TLSerializationException(String.Format("Body length ({0}) is greated than available to read bytes till end ({1}).", bodyLength, streamer.BytesTillEnd)); } object body = TLRig.Deserialize(context); return(new Message(msgId, seqNo, body)); }
protected override void WriteBody(object obj, TLSerializationContext context) { bool hasFlags = false; uint flagsValue = 0; hasFlags = CheckForFlags(obj); if (hasFlags) { var properties = obj.GetType().GetProperties(); flagsValue = (uint)properties[0].GetValue(obj); string binary = Convert.ToString(flagsValue, 2); var maxIndex = binary.Length - 1; for (int i = 0; i < _serializationAgents.Length; i++) { var currentPropertyInfo = properties[i]; var propInfo = currentPropertyInfo.GetCustomAttribute <TLPropertyAttribute>(); //convert the flags to binary if (propInfo.IsFlag) { if (maxIndex < propInfo.Flag) { //since the length is smaller than the flag index, it means this flag was not sent in the msg continue; } if (binary[(int)(maxIndex - propInfo.Flag)] == '0') { //if the flag is set to zero continue; } } ITLPropertySerializationAgent agent = _serializationAgents[i]; agent.Write(obj, context); } } else { for (int i = 0; i < _serializationAgents.Length; i++) { ITLPropertySerializationAgent agent = _serializationAgents[i]; agent.Write(obj, context); } } }
public object Read(TLSerializationContext context, TLSerializationMode? modeOverride = null) { if (modeOverride.HasValue && modeOverride.Value == TLSerializationMode.Bare) { throw new InvalidOperationException("BooleanSerializer doesn't support bare type serialization."); } uint constructorNumber = context.Streamer.ReadUInt32(); if (constructorNumber == TrueConstructorNumber) { return true; } if (constructorNumber == FalseConstructorNumber) { return false; } throw new InvalidOperationException(string.Format("Invalid boolean value: '{0}', or not supported by current BooleanSerializer.", constructorNumber)); }
protected override void WriteBody(object obj, TLSerializationContext context) { bool hasFlags = false; uint flagsValue = 0; hasFlags = CheckForFlags(obj); if (hasFlags) { var properties = obj.GetType().GetProperties(); flagsValue = (uint)properties[0].GetValue(obj); string binary = Convert.ToString(flagsValue, 2); var maxIndex = binary.Length - 1; for (int i = 0; i < _serializationAgents.Length; i++) { var currentPropertyInfo = properties[i]; var propInfo = currentPropertyInfo.GetCustomAttribute<TLPropertyAttribute>(); //convert the flags to binary if (propInfo.IsFlag) { if (maxIndex < propInfo.Flag) {//since the length is smaller than the flag index, it means this flag was not sent in the msg continue; } if (binary[(int)(maxIndex - propInfo.Flag)] == '0') {//if the flag is set to zero continue; } } ITLPropertySerializationAgent agent = _serializationAgents[i]; agent.Write(obj, context); } } else { for (int i = 0; i < _serializationAgents.Length; i++) { ITLPropertySerializationAgent agent = _serializationAgents[i]; agent.Write(obj, context); } } }
public object Read(TLSerializationContext context, TLSerializationMode?modeOverride = null) { if (modeOverride.HasValue && modeOverride.Value == TLSerializationMode.Bare) { throw new InvalidOperationException("BooleanSerializer doesn't support bare type serialization."); } uint constructorNumber = context.Streamer.ReadUInt32(); if (constructorNumber == TrueConstructorNumber) { return(true); } if (constructorNumber == FalseConstructorNumber) { return(false); } throw new InvalidOperationException(string.Format("Invalid boolean value: '{0}', or not supported by current BooleanSerializer.", constructorNumber)); }
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); } } }
protected override void WriteTypedBody(Message message, TLSerializationContext context) { TLStreamer streamer = context.Streamer; streamer.WriteUInt64(message.MsgId); streamer.WriteUInt32(message.Seqno); // Skip 4 bytes for a body length. streamer.Position += 4; long bodyStartPosition = streamer.Position; TLRig.Serialize(message.Body, context, TLSerializationMode.Boxed); long bodyEndPosition = streamer.Position; long bodyLength = bodyEndPosition - bodyStartPosition; streamer.Position = bodyStartPosition - 4; // Write a body length. streamer.WriteInt32((int)bodyLength); streamer.Position = bodyEndPosition; }
protected override object ReadBody(TLSerializationContext context) { byte[] bytes = context.Streamer.ReadTLBytes(); return Encoding.GetString(bytes, 0, bytes.Length); }
protected override object ReadBody(TLSerializationContext context) { return(ReadBodyInternal(context, DefaultItemsSerializationMode)); }
protected override void WriteBody(object obj, TLSerializationContext context) { context.Streamer.WriteTLBytes((byte[]) obj); }
protected override void WriteValue(object propertyValue, TLSerializationContext context) { _serializer.Write(propertyValue, context, TLPropertyInfo.SerializationModeOverride, _itemsSerializationModeOverride); }
protected override void WriteValue(object propertyValue, TLSerializationContext context) { TLRig.Serialize(propertyValue, context, TLSerializationMode.Boxed); }
protected abstract void WriteValue(object propertyValue, TLSerializationContext context);
public void Write(object obj, TLSerializationContext context) { object propertyValue = obj.GetType().GetProperty(_propertyName).GetValue(obj); WriteValue(propertyValue, context); }
protected override object ReadBody(TLSerializationContext context) { return context.Streamer.ReadInt256(); }
protected override object ReadBody(TLSerializationContext context) { bool hasFlags = false; uint flagsValue = 0; object obj = Activator.CreateInstance(_objectType); hasFlags = CheckForFlags(obj); if (hasFlags) { var properties = obj.GetType().GetProperties(); ITLPropertySerializationAgent flagAgent = _serializationAgents[0]; //deserialize anf read the flags flagAgent.Read(obj, context); flagsValue = (uint)properties[0].GetValue(obj); //convert it to binary string binary = Convert.ToString(flagsValue, 2); //the length of the binary is the maximum index of flasg in the message, //anything greater than this index, is useless and should directly exit var maxIndex = binary.Length - 1; for (int i = 1; i < _serializationAgents.Length; i++) { var currentPropertyInfo = properties[i]; var propInfo = currentPropertyInfo.GetCustomAttribute<TLPropertyAttribute>(); //convert the flags to binary if (propInfo.IsFlag) { if (maxIndex < propInfo.Flag) {//since the length is smaller than the flag index, it means this flag was not sent in the msg continue; } if (binary[(int)(maxIndex - propInfo.Flag)] == '0') {//if the flag is set to zero continue; } } ITLPropertySerializationAgent agent = _serializationAgents[i]; agent.Read(obj, context); } } else { for (int i = 0; i < _serializationAgents.Length; i++) { ITLPropertySerializationAgent agent = _serializationAgents[i]; agent.Read(obj, context); } } return obj; }
public void Write(object obj, TLSerializationContext context, TLSerializationMode? modeOverride = null) { var value = (bool) obj; context.Streamer.WriteUInt32(value ? TrueConstructorNumber : FalseConstructorNumber); }
protected override object ReadBody(TLSerializationContext context) { return(context.Streamer.ReadInt256()); }
/// <summary> /// Write a body. /// </summary> /// <param name="obj">Object to be serialized.</param> /// <param name="context">Serialization context.</param> protected abstract void WriteBody(object obj, TLSerializationContext context);
protected override void WriteBody(object obj, TLSerializationContext context) { WriteBodyInternal(obj, context, DefaultItemsSerializationMode); }
protected override void WriteBody(object obj, TLSerializationContext context) { context.Streamer.WriteDouble((double) obj); }
/// <summary> /// Reads a body. /// </summary> /// <param name="context">Serialization context.</param> /// <returns>Body of an object.</returns> protected abstract object ReadBody(TLSerializationContext context);
protected override void WriteBody(object obj, TLSerializationContext context) { context.Streamer.WriteInt256((Int256) obj); }
/// <summary> /// Base serializer writes only header with type id. Then calls <see cref="WriteBody" />. /// </summary> /// <param name="obj">Object to be serialized.</param> /// <param name="context">Serialization context.</param> /// <param name="modeOverride">Override of default type serialization mode.</param> public virtual void Write(object obj, TLSerializationContext context, TLSerializationMode?modeOverride = null) { WriteHeader(context, modeOverride); WriteBody(obj, context); }
public void Read(object obj, TLSerializationContext context) { object value = ReadValue(context); obj.GetType().GetProperty(_propertyName).SetValue(obj, value); }
public virtual object Read(TLSerializationContext context, TLSerializationMode?modeOverride = null) { ReadAndCheckConstructorNumber(context, modeOverride); return(ReadBody(context)); }
protected abstract object ReadValue(TLSerializationContext context);
public void Write(object vector, TLSerializationContext context, TLSerializationMode?modeOverride, TLSerializationMode?itemsModeOverride) { WriteHeader(context, modeOverride); WriteBodyInternal(vector, context, itemsModeOverride); }
protected override object ReadValue(TLSerializationContext context) { return TLRig.Deserialize<object>(context, TLSerializationMode.Boxed); }
protected override void WriteBody(object obj, TLSerializationContext context) { context.Streamer.WriteUInt64((ulong) obj); }
protected override object ReadValue(TLSerializationContext context) { return _serializer.Read(context, TLPropertyInfo.SerializationModeOverride, _itemsSerializationModeOverride); }
/// <summary> /// Reads a body. /// </summary> /// <param name="context">Serialization context.</param> /// <returns>Body of an object.</returns> protected abstract object ReadBody(TLSerializationContext context);
/// <summary> /// Write a body. /// </summary> /// <param name="obj">Object to be serialized.</param> /// <param name="context">Serialization context.</param> protected abstract void WriteBody(object obj, TLSerializationContext context);
public object Read(TLSerializationContext context, TLSerializationMode?modeOverride, TLSerializationMode?itemsModeOverride) { ReadAndCheckConstructorNumber(context, modeOverride); return(ReadBodyInternal(context, itemsModeOverride)); }
protected override void WriteBody(object obj, TLSerializationContext context) { context.Streamer.WriteInt256((Int256)obj); }
public virtual object Read(TLSerializationContext context, TLSerializationMode? modeOverride = null) { ReadAndCheckConstructorNumber(context, modeOverride); return ReadBody(context); }