public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
                                                        ConcurrentDictionary <Type, ValueSerializer> typeMapping)
        {
            var preserveObjectReferences = serializer.Options.PreserveObjectReferences;
            var ser = new ObjectSerializer(type);

            typeMapping.TryAdd(type, ser);
            var elementSerializer = serializer.GetSerializerByType(typeof(DictionaryEntry));

            ObjectReader reader = (stream, session) =>
            {
                throw new NotSupportedException("Generic IDictionary<TKey,TValue> are not yet supported");
#pragma warning disable CS0162 // Unreachable code detected
                var instance = Activator.CreateInstance(type);
#pragma warning restore CS0162 // Unreachable code detected
                if (preserveObjectReferences)
                {
                    session.TrackDeserializedObject(instance);
                }
                var count   = stream.ReadInt32(session);
                var entries = new DictionaryEntry[count];
                for (var i = 0; i < count; i++)
                {
                    var entry = (DictionaryEntry)stream.ReadObject(session);
                    entries[i] = entry;
                }
                //TODO: populate dictionary
                return(instance);
            };

            ObjectWriter writer = (stream, obj, session) =>
            {
                if (preserveObjectReferences)
                {
                    session.TrackSerializedObject(obj);
                }
                var dict = obj as IDictionary;

                /*
                 * if (dict == null)
                 * {
                 *  Console.ForegroundColor = ConsoleColor.Yellow;
                 *  Console.WriteLine(obj.GetType().FullName);
                 *  return;
                 * }
                 * //*/
                // ReSharper disable once PossibleNullReferenceException
                Int32Serializer.WriteValueImpl(stream, dict.Count, session);
                foreach (var item in dict)
                {
                    stream.WriteObject(item, typeof(DictionaryEntry), elementSerializer,
                                       serializer.Options.PreserveObjectReferences, session);
                    // elementSerializer.WriteValue(stream,item,session);
                }
            };

            ser.Initialize(reader, writer);

            return(ser);
        }
Exemplo n.º 2
0
        private static void WriteValues <T>(T[] array, Stream stream, Type elementType, ValueSerializer elementSerializer, SerializerSession session)
        {
            Int32Serializer.WriteValueImpl(stream, array.Length, session);
            var preserveObjectReferences = session.Serializer.Options.PreserveObjectReferences;

            foreach (var value in array)
            {
                stream.WriteObject(value, elementType, elementSerializer, preserveObjectReferences, session);
            }
        }
Exemplo n.º 3
0
        private static void WriteValues <T>(LinkedList <T> list, Stream stream, Type elementType, ValueSerializer elementSerializer, SerializerSession session)
        {
            Int32Serializer.WriteValueImpl(stream, list.Count, session);
            var preserveObjectReferences = session.Serializer.Options.PreserveObjectReferences;

            foreach (var value in list)
            {
                stream.WriteObject(value, elementType, elementSerializer, preserveObjectReferences, session);
            }
        }
        public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
                                                        ConcurrentDictionary <Type, ValueSerializer> typeMapping)
        {
            var ser = new ObjectSerializer(type);

            if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index))
            {
                var wrapper = new KnownTypeObjectSerializer(ser, index);
                typeMapping.TryAdd(type, wrapper);
            }
            else
            {
                typeMapping.TryAdd(type, ser);
            }
            var          elementSerializer        = serializer.GetSerializerByType(typeof(DictionaryEntry));
            var          preserveObjectReferences = serializer.Options.PreserveObjectReferences;
            ObjectReader reader = (stream, session) =>
            {
                var count    = stream.ReadInt32(session);
                var instance = (IDictionary)Activator.CreateInstance(type, count);
                if (preserveObjectReferences)
                {
                    session.TrackDeserializedObject(instance);
                }

                for (var i = 0; i < count; i++)
                {
                    var entry = (DictionaryEntry)stream.ReadObject(session);
                    instance.Add(entry.Key, entry.Value);
                }
                return(instance);
            };

            ObjectWriter writer = (stream, obj, session) =>
            {
                if (preserveObjectReferences)
                {
                    session.TrackSerializedObject(obj);
                }
                var dict = obj as IDictionary;
                // ReSharper disable once PossibleNullReferenceException
                Int32Serializer.WriteValueImpl(stream, dict.Count, session);
                foreach (DictionaryEntry item in dict)
                {
                    stream.WriteObject(item, typeof(DictionaryEntry), elementSerializer,
                                       serializer.Options.PreserveObjectReferences, session);
                    // elementSerializer.WriteValue(stream,item,session);
                }
            };

            ser.Initialize(reader, writer);

            return(ser);
        }
        private static void WriteValues(Array array, Stream stream, Type elementType, ValueSerializer elementSerializer, SerializerSession session)
        {
            for (var i = 0; i < array.Rank; i++)
            {
                Int32Serializer.WriteValueImpl(stream, array.GetLength(i), session);
            }
            var preserveObjectReferences = session.Serializer.Options.PreserveObjectReferences;

            foreach (var value in array)
            {
                stream.WriteObject(value, elementType, elementSerializer, preserveObjectReferences, session);
            }
        }
Exemplo n.º 6
0
 private static void WriteHashSet <T>(HashSet <T> set, Stream stream, SerializerSession session, Type elementType,
                                      ValueSerializer elementSerializer, bool preserveObjectReferences)
 {
     if (preserveObjectReferences)
     {
         session.TrackSerializedObject(set);
     }
     // ReSharper disable once PossibleNullReferenceException
     Int32Serializer.WriteValueImpl(stream, set.Count, session);
     foreach (var item in set)
     {
         stream.WriteObject(item, elementType, elementSerializer, preserveObjectReferences, session);
     }
 }
Exemplo n.º 7
0
        private static void WriteValues <T>(LinkedList <T> llist, Stream stream, Type elementType, ValueSerializer elementSerializer,
                                            SerializerSession session, bool preserveObjectReferences)
        {
            if (preserveObjectReferences)
            {
                session.TrackSerializedObject(llist);
            }

            Int32Serializer.WriteValueImpl(stream, llist.Count, session);
            foreach (var value in llist)
            {
                stream.WriteObject(value, elementType, elementSerializer, preserveObjectReferences, session);
            }
        }
Exemplo n.º 8
0
        private static void WriteValues <T>(T[] array, Stream stream, Type elementType, ValueSerializer elementSerializer,
                                            SerializerSession session, bool preserveObjectReferences)
        {
            if (preserveObjectReferences)
            {
                session.TrackSerializedObject(array);
            }

            Int32Serializer.WriteValueImpl(stream, array.Length, session);
            foreach (var value in array)
            {
                stream.WriteObject(value, elementType, elementSerializer, preserveObjectReferences, session);
            }
        }
Exemplo n.º 9
0
        public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
                                                        ConcurrentDictionary <Type, ValueSerializer> typeMapping)
        {
            var preserveObjectReferences = serializer.Options.PreserveObjectReferences;
            var ser = new ObjectSerializer(type);

            typeMapping.TryAdd(type, ser);
            var elementSerializer = serializer.GetSerializerByType(typeof(DictionaryEntry));

            object Reader(Stream stream, DeserializerSession session)
            {
                var instance = (IDictionary <string, object>)Activator.CreateInstance(type) !;

                if (preserveObjectReferences)
                {
                    session.TrackDeserializedObject(instance);
                }
                var count = stream.ReadInt32(session);

                for (var i = 0; i < count; i++)
                {
                    var entry = (KeyValuePair <string, object>)stream.ReadObject(session);
                    instance.Add(entry);
                }

                return(instance);
            }

            void Writer(Stream stream, object obj, SerializerSession session)
            {
                if (preserveObjectReferences)
                {
                    session.TrackSerializedObject(obj);
                }
                var dict = (IDictionary <string, object>)obj;

                // ReSharper disable once PossibleNullReferenceException
                Int32Serializer.WriteValueImpl(stream, dict.Count, session);
                foreach (var item in dict)
                {
                    stream.WriteObject(item, typeof(DictionaryEntry), elementSerializer,
                                       serializer.Options.PreserveObjectReferences, session);
                }
                // elementSerializer.WriteValue(stream,item,session);
            }

            ser.Initialize(Reader, Writer);

            return(ser);
        }
        public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
                                                        ConcurrentDictionary <Type, ValueSerializer> typeMapping)
        {
            var x = new ObjectSerializer(type);

            typeMapping.TryAdd(type, x);
            var preserveObjectReferences = serializer.Options.PreserveObjectReferences;

            var elementType       = GetEnumerableType(type) ?? typeof(object);
            var elementSerializer = serializer.GetSerializerByType(elementType);

            var typeName        = type.Name;
            var genericSufixIdx = typeName.IndexOf('`');

            typeName = genericSufixIdx != -1 ? typeName.Substring(0, genericSufixIdx) : typeName;
            var creatorType =
                Type.GetType(ImmutableCollectionsNamespace + "." + typeName + ", " + ImmutableCollectionsAssembly);

            var genericTypes = elementType.GetTypeInfo().IsGenericType
                   ? elementType.GetTypeInfo().GetGenericArguments()
                   : new[] { elementType };

            // if creatorType == null it means that type is probably an interface
            // we propagate null to create mock serializer - it won't be used anyway

            var stackTypeDef   = Type.GetType(ImmutableCollectionsNamespace + ".IImmutableStack`1, " + ImmutableCollectionsAssembly, true);
            var stackInterface = stackTypeDef.MakeGenericType(genericTypes[0]);

            var isStack = stackInterface.IsAssignableFrom(type);

            var createRange = creatorType != null
                ? creatorType.GetTypeInfo().GetMethods(BindingFlags.Public | BindingFlags.Static)
                              .First(methodInfo => methodInfo.Name == "CreateRange" && methodInfo.GetParameters().Length == 1)
                              .MakeGenericMethod(genericTypes)
                : null;

            ObjectWriter writer = (stream, o, session) =>
            {
                var enumerable = o as ICollection;
                if (enumerable == null)
                {
                    // object can be IEnumerable but not ICollection i.e. ImmutableQueue
                    var e    = (IEnumerable)o;
                    var list = e.Cast <object>().ToList();//

                    enumerable = list;
                }
                Int32Serializer.WriteValueImpl(stream, enumerable.Count, session);
                foreach (var value in enumerable)
                {
                    stream.WriteObject(value, elementType, elementSerializer, preserveObjectReferences, session);
                }
                if (preserveObjectReferences)
                {
                    session.TrackSerializedObject(o);
                }
            };
            ObjectReader reader;

            if (isStack)
            {
                // if we are dealing with stack, we need to apply arguments in reverse order
                reader = (stream, session) =>
                {
                    var count = stream.ReadInt32(session);
                    var items = Array.CreateInstance(elementType, count);
                    for (var i = 0; i < count; i++)
                    {
                        var value = stream.ReadObject(session);
                        items.SetValue(value, count - i - 1);
                    }

                    var instance = createRange.Invoke(null, new object[] { items });
                    if (preserveObjectReferences)
                    {
                        session.TrackDeserializedObject(instance);
                    }
                    return(instance);
                };
            }
            else
            {
                reader = (stream, session) =>
                {
                    var count = stream.ReadInt32(session);
                    var items = Array.CreateInstance(elementType, count);
                    for (var i = 0; i < count; i++)
                    {
                        var value = stream.ReadObject(session);
                        items.SetValue(value, i);
                    }

                    var instance = createRange.Invoke(null, new object[] { items });
                    if (preserveObjectReferences)
                    {
                        session.TrackDeserializedObject(instance);
                    }
                    return(instance);
                };
            }
            x.Initialize(reader, writer);
            return(x);
        }
Exemplo n.º 11
0
 public static void WriteLengthEncodedByteArray(this Stream self, byte[] bytes, SerializerSession session)
 {
     Int32Serializer.WriteValueImpl(self, bytes.Length, session);
     self.Write(bytes, 0, bytes.Length);
 }
Exemplo n.º 12
0
        public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
                                                        ConcurrentDictionary <Type, ValueSerializer> typeMapping)
        {
            var x = new ObjectSerializer(type);

            typeMapping.TryAdd(type, x);
            var preserveObjectReferences = serializer.Options.PreserveObjectReferences;

            var elementType       = GetEnumerableType(type) ?? typeof(object);
            var elementSerializer = serializer.GetSerializerByType(elementType);

            var countProperty = type.GetTypeInfo().GetProperty("Count");
            var addRange      = type.GetTypeInfo().GetMethod("AddRange");
            var add           = type.GetTypeInfo().GetMethod("Add");

            Func <object, int> countGetter = o => (int)countProperty.GetValue(o);


            ObjectReader reader = (stream, session) =>
            {
                var instance = Activator.CreateInstance(type);
                if (preserveObjectReferences)
                {
                    session.TrackDeserializedObject(instance);
                }

                var count = stream.ReadInt32(session);

                if (addRange != null)
                {
                    var items = Array.CreateInstance(elementType, count);
                    for (var i = 0; i < count; i++)
                    {
                        var value = stream.ReadObject(session);
                        items.SetValue(value, i);
                    }
                    //HACK: this needs to be fixed, codegenerated or whatever

                    addRange.Invoke(instance, new object[] { items });
                    return(instance);
                }
                if (add != null)
                {
                    for (var i = 0; i < count; i++)
                    {
                        var value = stream.ReadObject(session);
                        add.Invoke(instance, new[] { value });
                    }
                }


                return(instance);
            };

            ObjectWriter writer = (stream, o, session) =>
            {
                if (preserveObjectReferences)
                {
                    session.TrackSerializedObject(o);
                }
                Int32Serializer.WriteValueImpl(stream, countGetter(o), session);
                var enumerable = o as IEnumerable;
                // ReSharper disable once PossibleNullReferenceException
                foreach (var value in enumerable)
                {
                    stream.WriteObject(value, elementType, elementSerializer, preserveObjectReferences, session);
                }
            };

            x.Initialize(reader, writer);
            return(x);
        }
        public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
                                                        CachedReadConcurrentDictionary <Type, ValueSerializer> typeMapping)
        {
            var preserveObjectReferences = serializer.Options.PreserveObjectReferences;
            var ser = new ObjectSerializer(type);

            typeMapping.TryAdd(type, ser);
            var dictionaryTypes   = GetKeyValuePairType(type);
            var elementSerializer = serializer.GetSerializerByType(dictionaryTypes.KeyValuePairType);

            ObjectReader reader = (stream, session) =>
            {
                object instance;
                try
                {
                    instance = Activator.CreateInstance(type, true); // IDictionary<TKey, TValue>
                }
                catch (Exception)
                {
                    instance = Activator.CreateInstance(type); // IDictionary<TKey, TValue>
                }

                if (preserveObjectReferences)
                {
                    session.TrackDeserializedObject(instance);
                }
                var count = stream.ReadInt32(session);
                for (var i = 0; i < count; i++)
                {
                    var entry = stream.ReadObject(session); // KeyValuePair<TKey, TValue>

                    // Get entry.Key and entry.Value
                    var key   = dictionaryTypes.KeyValuePairType.GetProperty(nameof(KeyValuePair <object, object> .Key)).GetValue(entry, null);
                    var value = dictionaryTypes.KeyValuePairType.GetProperty(nameof(KeyValuePair <object, object> .Value)).GetValue(entry, null);

                    // Same as: instance.Add(key, value)
                    dictionaryTypes.DictionaryInterfaceType
                    .GetMethod(nameof(IDictionary <object, object> .Add), new[] { dictionaryTypes.KeyType, dictionaryTypes.ValueType })
                    .Invoke(instance, new[] { key, value });
                }

                return(instance);
            };

            void writer(System.IO.Stream stream, object obj, SerializerSession session)
            {
                if (preserveObjectReferences)
                {
                    session.TrackSerializedObject(obj);
                }

                var dict  = obj as IEnumerable; // IDictionary<T, V> is IEnumerable<KeyValuePair<T, V>>
                var count = dict.Cast <object>().Count();

                // ReSharper disable once PossibleNullReferenceException
                Int32Serializer.WriteValueImpl(stream, count, session);
                foreach (var item in dict)
                {
                    stream.WriteObject(item, dictionaryTypes.KeyValuePairType, elementSerializer, serializer.Options.PreserveObjectReferences, session);
                }
            }

            ser.Initialize(reader, writer);

            return(ser);
        }
        public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
                                                        ConcurrentDictionary <Type, ValueSerializer> typeMapping)
        {
            var x = new ObjectSerializer(type);

            typeMapping.TryAdd(type, x);

            var preserveObjectReferences = serializer.Options.PreserveObjectReferences;

            var elementType       = GetEnumerableType(type) ?? typeof(object);
            var elementSerializer = serializer.GetSerializerByType(elementType);

            var countProperty = type.GetTypeInfo().GetProperty("Count");

            var addMethod             = type.GetTypeInfo().GetMethod("Add", BindingFlagsEx.All);
            var enumerableConstructor = GetEnumerableConstructor(type);

            Func <object, int> countGetter = o => (int)countProperty.GetValue(o);
            ObjectReader       reader      = null;

            if (HasParameterlessConstructor(type) && addMethod != null)
            {
                var add = CompileMethodToDelegate(addMethod, type, elementType);
                reader = (stream, session) =>
                {
                    var instance = Activator.CreateInstance(type, true);
                    if (preserveObjectReferences)
                    {
                        session.TrackDeserializedObject(instance);
                    }
                    var count = stream.ReadInt32(session);
                    for (var i = 0; i < count; i++)
                    {
                        var value = stream.ReadObject(session);
                        add(instance, value);
                    }
                    return(instance);
                };
            }
            else if (enumerableConstructor != null)
            {
                var construct = CompileCtorToDelegate(enumerableConstructor, elementType.MakeArrayType());
                reader = (stream, session) =>
                {
                    var count = stream.ReadInt32(session);
                    var items = Array.CreateInstance(elementType, count);
                    for (var i = 0; i < count; i++)
                    {
                        var value = stream.ReadObject(session);
                        items.SetValue(value, i);
                    }
                    var instance = construct(items);
                    if (preserveObjectReferences)
                    {
                        session.TrackDeserializedObject(instance);
                    }
                    return(instance);
                };
            }

            ObjectWriter writer = (stream, o, session) =>
            {
                if (preserveObjectReferences)
                {
                    session.TrackSerializedObject(o);
                }
                Int32Serializer.WriteValueImpl(stream, countGetter(o), session);
                var enumerable = o as IEnumerable;
                // ReSharper disable once PossibleNullReferenceException
                foreach (var value in enumerable)
                {
                    stream.WriteObject(value, elementType, elementSerializer, preserveObjectReferences, session);
                }
            };

            x.Initialize(reader, writer);
            return(x);
        }
        public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
                                                        ConcurrentDictionary <Type, ValueSerializer> typeMapping)
        {
            var x = new ObjectSerializer(serializer.Options.FieldSelector, type);

            typeMapping.TryAdd(type, x);
            var preserveObjectReferences = serializer.Options.PreserveObjectReferences;

            var elementType       = GetEnumerableType(type) ?? typeof(object);
            var elementSerializer = serializer.GetSerializerByType(elementType);

            var typeName        = type.Name;
            var genericSufixIdx = typeName.IndexOf('`');

            typeName = genericSufixIdx != -1 ? typeName.Substring(0, genericSufixIdx) : typeName;
            var creatorType =
                Type.GetType(
                    ImmutableCollectionsNamespace + "." + typeName + ", " + ImmutableCollectionsAssembly, true);

            var genericTypes = elementType.IsGenericType()
                ? elementType.GetGenericArguments()
                : new[] { elementType };
            var createRange = creatorType.GetMethods(BindingFlags.Public | BindingFlags.Static)
                              .First(methodInfo => methodInfo.Name == "CreateRange" && methodInfo.GetParameters().Length == 1)
                              .MakeGenericMethod(genericTypes);

            ObjectWriter writer = (stream, o, session) =>
            {
                var enumerable = o as ICollection;
                if (enumerable == null)
                {
                    // object can be IEnumerable but not ICollection i.e. ImmutableQueue
                    var e    = (IEnumerable)o;
                    var list = e.Cast <object>().ToList(); //

                    enumerable = list;
                }
                Int32Serializer.WriteValueImpl(stream, enumerable.Count, session);
                foreach (var value in enumerable)
                {
                    stream.WriteObject(value, elementType, elementSerializer, preserveObjectReferences, session);
                }
                if (preserveObjectReferences)
                {
                    session.TrackSerializedObject(o);
                }
            };

            ObjectReader reader = (stream, session) =>
            {
                var count = stream.ReadInt32(session);
                var items = Array.CreateInstance(elementType, count);
                for (var i = 0; i < count; i++)
                {
                    var value = stream.ReadObject(session);
                    items.SetValue(value, i);
                }

                var instance = createRange.Invoke(null, new object[] { items });
                if (preserveObjectReferences)
                {
                    session.TrackDeserializedObject(instance);
                }
                return(instance);
            };

            x.Initialize(reader, writer);
            return(x);
        }
Exemplo n.º 16
0
        public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
                                                        ConcurrentDictionary <Type, ValueSerializer> typeMapping)
        {
            var preserveObjectReferences = serializer.Options.PreserveObjectReferences;
            var ser = new ObjectSerializer(type);

            typeMapping.TryAdd(type, ser);
            var dictionaryTypes   = GetKeyValuePairType(type);
            var elementSerializer = serializer.GetSerializerByType(dictionaryTypes.KeyValuePairType);

            ObjectReader reader = (stream, session) =>
            {
                object instance;

                void ReadDictionaryKeyValuePairs(object dictionaryInstance)
                {
                    var count = stream.ReadInt32(session);

                    for (var i = 0; i < count; i++)
                    {
                        var entry = stream.ReadObject(session); // KeyValuePair<TKey, TValue>

                        // Get entry.Key and entry.Value
                        var key   = dictionaryTypes.KeyValuePairType.GetProperty(nameof(KeyValuePair <object, object> .Key)).GetValue(entry, null);
                        var value = dictionaryTypes.KeyValuePairType.GetProperty(nameof(KeyValuePair <object, object> .Value)).GetValue(entry, null);

                        // Same as: instance.Add(key, value)
                        dictionaryTypes.DictionaryInterfaceType
                        .GetMethod(nameof(IDictionary <object, object> .Add), new[] { dictionaryTypes.KeyType, dictionaryTypes.ValueType })
                        .Invoke(dictionaryInstance, new[] { key, value });
                    }
                }

                #region Special case for ReadOnlyDictionary
                // Special case for ReadOnlyDictionary since ReadOnlyDictionary
                // does not have a parameterless constructor
                var genericReadOnlyDictionary = typeof(ReadOnlyDictionary <,>);
                var readOnlyDictionaryType    =
                    genericReadOnlyDictionary.MakeGenericType(dictionaryTypes.KeyType, dictionaryTypes.ValueType);
                if (type.Equals(readOnlyDictionaryType))
                {
                    var genericDictionary     = typeof(Dictionary <,>);
                    var genericDictionaryType = genericDictionary.MakeGenericType(dictionaryTypes.KeyType, dictionaryTypes.ValueType);
                    var dictionary            = Activator.CreateInstance(genericDictionaryType); // normal dictionary

                    ReadDictionaryKeyValuePairs(dictionary);
                    instance = Activator.CreateInstance(type, dictionary); // IDictionary<TKey, TValue>
                    if (preserveObjectReferences)
                    {
                        session.TrackDeserializedObject(instance);
                    }
                    return(instance);
                }
                #endregion

                try
                {
                    instance = Activator.CreateInstance(type, true); // IDictionary<TKey, TValue>
                } catch (Exception) {
                    instance = Activator.CreateInstance(type);       // IDictionary<TKey, TValue>
                }

                if (preserveObjectReferences)
                {
                    session.TrackDeserializedObject(instance);
                }
                ReadDictionaryKeyValuePairs(instance);

                return(instance);
            };

            ObjectWriter writer = (stream, obj, session) =>
            {
                if (preserveObjectReferences)
                {
                    session.TrackSerializedObject(obj);
                }

                var dict  = obj as IEnumerable; // IDictionary<T, V> is IEnumerable<KeyValuePair<T, V>>
                var count = dict.Cast <object>().Count();
                // ReSharper disable once PossibleNullReferenceException
                Int32Serializer.WriteValueImpl(stream, count, session);
                foreach (var item in dict)
                {
                    stream.WriteObject(item, dictionaryTypes.KeyValuePairType, elementSerializer, serializer.Options.PreserveObjectReferences, session);
                }
            };

            ser.Initialize(reader, writer);

            return(ser);
        }