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 = defType; var keyTypeId = inStream.ReadUInt32(); var keyType = keyTypeId.ToType(); object key = SerializerFactory.GetSerializer(keyType).ReadObject(inStream, defType.GetGenericArguments()[0]); object value = null; var valueTypeId = inStream.ReadUInt32(); var valueType = valueTypeId.ToType(); if (valueTypeId != 0) { value = SerializerFactory.GetSerializer(valueType).ReadObject(inStream, defType.GetGenericArguments()[1]); } object obj = Activator.CreateInstance(type, key, value); inStream.SetRef(hashcode, obj); return(obj); }
public override void WriteObject(object obj, Com.Bj58.Spat.Gaea.Serializer.Component.GaeaStream outStream) { outStream.WriteUInt32(obj.GetType().GetTypeId()); string value = obj.ToString(); SerializerFactory.GetSerializer(typeof(string)).WriteObject(value, outStream); }
public override void WriteObject(object obj, GaeaStream outStream) { if (obj == null) { SerializerFactory.GetSerializer(null).WriteObject(null, outStream); } IDictionary dic = (IDictionary)obj; Type type = typeof(IDictionary); uint typeId = type.GetTypeId(); outStream.WriteUInt32(typeId); if (outStream.WriteRef(obj)) { return; } outStream.WriteInt32(dic.Count); foreach (DictionaryEntry item in dic) { var keyType = item.Key.GetType(); outStream.Write(keyType.GetTypeId().GetBytes(), 0, 4); SerializerFactory.GetSerializer(keyType).WriteObject(item.Key, outStream); if (item.Value == null) { SerializerFactory.GetSerializer(null).WriteObject(item.Value, outStream); } else { var valueType = item.Value.GetType(); outStream.Write(valueType.GetTypeId().GetBytes(), 0, 4); SerializerFactory.GetSerializer(valueType).WriteObject(item.Value, outStream); } } }
public override void WriteObject(object obj, GaeaStream outStream) { if (obj == null) { SerializerFactory.GetSerializer(null).WriteObject(null, outStream); } Type type = obj.GetType(); uint typeId = type.GetTypeId(); outStream.WriteUInt32(typeId); if (outStream.WriteRef(obj)) { return; } object key = type.GetProperty("Key").GetValue(obj, null); object value = type.GetProperty("Value").GetValue(obj, null); var keyType = key.GetType(); outStream.Write(keyType.GetTypeId().GetBytes(), 0, 4); SerializerFactory.GetSerializer(keyType).WriteObject(key, outStream); if (value == null) { SerializerFactory.GetSerializer(null).WriteObject(value, outStream); } else { var valueType = value.GetType(); outStream.Write(valueType.GetTypeId().GetBytes(), 0, 4); SerializerFactory.GetSerializer(valueType).WriteObject(value, outStream); } }
public override void WriteObject(object obj, GaeaStream outStream) { if (obj == null) { SerializerFactory.GetSerializer(null).WriteObject(null, outStream); } IList list = (IList)obj; Type type = typeof(IList); uint typeId = type.GetTypeId(); outStream.WriteUInt32(typeId); if (outStream.WriteRef(obj)) { return; } outStream.WriteInt32(list.Count); foreach (var item in list) { if (item == null) { SerializerFactory.GetSerializer(null).WriteObject(item, outStream); } else { var itemType = item.GetType(); outStream.Write(itemType.GetTypeId().GetBytes(), 0, 4); SerializerFactory.GetSerializer(itemType).WriteObject(item, outStream); } } }
public override object ReadObject(Com.Bj58.Spat.Gaea.Serializer.Component.GaeaStream inStream, Type defType) { uint typeId = inStream.ReadUInt32(); Type type = typeId.ToType(); String value = Convert.ToString(SerializerFactory.GetSerializer(typeof(string)).ReadObject(inStream, typeof(string))); return(Enum.Parse(type, value)); }
public override object ReadObject(GaeaStream inStream, Type defType) { uint typeId = inStream.ReadUInt32(); if (typeId == 0) { return(null); } byte isRef = (byte)inStream.ReadByte(); int hashCode = inStream.ReadInt32(); if (isRef > 0) { return(inStream.GetRef(hashCode)); } int len = inStream.ReadInt32(); Type type = typeId.ToType(); if (type == typeof(byte)) { byte[] buffer = new byte[len]; inStream.SafeRead(buffer, 0, len); return(buffer); } if (type.IsGenericType) { type = GenericHelper.GetRealGenType(type, defType.GetElementType()); } var array = Array.CreateInstance(type, len); if (!type.IsPrimitive) { for (int i = 0; i < len; i++) { var itemTypeId = inStream.ReadUInt32(); if (itemTypeId == 0) { array.SetValue(null, i); } else { Type itemType = itemTypeId.ToType(); var value = SerializerFactory.GetSerializer(itemType).ReadObject(inStream, defType.GetElementType()); array.SetValue(value, i); } } } else { for (int i = 0; i < len; i++) { var value = SerializerFactory.GetSerializer(type).ReadObject(inStream, defType.GetElementType()); array.SetValue(value, i); } } inStream.SetRef(hashCode, array); return(array); }
public override object ReadObject(Com.Bj58.Spat.Gaea.Serializer.Component.GaeaStream inStream, Type defType) { object value = SerializerFactory.GetSerializer(typeof(string)).ReadObject(inStream, typeof(string)); if (value != null) { return(decimal.Parse(value.ToString())); } return(default(decimal)); }
public override void WriteObject(object obj, GaeaStream outStream) { if (obj == null) { SerializerFactory.GetSerializer(null).WriteObject(null, outStream); return; } Array array = (Array)obj; Type t = obj.GetType(); Type type = t.GetElementType(); uint typeId = type.GetTypeId(); outStream.WriteUInt32(typeId); if (outStream.WriteRef(obj)) { return; } outStream.WriteInt32(array.Length); if (t == (typeof(byte))) { outStream.Write((byte[])obj, 0, array.Length); return; } if (!type.IsPrimitives()) { foreach (var item in array) { if (item == null) { SerializerFactory.GetSerializer(null).WriteObject(item, outStream); //if item is null write 0x00000000 to type } else { Type itemT = item.GetType(); uint itemTypeId = itemT.GetTypeId(); outStream.WriteUInt32(itemTypeId); SerializerFactory.GetSerializer(itemT).WriteObject(item, outStream); } } } else { foreach (var item in array) { SerializerFactory.GetSerializer(type).WriteObject(item, outStream); } } }
public override void WriteObject(object obj, GaeaStream outStream) { if (obj == null) { SerializerFactory.GetSerializer(null).WriteObject(null, outStream); return; } Type type = obj.GetType(); TypeInfo typeInfo = GetTypeInfo(type); var typId = typeInfo.TypeId.GetBytes(); outStream.Write(typId, 0, 4); if (outStream.WriteRef(obj)) { return; } foreach (var p in typeInfo.Properies) { var value = p.GetValue(obj, null); if (value == null) { SerializerFactory.GetSerializer(null).WriteObject(value, outStream); } else { outStream.WriteUInt32(value.GetType().GetTypeId()); if (value is IGaeaSerializer) { ((IGaeaSerializer)value).Serialize(outStream); } else { try { SerializerFactory.GetSerializer(value.GetType()).WriteObject(value, outStream); } catch (Exception err) { var e = new SerializeException(err); e.Type = type; e.PropertyName = p.Name; throw e; } } } } }
public object Derialize(byte[] buffer, Type type) { using (GaeaStream stream = new GaeaStream(buffer)) { stream.Encoder = _encoder; if (type.GetInterface("IGaeaSerializer") != null) { var obj = ((IGaeaSerializer)Activator.CreateInstance(type)); obj.Derialize(stream); return(obj); } else { object obj = SerializerFactory.GetSerializer(type).ReadObject(stream, type); return(obj); } } }
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); }
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); }
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); }
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); }