/// <summary> /// Initializes a new instance of the <see cref="VersionNotSupportedException"/> class. /// </summary> /// <param name="archive">Serializer archive passed in during serialization.</param> public VersionNotSupportedException(SerializationArchive archive) : base($"Specified serializer version ({archive.Version}) is not supported for type '{archive.DataType.FullName}'.") { Type = archive.DataType; RequestedVersion = archive.Version; MaxVersion = Serializer.GetSerializerVersion(Type); }
/// <summary> /// Serializes the object. /// </summary> /// <param name="archive">Archive to serialize into.</param> /// <param name="stack">The queue to serialize.</param> /// <exception cref="VersionNotSupportedException">Serializer version is not supported.</exception> public override void Serialize(SerializationArchive archive, Stack <T> stack) { if (archive.Version == 1) { // write number of items int count = stack.Count; archive.Write(count); // write items (bottom up) var buffer = ArrayPool <T> .Shared.Rent(count); try { stack.CopyTo(buffer, 0); Array.Reverse(buffer, 0, count); for (int i = 0; i < count; i++) { archive.Write(buffer[i], archive.Context); } } finally { ArrayPool <T> .Shared.Return(buffer); } return; } throw new VersionNotSupportedException(archive); }
/// <summary> /// Serializes the object. /// </summary> /// <param name="archive">Archive to serialize into.</param> /// <param name="converter">The converter to serialize.</param> /// <exception cref="VersionNotSupportedException">Serializer version is not supported.</exception> public override void Serialize(SerializationArchive archive, IConverter converter) { if (archive.Version == 1) { return; } throw new VersionNotSupportedException(archive); }
/// <summary> /// Opens a base archive and calls the serializer of the derived type (for base class serialization). /// </summary> /// <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 context = null) { var baseClassType = DataType.BaseType ?? throw new ArgumentException($"{DataType.FullName} does not have a base class."); // try internal object serializer var ios = Serializer.GetInternalObjectSerializer(mObjectToSerialize, baseClassType, out uint version); if (ios != null) { // consider serializer version overrides... if (mSerializer.GetSerializerVersionOverride(baseClassType, out uint versionOverride)) { version = versionOverride; } // write base archive header var buffer = mBufferWriter.GetSpan(1 + Leb128EncodingHelper.MaxBytesFor32BitValue); int bufferIndex = 0; buffer[bufferIndex++] = (byte)PayloadType.BaseArchiveStart; bufferIndex += Leb128EncodingHelper.Write(buffer.Slice(bufferIndex), version); mBufferWriter.Advance(bufferIndex); // call the Serialize() method of the base class var archive = new SerializationArchive(mSerializer, mBufferWriter, baseClassType, mObjectToSerialize, version, context); var serializeDelegate = Serializer.GetInternalObjectSerializerSerializeCaller(baseClassType); serializeDelegate(ios, archive); return; } // try external object serializer var eos = ExternalObjectSerializerFactory.GetExternalObjectSerializer(baseClassType, out version); if (eos != null) { // consider serializer version overrides... if (mSerializer.GetSerializerVersionOverride(baseClassType, out uint versionOverride)) { version = versionOverride; } // write base archive header var buffer = mBufferWriter.GetSpan(1 + Leb128EncodingHelper.MaxBytesFor32BitValue); int bufferIndex = 0; buffer[bufferIndex++] = (byte)PayloadType.BaseArchiveStart; bufferIndex += Leb128EncodingHelper.Write(buffer.Slice(bufferIndex), version); mBufferWriter.Advance(bufferIndex); // serialize object var archive = new SerializationArchive(mSerializer, mBufferWriter, baseClassType, mObjectToSerialize, version, context); eos.Serialize(archive, mObjectToSerialize); return; } // specified type is not serializable... throw new ArgumentException($"Specified type ({baseClassType.FullName}) is not serializable.", nameof(baseClassType)); }
/// <summary> /// Serializes the object. /// </summary> /// <param name="archive">Archive to serialize into.</param> /// <param name="list">The list to serialize.</param> /// <exception cref="VersionNotSupportedException">Serializer version is not supported.</exception> public override void Serialize(SerializationArchive archive, IList list) { if (archive.Version == 1) { // write number of list items int count = list.Count; archive.Write(count); // write list items for (int i = 0; i < count; i++) { archive.Write(list[i], archive.Context); } return; } throw new VersionNotSupportedException(archive); }
/// <summary> /// Serializes the object. /// </summary> /// <param name="archive">Archive to serialize into.</param> /// <param name="set">The set to serialize.</param> /// <exception cref="VersionNotSupportedException">Serializer version is not supported.</exception> public override void Serialize(SerializationArchive archive, HashSet <T> set) { if (archive.Version == 1) { // write number of items int count = set.Count; archive.Write(count); // write items foreach (var item in set) { archive.Write(item, archive.Context); } return; } throw new VersionNotSupportedException(archive); }
/// <summary> /// Serializes the object. /// </summary> /// <param name="archive">Archive to serialize into.</param> /// <param name="list">The list to serialize.</param> /// <exception cref="VersionNotSupportedException">Serializer version is not supported.</exception> public override void Serialize(SerializationArchive archive, SortedList <TKey, TValue> list) { if (archive.Version == 1) { // write number of list items int count = list.Count; archive.Write(count); // write list items foreach (var pair in list) { archive.Write(pair.Key, archive.Context); archive.Write(pair.Value, archive.Context); } return; } throw new VersionNotSupportedException(archive); }
/// <summary> /// Serializes the object. /// </summary> /// <param name="archive">Archive to serialize into.</param> /// <param name="dictionary">The dictionary to serialize.</param> /// <exception cref="VersionNotSupportedException">Serializer version is not supported.</exception> public override void Serialize(SerializationArchive archive, SortedDictionary <TKey, TValue> dictionary) { if (archive.Version == 1) { // write number of dictionary entries int count = dictionary.Count; archive.Write(count); // write dictionary entries foreach (var pair in dictionary) { archive.Write(pair.Key, archive.Context); archive.Write(pair.Value, archive.Context); } return; } throw new VersionNotSupportedException(archive); }
/// <summary> /// Serializes the object. /// </summary> /// <param name="archive">Archive to serialize into.</param> /// <param name="list">The list to serialize.</param> /// <exception cref="VersionNotSupportedException">Serializer version is not supported.</exception> public override void Serialize(SerializationArchive archive, LinkedList <T> list) { if (archive.Version == 1) { // write number of list items int count = list.Count; archive.Write(count); // write list items var node = list.First; while (node != null) { archive.Write(node.Value, archive.Context); node = node.Next; } return; } throw new VersionNotSupportedException(archive); }
/// <summary> /// Serializes the specified object to the specified archive. /// </summary> /// <param name="archive">Archive to serialize the specified object to.</param> /// <param name="obj">The object to serialize.</param> public abstract void Serialize(SerializationArchive archive, T obj);
/// <summary> /// Serializes the specified object to the specified archive. /// </summary> /// <param name="archive">Archive to serialize the specified object to.</param> /// <param name="obj">The object to serialize.</param> void IExternalObjectSerializer.Serialize(SerializationArchive archive, object obj) { Serialize(archive, (T)obj); }