Beispiel #1
0
        internal static void Serialize(object obj, BinaryWriter binaryWriter)
        {
            if (obj == null)
            {
                obj = Null;
            }

            var objectType = obj.GetType().GetTypeInfo();


            if (SerializationMap.GetIdByType(objectType, out var typeId))
            {
                binaryWriter.Write(typeId);

                var metadatas = GetMetadatas(objectType);

                ComputeFlags(obj, metadatas);

                Serialize(binaryWriter, obj);
            }
            else
            {
                SerializeValue(binaryWriter, obj, null);
            }
        }
Beispiel #2
0
        public static object Deserialize(BinaryReader binaryReader, TypeInfo typeInfo)
        {
            if (typeInfo == null || typeof(IObject).GetTypeInfo().IsAssignableFrom(typeInfo))
            {
                return(DeserializeObject(binaryReader, typeInfo));
            }

            var serializator = SerializationMap.GetSerializator(typeInfo);

            return(serializator.Deserialize(binaryReader, null));
        }
Beispiel #3
0
        private static IObject DeserializeObject(BinaryReader reader, TypeInfo propertyTypeInfo)
        {
            var id = reader.ReadUInt32();

            if (!SerializationMap.GetTypeById(id, out var typeInfo))
            {
                throw new NotSupportedException();
            }

            if (Equals(typeInfo, NullTypeInfo))
            {
                return(null);
            }

            var metadatas = GetMetadatas(typeInfo);

            var typeForCreate = (propertyTypeInfo != null && propertyTypeInfo.IsClass ? propertyTypeInfo : typeInfo).AsType();
            var obj           = Activator.CreateInstance(typeForCreate);

            foreach (var metadata in metadatas)
            {
                if (metadata.CanSerializeIndex.HasValue)
                {
                    var flags = (BitArray)metadata.CanSerializeSource.GetValue(obj);
                    if (!flags[metadata.CanSerializeIndex.Value])
                    {
                        continue;
                    }
                }

                if (metadata.FromFlagIndex.HasValue)
                {
                    var flags     = (BitArray)metadata.FromFlagSource.GetValue(obj);
                    var flagValue = flags[metadata.FromFlagIndex.Value];
                    metadata.PropertyInfo.SetValue(obj, flagValue);

                    continue;
                }

                var serializator = SerializationMap.GetSerializator(metadata.PropertyTypeInfo);

                var value = serializator.Deserialize(reader, metadata);
                metadata.PropertyInfo.SetValue(obj, value);
            }

            return((IObject)obj);
        }
Beispiel #4
0
        private static void SerializeValue(BinaryWriter binaryWriter, object value, SerializationMetadata metadata)
        {
            ISerializator ser;

            if (value != null)
            {
                ser = SerializationMap.GetSerializator(value.GetType().GetTypeInfo());
            }
            else
            {
                value = Null;
                ser   = SerializationMap.GetSerializator(NullTypeInfo);
            }


            ser.Serialize(binaryWriter, value, metadata);
        }
Beispiel #5
0
        // ReSharper disable once MethodOverloadWithOptionalParameter
        internal static object Deserialize(IByteBuffer buffer, SerializationMetadata metadata)
        {
            if (metadata != null)
            {
                ISerializator ser;
                if (buffer.GetUnsignedIntLE(buffer.ReaderIndex) == TgZipPackageId)
                {
                    buffer.SkipBytes(4);
                    ser = SerializationMap.GetSerializator(typeof(TgZipPacked).GetTypeInfo());
                }
                else
                {
                    ser = SerializationMap.GetSerializator(metadata.PropertyTypeInfo);
                }
                return(ser.Deserialize(buffer, metadata));
            }

            return(Deserialize(buffer, typeof(IObject).GetTypeInfo()));
        }
Beispiel #6
0
        internal static void Serialize(object obj, IByteBuffer buffer, SerializationMetadata metadata = null)
        {
            var ser = SerializationMap.GetSerializator(obj.GetType().GetTypeInfo());

            ser.Serialize(obj, buffer, metadata);
        }
Beispiel #7
0
        internal static object Deserialize(IByteBuffer buffer, TypeInfo typeInfo)
        {
            var ser = SerializationMap.GetSerializator(typeInfo);

            return(ser.Deserialize(buffer, null));
        }
Beispiel #8
0
 static Serializer()
 {
     SerializationMap.GetIdByType(typeof(TgZipPacked).GetTypeInfo(), out TgZipPackageId);
 }