Exemple #1
0
 public byte[] Serialize(object obj)
 {
     using (GaeaStream stream = new GaeaStream())
     {
         stream.Encoder = _encoder;
         if (obj == null)
         {
             SerializerFactory.GetSerializer(null).WriteObject(obj, stream);
         }
         else
         {
             if (obj is IGaeaSerializer)
             {
                 ((IGaeaSerializer)obj).Serialize(stream);
             }
             else
             {
                 SerializerFactory.GetSerializer(obj.GetType()).WriteObject(obj, stream);
             }
         }
         return(stream.ToArray());
     }
 }
 public override void WriteObject(object obj, Com.Bj58.Spat.Gaea.Serializer.Component.GaeaStream outStream)
 {
     SerializerFactory.GetSerializer(typeof(string)).WriteObject(obj.ToString(), outStream);
 }
Exemple #3
0
        public override object ReadObject(GaeaStream inStream, Type defType)
        {
            uint typeId = inStream.ReadUInt32();

            if (typeId == 0)
            {
                return(null);
            }
            Type type = typeId.ToType();

            if (type == null)
            {
                throw new NotFindTypeException("Not find the type from current application.type:" + defType.ToString() + ".typeId:" + typeId);
            }
            if (type.IsGenericType)
            {
                type = type.MakeGenericType(defType.GetGenericArguments());
            }
            int fbyte    = inStream.ReadByte();
            int hashcode = inStream.ReadInt32();

            if (fbyte > 0)
            {
                return(inStream.GetRef(hashcode));
            }
            TypeInfo typeInfo = GetTypeInfo(type);
            object   obj      = Activator.CreateInstance(type, true);

            foreach (var p in typeInfo.Properies)
            {
                var ptypeId = inStream.ReadUInt32();
                if (ptypeId == 0)
                {
                    p.SetValue(obj, null, null);
                    continue;
                }
                Type t = ptypeId.ToType();
                if (t == null)
                {
                    throw new NotFindTypeException("Not find the type from current application.type:" + p.PropertyType.Name + ",propery name:" + p.Name);
                }
                if (t.GetInterface("IGaeaSerializer") != null)
                {
                    var value = ((IGaeaSerializer)Activator.CreateInstance(t));
                    value.Derialize(inStream);
                    p.SetValue(obj, value, null);
                }
                else
                {
                    try
                    {
                        var    value  = SerializerFactory.GetSerializer(t).ReadObject(inStream, p.PropertyType);
                        object pValue = value;
                        if (p.PropertyType == typeof(byte))
                        {
                            pValue = Convert.ToByte(value);
                        }
                        else if (p.PropertyType == typeof(int))
                        {
                            pValue = Convert.ToInt32(value);
                        }
                        else if (p.PropertyType == typeof(long))
                        {
                            pValue = Convert.ToInt64(value);
                        }
                        p.SetValue(obj, pValue, null);
                    }
                    catch (Exception err)
                    {
                        var e = new SerializeException(err);
                        e.Type         = type;
                        e.PropertyName = p.Name;
                        throw e;
                    }
                }
            }
            inStream.SetRef(hashcode, obj);
            return(obj);
        }
Exemple #4
0
        public override object ReadObject(GaeaStream inStream, Type defType)
        {
            var typeId = inStream.ReadUInt32();

            if (typeId == 0)
            {
                return(null);
            }
            byte isRef    = (byte)inStream.ReadByte();
            int  hashcode = inStream.ReadInt32();

            if (isRef > 0)
            {
                return(inStream.GetRef(hashcode));
            }
            var type = typeId.ToType();

            if (type == null)
            {
                throw new NotFindTypeException("Type id can't convert to type !typeId:" + typeId);
            }
            if (type != typeof(IDictionary))
            {
                throw new TypeNoMatchException("Type must be IDictionary!type:" + type.FullName);
            }
            if (!defType.IsAbstract && !defType.IsInterface && typeof(IDictionary).IsAssignableFrom(defType))
            {
                type = defType;
            }
            else
            {
                if (defType.IsGenericType)
                {
                    type = typeof(Dictionary <,>).MakeGenericType(defType.GetGenericArguments());
                }
                else
                {
                    type = typeof(Hashtable);
                }
                if (!defType.IsAssignableFrom(type))
                {
                    throw new TypeNoMatchException("Defind type and value type not match !defind type:" + defType.FullName + ",value type:" + type.FullName);
                }
            }
            var         len = inStream.ReadInt32();
            IDictionary obj = (IDictionary)Activator.CreateInstance(type, true);

            Type[] gtypes = null;
            if (type.IsGenericType)
            {
                gtypes = type.GetGenericArguments();
            }
            for (int i = 0; i < len; i++)
            {
                var keyTypeId = inStream.ReadUInt32();
                var keyType   = keyTypeId.ToType();
                var dkeyType  = keyType;
                if (type.IsGenericType)
                {
                    dkeyType = gtypes[0];
                }
                object key             = SerializerFactory.GetSerializer(keyTypeId.ToType()).ReadObject(inStream, dkeyType);
                byte[] valueTypeBuffer = new byte[4];
                inStream.SafeRead(valueTypeBuffer, 0, 4);
                var valueTypeId = valueTypeBuffer.ToUInt32(0);
                if (valueTypeId == 0)
                {
                    obj[key] = null;
                }
                else
                {
                    var valueType  = valueTypeId.ToType();
                    var dvalueType = valueType;
                    if (type.IsGenericType)
                    {
                        dvalueType = gtypes[1];
                    }
                    object value = SerializerFactory.GetSerializer(valueTypeId.ToType()).ReadObject(inStream, dvalueType);
                    obj[key] = value;
                }
            }
            inStream.SetRef(hashcode, obj);
            return(obj);
        }
Exemple #5
0
        public override object ReadObject(GaeaStream inStream, Type defType)
        {
            var typeId = inStream.ReadUInt32();

            if (typeId == 0)
            {
                return(null);
            }
            byte  isRef    = (byte)inStream.ReadByte();
            int   hashcode = inStream.ReadInt32();
            IList obj;

            if (isRef > 0)
            {
                obj = (IList)inStream.GetRef(hashcode);
            }
            else
            {
                var type = typeId.ToType();
                if (type == null)
                {
                    throw new TypeNoMatchException("Type id can't convert to type !typeId:" + typeId);
                }
                if (type != typeof(IList))
                {
                    throw new TypeNoMatchException("Type must be IList!type:" + type.FullName);
                }
                if (!defType.IsAbstract && !defType.IsInterface && typeof(IList).IsAssignableFrom(defType))
                {
                    type = defType;
                }
                else
                {
                    if (defType.IsGenericType)
                    {
                        var gtypes = defType.GetGenericArguments();
                        if (gtypes.Length != 1)
                        {
                            throw new TypeNoMatchException("Defind type Generic parameters length error!deftype:" + defType.FullName);
                        }
                        type = typeof(List <>).MakeGenericType(gtypes);
                    }
                    else
                    {
                        type = typeof(ArrayList);
                    }
                    if (!defType.IsAssignableFrom(type))
                    {
                        throw new TypeNoMatchException("Defind type and value type not match !defind type:" + defType.FullName + ",value type:" + type.FullName);
                    }
                }
                var len = inStream.ReadInt32();
                obj = (IList)Activator.CreateInstance(type, true);
                for (int i = 0; i < len; i++)
                {
                    var itemTypeId = inStream.ReadUInt32();
                    if (itemTypeId == 0)
                    {
                        obj.Add(null);
                    }
                    else
                    {
                        var itemType = itemTypeId.ToType();
                        if (itemType == null)
                        {
                            throw new NotFindTypeException("Not find the type from current application.typeId:" + itemTypeId);
                        }
                        Type dType = itemType;//item define type
                        if (type.IsGenericType)
                        {
                            dType = type.GetGenericArguments()[0];
                        }
                        object value = SerializerFactory.GetSerializer(dType).ReadObject(inStream, dType);
                        obj.Add(value);
                    }
                }
                inStream.SetRef(hashcode, obj);
            }

            return(obj);
        }