/// <summary> /// Initializes a new instance of the <see cref="VersionNotSupportedException"/> class. /// </summary> /// <param name="archive">Serializer archive passed in during deserialization.</param> public VersionNotSupportedException(SerializerArchive archive) : base(string.Format("Specified serializer version ({0}) is not supported for type '{1}'.", archive.Version, archive.Type.FullName)) { Type = archive.Type; RequestedVersion = archive.Version; MaxVersion = Serializer.GetSerializerVersion(Type); }
/// <summary> /// Deserializes a GUID from a stream. /// </summary> /// <param name="archive">Archive containing the serialized GUID.</param> /// <returns>Deserialized object (type System.Guid).</returns> /// <exception cref="VersionNotSupportedException">Serializer version is not supported.</exception> public object Deserialize(SerializerArchive archive) { if (archive.Version == 1) { byte[] buffer = (byte[])archive.ReadObject(); return(buffer.ToRfc4122Guid(0)); } throw new VersionNotSupportedException(archive); }
/// <summary> /// Serializes a GUID to a stream. /// </summary> /// <param name="archive">Archive to put the specfied GUID into.</param> /// <param name="version">Requested version when serializing the GUID.</param> /// <param name="obj">Object to serialize (type System.Guid).</param> /// <exception cref="VersionNotSupportedException">Serializer version is not supported.</exception> public void Serialize(SerializerArchive archive, uint version, object obj) { if (version == 1) { Guid guid = (Guid)obj; archive.Write(guid.ToUuidByteArray()); } else { throw new VersionNotSupportedException(typeof(Guid), version); } }
/// <summary> /// Serializes a generic list to a stream. /// </summary> /// <param name="archive">Archive to put the specified list into.</param> /// <param name="version">Serializer version to use.</param> /// <param name="obj">List to serialize.</param> /// <exception cref="VersionNotSupportedException">Serializer version is not supported.</exception> public void Serialize(SerializerArchive archive, uint version, object obj) { if (version == 1) { IList list = (IList)obj; int count = list.Count; archive.Write(count); for (int i = 0; i < count; i++) { archive.Write(list[i], archive.Context); } } else { throw new VersionNotSupportedException(typeof(List <>), version); } }
/// <summary> /// Deserializes a generic list from a stream. /// </summary> /// <param name="archive">Archive containing a serialized generic list object.</param> /// <returns>Deserialized list.</returns> /// <exception cref="VersionNotSupportedException">Serializer version is not supported.</exception> public object Deserialize(SerializerArchive archive) { if (archive.Version == 1) { // read number of elements and set the capacity of the list appropriately to // avoid resizing while populating the list int count = archive.ReadInt32(); // read elements from the archive and put them into the list IList collection = (IList)FastActivator.CreateInstance(archive.Type, count); for (int i = 0; i < count; i++) { object obj = archive.ReadObject(archive.Context); collection.Add(obj); } return(collection); } throw new VersionNotSupportedException(archive); }
/// <summary> /// Openes a base archive and calls the serializer of the specified type (for base class serialization). /// </summary> /// <param name="obj">Object to serialize.</param> /// <param name="type">Type of the base class to serialize.</param> /// <param name="context">Context object to pass to the serializer of the base class.</param> /// <exception cref="ArgumentException">Specified type is not serializable.</exception> public void WriteBaseArchive(object obj, Type type, object context = null) { CloseArchiveStream(); // ensure the specified type is the type of the specified object or the type of one of its base classes if (obj.GetType().IsAssignableFrom(type)) { throw new ArgumentException("Specified type is neither the type of the specified object nor the type of one of its base classes.", "obj,type"); } // try external object serializer uint version; IExternalObjectSerializer eos = Serializer.GetExternalObjectSerializer(type, out version); if (eos != null) { // consider serializer version overrides... uint versionOverride; if (mSerializer.GetSerializerVersionOverride(type, out versionOverride)) { version = versionOverride; } // write base archive header byte[] buffer = mSerializer.mTempBuffer_Buffer; buffer[0] = (byte)PayloadType.BaseArchiveStart; int count = Leb128EncodingHelper.Write(buffer, 1, version); mStream.Write(buffer, 0, 1 + count); // serialize object SerializerArchive archive = new SerializerArchive(mSerializer, mStream, type, version, context); eos.Serialize(archive, version, obj); archive.Close(); return; } // try internal object serializer IInternalObjectSerializer ios = Serializer.GetInternalObjectSerializer(obj, type, out version); if (ios != null) { // consider serializer version overrides... uint versionOverride; if (mSerializer.GetSerializerVersionOverride(type, out versionOverride)) { version = versionOverride; } // write base archive header byte[] buffer = mSerializer.mTempBuffer_Buffer; buffer[0] = (byte)PayloadType.BaseArchiveStart; int count = Leb128EncodingHelper.Write(buffer, 1, version); mStream.Write(buffer, 0, 1 + count); // call the Serialize() method of the base class SerializerArchive archive = new SerializerArchive(mSerializer, mStream, type, version, context); Serializer.IosSerializeDelegate serializeDelegate = Serializer.GetInternalObjectSerializerSerializeCaller(type); serializeDelegate(ios, archive, version); archive.Close(); return; } // specified type is not serializable... string error = string.Format("Specified type ({0}) is not serializable.", type.FullName); throw new ArgumentException(error, nameof(type)); }