diff --git a/src/Hyperion/SerializerFactories/ArraySerializerFactory.cs b/src/Hyperion/SerializerFactories/ArraySerializerFactory.cs index a4ac76a3..797d635b 100644 --- a/src/Hyperion/SerializerFactories/ArraySerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/ArraySerializerFactory.cs @@ -71,7 +71,14 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type WriteValues((dynamic)arr, stream, elementType, elementSerializer, session); }; arraySerializer.Initialize(reader, writer); - typeMapping.TryAdd(type, arraySerializer); + + if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index)) + { + var wrapper = new KnownTypeObjectSerializer(arraySerializer, index); + typeMapping.TryAdd(type, wrapper); + } + else + typeMapping.TryAdd(type, arraySerializer); return arraySerializer; } } diff --git a/src/Hyperion/SerializerFactories/ConstructorInfoSerializerFactory.cs b/src/Hyperion/SerializerFactories/ConstructorInfoSerializerFactory.cs index 877bc950..6cf7f5e3 100644 --- a/src/Hyperion/SerializerFactories/ConstructorInfoSerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/ConstructorInfoSerializerFactory.cs @@ -32,7 +32,13 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type ConcurrentDictionary typeMapping) { var os = new ObjectSerializer(type); - typeMapping.TryAdd(type, os); + if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index)) + { + var wrapper = new KnownTypeObjectSerializer(os, index); + typeMapping.TryAdd(type, wrapper); + } + else + typeMapping.TryAdd(type, os); ObjectReader reader = (stream, session) => { var owner = stream.ReadObject(session) as Type; diff --git a/src/Hyperion/SerializerFactories/DefaultDictionarySerializerFactory.cs b/src/Hyperion/SerializerFactories/DefaultDictionarySerializerFactory.cs index b7d26ece..29902e95 100644 --- a/src/Hyperion/SerializerFactories/DefaultDictionarySerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/DefaultDictionarySerializerFactory.cs @@ -31,7 +31,13 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type ConcurrentDictionary typeMapping) { var ser = new ObjectSerializer(type); - typeMapping.TryAdd(type, ser); + 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) => diff --git a/src/Hyperion/SerializerFactories/DelegateSerializerFactory.cs b/src/Hyperion/SerializerFactories/DelegateSerializerFactory.cs index 850233df..6181c81e 100644 --- a/src/Hyperion/SerializerFactories/DelegateSerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/DelegateSerializerFactory.cs @@ -31,7 +31,13 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type ConcurrentDictionary typeMapping) { var os = new ObjectSerializer(type); - typeMapping.TryAdd(type, os); + if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index)) + { + var wrapper = new KnownTypeObjectSerializer(os, index); + typeMapping.TryAdd(type, wrapper); + } + else + typeMapping.TryAdd(type, os); var methodInfoSerializer = serializer.GetSerializerByType(typeof(MethodInfo)); var preserveObjectReferences = serializer.Options.PreserveObjectReferences; ObjectReader reader = (stream, session) => diff --git a/src/Hyperion/SerializerFactories/DictionarySerializerFactory.cs b/src/Hyperion/SerializerFactories/DictionarySerializerFactory.cs index 113317b5..4c6fdbec 100644 --- a/src/Hyperion/SerializerFactories/DictionarySerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/DictionarySerializerFactory.cs @@ -39,7 +39,13 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type { var preserveObjectReferences = serializer.Options.PreserveObjectReferences; var ser = new ObjectSerializer(type); - typeMapping.TryAdd(type, ser); + 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 dictionaryTypes = GetKeyValuePairType(type); var elementSerializer = serializer.GetSerializerByType(dictionaryTypes.KeyValuePairType); diff --git a/src/Hyperion/SerializerFactories/EnumerableSerializerFactory.cs b/src/Hyperion/SerializerFactories/EnumerableSerializerFactory.cs index ebadbf2b..854d40e2 100644 --- a/src/Hyperion/SerializerFactories/EnumerableSerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/EnumerableSerializerFactory.cs @@ -136,7 +136,13 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type ConcurrentDictionary typeMapping) { var x = new ObjectSerializer(type); - typeMapping.TryAdd(type, x); + if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index)) + { + var wrapper = new KnownTypeObjectSerializer(x, index); + typeMapping.TryAdd(type, wrapper); + } + else + typeMapping.TryAdd(type, x); var preserveObjectReferences = serializer.Options.PreserveObjectReferences; diff --git a/src/Hyperion/SerializerFactories/ExceptionSerializerFactory.cs b/src/Hyperion/SerializerFactories/ExceptionSerializerFactory.cs index f7ac7b63..aca00d58 100644 --- a/src/Hyperion/SerializerFactories/ExceptionSerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/ExceptionSerializerFactory.cs @@ -91,7 +91,13 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type StringSerializer.WriteValueImpl(stream, stackTraceString, session); stream.WriteObjectWithManifest(innerException, session); }); - typeMapping.TryAdd(type, exceptionSerializer); + if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index)) + { + var wrapper = new KnownTypeObjectSerializer(exceptionSerializer, index); + typeMapping.TryAdd(type, wrapper); + } + else + typeMapping.TryAdd(type, exceptionSerializer); return exceptionSerializer; } } diff --git a/src/Hyperion/SerializerFactories/ExpandoObjectSerializerFactory.cs b/src/Hyperion/SerializerFactories/ExpandoObjectSerializerFactory.cs index b04775a0..48ba56af 100644 --- a/src/Hyperion/SerializerFactories/ExpandoObjectSerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/ExpandoObjectSerializerFactory.cs @@ -29,7 +29,13 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type { var preserveObjectReferences = serializer.Options.PreserveObjectReferences; var ser = new ObjectSerializer(type); - typeMapping.TryAdd(type, ser); + 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)); ObjectReader reader = (stream, session) => diff --git a/src/Hyperion/SerializerFactories/FSharpListSerializerFactory.cs b/src/Hyperion/SerializerFactories/FSharpListSerializerFactory.cs index abcfd41f..4716d355 100644 --- a/src/Hyperion/SerializerFactories/FSharpListSerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/FSharpListSerializerFactory.cs @@ -48,7 +48,13 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type ConcurrentDictionary typeMapping) { var x = new ObjectSerializer(type); - typeMapping.TryAdd(type, x); + if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index)) + { + var wrapper = new KnownTypeObjectSerializer(x, index); + typeMapping.TryAdd(type, wrapper); + } + else + typeMapping.TryAdd(type, x); var elementType = GetEnumerableType(type); var arrType = elementType.MakeArrayType(); diff --git a/src/Hyperion/SerializerFactories/FSharpMapSerializerFactory.cs b/src/Hyperion/SerializerFactories/FSharpMapSerializerFactory.cs index 55db6c2d..5d2738e0 100644 --- a/src/Hyperion/SerializerFactories/FSharpMapSerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/FSharpMapSerializerFactory.cs @@ -64,7 +64,13 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type ConcurrentDictionary typeMapping) { var x = new ObjectSerializer(type); - typeMapping.TryAdd(type, x); + if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index)) + { + var wrapper = new KnownTypeObjectSerializer(x, index); + typeMapping.TryAdd(type, wrapper); + } + else + typeMapping.TryAdd(type, x); var keyType = GetKeyType(type); var valueType = GetValyeType(type); diff --git a/src/Hyperion/SerializerFactories/FieldInfoSerializerFactory.cs b/src/Hyperion/SerializerFactories/FieldInfoSerializerFactory.cs index 84685233..adf5e1d3 100644 --- a/src/Hyperion/SerializerFactories/FieldInfoSerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/FieldInfoSerializerFactory.cs @@ -31,7 +31,13 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type ConcurrentDictionary typeMapping) { var os = new ObjectSerializer(type); - typeMapping.TryAdd(type, os); + if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index)) + { + var wrapper = new KnownTypeObjectSerializer(os, index); + typeMapping.TryAdd(type, wrapper); + } + else + typeMapping.TryAdd(type, os); ObjectReader reader = (stream, session) => { var name = stream.ReadString(session); diff --git a/src/Hyperion/SerializerFactories/ImmutableCollectionsSerializerFactory.cs b/src/Hyperion/SerializerFactories/ImmutableCollectionsSerializerFactory.cs index e6eab05e..29004b9e 100644 --- a/src/Hyperion/SerializerFactories/ImmutableCollectionsSerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/ImmutableCollectionsSerializerFactory.cs @@ -49,7 +49,13 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type ConcurrentDictionary typeMapping) { var x = new ObjectSerializer(type); - typeMapping.TryAdd(type, x); + if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index)) + { + var wrapper = new KnownTypeObjectSerializer(x, index); + typeMapping.TryAdd(type, wrapper); + } + else + typeMapping.TryAdd(type, x); var preserveObjectReferences = serializer.Options.PreserveObjectReferences; var elementType = GetEnumerableType(type) ?? typeof (object); diff --git a/src/Hyperion/SerializerFactories/MethodInfoSerializerFactory.cs b/src/Hyperion/SerializerFactories/MethodInfoSerializerFactory.cs index 44b63e41..7444cf8d 100644 --- a/src/Hyperion/SerializerFactories/MethodInfoSerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/MethodInfoSerializerFactory.cs @@ -32,7 +32,13 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type ConcurrentDictionary typeMapping) { var os = new ObjectSerializer(type); - typeMapping.TryAdd(type, os); + if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index)) + { + var wrapper = new KnownTypeObjectSerializer(os, index); + typeMapping.TryAdd(type, wrapper); + } + else + typeMapping.TryAdd(type, os); ObjectReader reader = (stream, session) => { var name = stream.ReadString(session); diff --git a/src/Hyperion/SerializerFactories/MultipleDimensionalArraySerialzierFactory.cs b/src/Hyperion/SerializerFactories/MultipleDimensionalArraySerialzierFactory.cs index cdfab7a2..86d83bce 100644 --- a/src/Hyperion/SerializerFactories/MultipleDimensionalArraySerialzierFactory.cs +++ b/src/Hyperion/SerializerFactories/MultipleDimensionalArraySerialzierFactory.cs @@ -142,7 +142,13 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type WriteValues((Array)arr, stream, elementType, elementSerializer, session); }; arraySerializer.Initialize(reader, writer); - typeMapping.TryAdd(type, arraySerializer); + if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index)) + { + var wrapper = new KnownTypeObjectSerializer(arraySerializer, index); + typeMapping.TryAdd(type, wrapper); + } + else + typeMapping.TryAdd(type, arraySerializer); return arraySerializer; } diff --git a/src/Hyperion/SerializerFactories/PropertyInfoSerializerFactory.cs b/src/Hyperion/SerializerFactories/PropertyInfoSerializerFactory.cs index c9359518..45d47ad8 100644 --- a/src/Hyperion/SerializerFactories/PropertyInfoSerializerFactory.cs +++ b/src/Hyperion/SerializerFactories/PropertyInfoSerializerFactory.cs @@ -31,7 +31,13 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type ConcurrentDictionary typeMapping) { var os = new ObjectSerializer(type); - typeMapping.TryAdd(type, os); + if (serializer.Options.KnownTypesDict.TryGetValue(type, out var index)) + { + var wrapper = new KnownTypeObjectSerializer(os, index); + typeMapping.TryAdd(type, wrapper); + } + else + typeMapping.TryAdd(type, os); ObjectReader reader = (stream, session) => { var name = stream.ReadString(session);