public static SerializeTypeInfo ReadFromStream(Stream s)
        {
            UInt16 count    = s.ReadUInt16();
            var    datainfo = new SerializeTypeInfo();

            //FieldInfo
            List <SerializeFieldInfo> fieldDataList = new List <SerializeFieldInfo>();

            for (var i = 0; i < count; i++)
            {
                var fdata = SerializeFieldInfo.ReadFromStream(s);
                fieldDataList.Add(fdata);
            }
            datainfo.FieldData = fieldDataList;

            //CustomTypeInfo
            var customCount = s.ReadByte();

            if (customCount > 0)
            {
                datainfo.CustomDataTypeInfo = new List <SerializeTypeInfo>();
                for (var i = 0; i < customCount; i++)
                {
                    datainfo.CustomDataTypeInfo.Add(ReadFromStream(s));
                }
            }

            return(datainfo);
        }
        public static SerializeTypeInfo Parse(Type type, object t)
        {
            var datainfo = new SerializeTypeInfo();

            datainfo.DataType = type;
            var finfos = GetFieldInfos(type);

            datainfo.FieldData = new List <SerializeFieldInfo>();
            var customDataTypeInfo = new List <SerializeTypeInfo>();

            datainfo.CustomDataTypeInfo = customDataTypeInfo;
            for (var i = 0; i < finfos.Count; i++)
            {
                var finfo           = finfos[i];
                var fieldType       = finfo.FieldType;
                var isArray         = fieldType.IsArray;
                var elementType     = isArray ? fieldType.GetElementType() : fieldType;
                var typeEnum        = GetTypeEnum(elementType);
                var isPrimitive     = typeEnum != SerializeTypeEnum.Custom;
                var customTypeIndex = 0;
                if (isPrimitive)
                {
                }
                else
                {
                    var index = customDataTypeInfo.FindIndex(f => f.DataType == elementType);
                    if (index == -1)
                    {
                        customTypeIndex = customDataTypeInfo.Count;
                        customDataTypeInfo.Add(Parse(elementType, null));
                    }
                    else
                    {
                        customTypeIndex = index;
                    }
                }

                if (customTypeIndex > 255)
                {
                    throw new Exception("Custom type count must under 255");
                }
                var data = new SerializeFieldInfo
                {
                    FieldActive     = true,
                    HasLabel        = false,
                    FieldType       = fieldType,
                    ElementType     = elementType,
                    IsArray         = isArray,
                    IsPrimitive     = isPrimitive,
                    FieldName       = finfo.Name,
                    TypeEnum        = typeEnum,
                    CustomTypeIndex = (byte)customTypeIndex,
                };

                datainfo.FieldData.Add(data);
            }
            return(datainfo);
        }
Пример #3
0
        public static T Deserialize <T>(Stream stream)
        {
            var dataInfo = SerializeTypeInfo.ReadFromStream <T>(stream);
            var refInfo  = SerializeTypeInfo.Parse <T>();

            if (!refInfo.Verify(dataInfo))
            {
                throw new Exception("Invalid type info");
            }
            return(ReadData <T>(stream, refInfo));
        }
Пример #4
0
        public static byte[] Serialize <T>(T t, bool extraInfo = false)
        {
            var stream   = new MemoryStream();
            var dataInfo = SerializeTypeInfo.Parse(t);

            dataInfo.WriteToStream(stream, extraInfo);
            //WriteData

            WriteData(stream, t, dataInfo);

            return(stream.ToArray());
        }
        public static SerializeTypeInfo ReadFromStream <T>(Stream s)
        {
            var typeDataInfo = SerializeTypeInfo.Parse <T>();

            var deserializeDataInfo = ReadFromStream(s);

            if (!typeDataInfo.Verify(deserializeDataInfo))
            {
                throw new Exception("type verify failed!");
            }

            return(deserializeDataInfo);
        }
        public bool Verify(SerializeTypeInfo o)
        {
            if (this.FieldData == null || o.FieldData == null)
            {
                throw new Exception();
            }
            if (this.FieldData.Count != o.FieldData.Count)
            {
                return(false);
            }

            var count = this.FieldData.Count;

            for (var i = 0; i < count; i++)
            {
                if (!FieldData[i].Equals(o.FieldData[i]))
                {
                    return(false);
                }
            }
            return(true);
        }
Пример #7
0
        private static void WriteData(Stream stream, object t, Type type, SerializeTypeInfo dataInfo)
        {
            if (t == null)
            {
                stream.WriteBool(false);
                return;
            }
            else
            {
                stream.WriteBool(true);
            }

            var finfo   = dataInfo.FieldData;
            var refinfo = SerializeTypeInfo.GetFieldInfos(type);

            for (var i = 0; i < finfo.Count; i++)
            {
                var fdata = finfo[i];

                var fvalue = refinfo[i].GetValue(t);
                if (fdata.IsPrimitive)
                {
                    if (fdata.IsArray)
                    {
                        if (fvalue == null)
                        {
                            stream.WriteBool(false);
                        }
                        else
                        {
                            stream.WriteBool(true);
                            var array = fvalue as Array;
                            stream.WriteUInt32((uint)array.Length);
                            if (array.Length != 0)
                            {
                                WritePrimitiveDataArray(stream, fdata.TypeEnum, array);
                            }
                        }
                    }
                    else
                    {
                        WritePrimitiveData(stream, fdata.TypeEnum, fvalue);
                    }
                }
                else
                {
                    if (fvalue == null)
                    {
                        stream.WriteBool(false);
                    }
                    else
                    {
                        stream.WriteBool(true);
                        var customTypeIndex = fdata.CustomTypeIndex;
                        var customTypeInfo  = dataInfo.CustomDataTypeInfo[customTypeIndex];
                        if (fdata.IsArray)
                        {
                            var array = fvalue as Array;
                            stream.WriteInt32(array.Length);
                            if (array.Length != 0)
                            {
                                for (var j = 0; j < array.Length; j++)
                                {
                                    WriteData(stream, array.GetValue(j), fdata.ElementType, customTypeInfo);
                                }
                            }
                        }
                        else
                        {
                            if (customTypeInfo == null)
                            {
                                throw new Exception("Custom datatype info missing");
                            }
                            //write value
                            WriteData(stream, fvalue, fvalue.GetType(), customTypeInfo);
                        }
                    }
                }
            }
        }
Пример #8
0
 private static void WriteData <T>(Stream stream, T t, SerializeTypeInfo dataInfo)
 {
     WriteData(stream, t, typeof(T), dataInfo);
 }
Пример #9
0
        private static object ReadData(Type type, Stream stream, SerializeTypeInfo dataInfo)
        {
            bool objNotNull = stream.ReadBool();

            if (!objNotNull)
            {
                return(null);
            }
            var    fieldInfo = SerializeTypeInfo.GetFieldInfos(type);
            object t         = Activator.CreateInstance(type);

            var finfo = dataInfo.FieldData;

            for (var i = 0; i < finfo.Count; i++)
            {
                var fdata = finfo[i];
                if (fdata.IsPrimitive)
                {
                    if (fdata.IsArray)
                    {
                        bool notNull = stream.ReadBool();
                        if (notNull == false)
                        {
                            fieldInfo[i].SetValue(t, null);
                        }
                        else
                        {
                            var length = stream.ReadUInt32();
                            var ary    = ReadPrimitiveDataArray(stream, fdata.TypeEnum, (int)length);
                            fieldInfo[i].SetValue(t, ary);
                        }
                    }
                    else
                    {
                        var val = ReadPrimitiveData(stream, fdata.TypeEnum);
                        fieldInfo[i].SetValue(t, val);
                    }
                }
                else
                {
                    var notNull = stream.ReadBool();
                    if (notNull)
                    {
                        var customTypeIndex = fdata.CustomTypeIndex;
                        var customTypeInfo  = dataInfo.CustomDataTypeInfo[customTypeIndex];
                        if (fdata.IsArray)
                        {
                            var arylength = stream.ReadInt32();
                            var array     = Array.CreateInstance(fdata.ElementType, arylength);

                            if (arylength == 0)
                            {
                                fieldInfo[i].SetValue(t, array);
                            }
                            else
                            {
                                for (var j = 0; j < arylength; j++)
                                {
                                    var obj = ReadData(fdata.ElementType, stream, customTypeInfo);
                                    array.SetValue(obj, j);
                                }
                                fieldInfo[i].SetValue(t, array);
                            }
                        }
                        else
                        {
                            if (customTypeInfo == null)
                            {
                                throw new Exception("Custom datatype info missing");
                            }
                            var eobj = ReadData(fdata.FieldType, stream, customTypeInfo);
                            fieldInfo[i].SetValue(t, eobj);
                        }
                    }
                    else
                    {
                        fieldInfo[i].SetValue(t, null);
                    }
                }
            }
            return(t);
        }
Пример #10
0
 private static T ReadData <T>(Stream stream, SerializeTypeInfo dataInfo)
 {
     return((T)ReadData(typeof(T), stream, dataInfo));
 }