public static TDocument Deserialize <TDocument>(byte[] bytes) where TDocument : AbstractDocument, new() { if (bytes == null) { throw new ArgumentException("Can't deserialize null", "bytes"); } using (MemoryStream input = new MemoryStream(bytes)) { BinaryReader binaryReader = new BinaryReader(input); try { byte b = binaryReader.ReadByte(); if (b != 0) { throw new CorruptionException("Can't deserialize format version " + b); } byte typeId = binaryReader.ReadByte(); SerializedTypeReflection typeReflection = SerializerReflectionCache.GetTypeReflection(typeId); TDocument val = (TDocument)Activator.CreateInstance(typeReflection.Type); val.TypeId = typeId; SerializedFieldReflection[] fieldReflections = typeReflection.FieldReflections; byte b2 = binaryReader.ReadByte(); for (int i = 0; i < b2; i++) { byte b3 = binaryReader.ReadByte(); Type fieldType = GetFieldType(b3); byte b4 = binaryReader.ReadByte(); SerializedFieldReflection fieldInfo = GetFieldInfo(fieldReflections, b4); FieldTypeId fieldTypeId = (FieldTypeId)b3; FieldTypeMapping[] array = fieldTypeMappings; for (int j = 0; j < array.Length; j++) { FieldTypeMapping fieldTypeMapping = array[j]; if (fieldTypeMapping.Id == fieldTypeId) { object value = fieldTypeMapping.Reader(binaryReader); if (fieldInfo != null) { FieldInfo fieldInfo2 = fieldInfo.FieldInfo; Type fieldType2 = fieldInfo2.FieldType; if (fieldType != fieldType2) { throw new CorruptionException(string.Concat("Field ", b4, " has type ", fieldType, " in the data to deserialize and type ", fieldType2, " in the code")); } fieldInfo2.SetValue(val, value); break; } break; } } } return(val); } catch (Exception innerException) { throw new CorruptionException("Unknown error while deserializing", innerException); } } }
private static SerializedTypeReflection CacheTypeReflection(byte id, Type type, SerializedFieldReflection[] fieldReflections) { SerializedTypeReflection serializedTypeReflection = new SerializedTypeReflection(type, id, fieldReflections); typeReflectionsByType.Add(type, serializedTypeReflection); typeReflectionsById.Add(id, serializedTypeReflection); return(serializedTypeReflection); }
private static void AddType(Type type) { SerializedAttribute serializedAttribute = GetSerializedAttribute(type); if (serializedAttribute != null) { AssertNewType(type, serializedAttribute.PrimaryId); AssertTypeExtendsAbstractDocument(type); SerializedTypeReflection value = AddType(type, serializedAttribute.PrimaryId); byte[] alternateIds = serializedAttribute.AlternateIds; foreach (byte key in alternateIds) { typeReflectionsById.Add(key, value); } } }
public static byte[] Serialize(object obj, Type objType) { if (obj == null) { throw new ArgumentException("Can't serialize null", "obj"); } SerializedTypeReflection typeReflection = SerializerReflectionCache.GetTypeReflection(objType); using (MemoryStream memoryStream = new MemoryStream()) { BinaryWriter binaryWriter = new BinaryWriter(memoryStream, Encoding.UTF8); binaryWriter.Write((byte)0); binaryWriter.Write(typeReflection.Id); SerializedFieldReflection[] fieldReflections = typeReflection.FieldReflections; binaryWriter.Write((byte)fieldReflections.Length); SerializedFieldReflection[] array = fieldReflections; foreach (SerializedFieldReflection serializedFieldReflection in array) { FieldInfo fieldInfo = serializedFieldReflection.FieldInfo; Type fieldType = fieldInfo.FieldType; FieldTypeId fieldTypeId = GetFieldTypeId(fieldType); binaryWriter.Write((byte)fieldTypeId); binaryWriter.Write(serializedFieldReflection.Id); object value = fieldInfo.GetValue(obj); FieldTypeMapping[] array2 = fieldTypeMappings; for (int j = 0; j < array2.Length; j++) { FieldTypeMapping fieldTypeMapping = array2[j]; if (fieldTypeMapping.Type == fieldType) { fieldTypeMapping.Writer(value, binaryWriter); break; } } } return(memoryStream.ToArray()); } }