Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/Hyperion.Tests/ImmutableCollectionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

using System.Collections.Generic;
using System.Collections.Immutable;
using System.Collections.ObjectModel;
using System.Linq;
using Xunit;

Expand Down Expand Up @@ -150,6 +151,62 @@ public void CanSerializeImmutableDictionary()
Assert.Equal(expected.ToList(), actual.ToList());
}

[Fact]
public void CanSerializeIReadOnlyDictionary()
{
var dict = ImmutableDictionary.CreateRange(new Dictionary<string, Something>
{
["a1"] = new Something
{
BoolProp = true,
Else = new Else
{
Name = "Yoho"
},
Int32Prop = 999,
StringProp = "Yesbox!"
},
["a2"] = new Something(),
["a3"] = new Something(),
["a4"] = null
});

var expected = (IReadOnlyDictionary<string, Something>)dict;

Serialize(expected);
Reset();
var actual = Deserialize<IReadOnlyDictionary<string, Something>>();
Assert.Equal(expected.ToList(), actual.ToList());
}

[Fact]
public void CanSerializeReadOnlyDictionary()
{
var dict = new Dictionary<string, Something>
{
["a1"] = new Something
{
BoolProp = true,
Else = new Else
{
Name = "Yoho"
},
Int32Prop = 999,
StringProp = "Yesbox!"
},
["a2"] = new Something(),
["a3"] = new Something(),
["a4"] = null
};

var expected = new ReadOnlyDictionary<string, Something>(dict);

Serialize(expected);
Reset();
var actual = Deserialize<ReadOnlyDictionary<string, Something>>();
Assert.Equal(expected.ToList(), actual.ToList());
}

[Fact]
public void CanSerializeImmutableQueue()
{
Expand Down
54 changes: 40 additions & 14 deletions src/Hyperion/SerializerFactories/DictionarySerializerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using Hyperion.Extensions;
Expand Down Expand Up @@ -45,6 +46,44 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type
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>
Expand All @@ -56,20 +95,7 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type
{
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 });
}
ReadDictionaryKeyValuePairs(instance);

return instance;
};
Expand Down