Permalink
Browse files

linked list support

  • Loading branch information...
1 parent 34b08cf commit a69c6e0821b47ad351b569784b424ab24297b31a @rogeralsing committed Dec 15, 2016
@@ -11,6 +11,22 @@ namespace Wire.Tests
public class CollectionTests : TestBase
{
[Fact]
+ public void CanSerializeLinkedList()
+ {
+ var expected = new LinkedList<string>();
+ expected.AddLast("a");
+ expected.AddLast("b");
+ expected.AddLast("c");
+ expected.AddLast("d");
+
+ Serialize(expected);
+ Reset();
+ var actual = Deserialize<LinkedList<string>>();
+
+ Assert.True(actual.SequenceEqual(expected));
+ }
+
+ [Fact]
public void CanSerializeArrayOfTuples()
{
var expected = new[]
@@ -15,14 +15,14 @@ namespace Wire.SerializerFactories
{
public class DefaultDictionarySerializerFactory : ValueSerializerFactory
{
- public override bool CanSerialize(Serializer serializer, Type type) => IsInterface(type);
+ public override bool CanSerialize(Serializer serializer, Type type) => IsDictionary(type);
- private static bool IsInterface(Type type)
+ private static bool IsDictionary(Type type)
{
return type.IsConstructedGenericType && type.GetGenericTypeDefinition() == typeof(Dictionary<,>);
}
- public override bool CanDeserialize(Serializer serializer, Type type) => IsInterface(type);
+ public override bool CanDeserialize(Serializer serializer, Type type) => IsDictionary(type);
public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
ConcurrentDictionary<Type, ValueSerializer> typeMapping)
@@ -0,0 +1,90 @@
+// //-----------------------------------------------------------------------
+// // <copyright file="LinkedListSerializerFactory.cs" company="Asynkron HB">
+// // Copyright (C) 2015-2016 Asynkron HB All rights reserved
+// // </copyright>
+// //-----------------------------------------------------------------------
+
+using System;
+using System.Collections;
+using System.Collections.Concurrent;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Reflection;
+using Wire.Extensions;
+using Wire.ValueSerializers;
+
+namespace Wire.SerializerFactories
+{
+ public class LinkedListSerializerFactory : ValueSerializerFactory
+ {
+ public override bool CanSerialize(Serializer serializer, Type type)
+ {
+ return type.IsConstructedGenericType && type.GetGenericTypeDefinition() == typeof(LinkedList<>);
+ }
+
+ public override bool CanDeserialize(Serializer serializer, Type type)
+ {
+ return CanSerialize(serializer, type);
+ }
+
+ 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);
+ }
+ }
+
+ private static object ReadValues<T>(Stream stream, DeserializerSession session, bool preserveObjectReferences)
+ {
+ var length = stream.ReadInt32(session);
+ var llist = new LinkedList<T>();
+ if (preserveObjectReferences)
+ {
+ session.TrackDeserializedObject(llist);
+ }
+ for (var i = 0; i < length; i++)
+ {
+ var value = (T)stream.ReadObject(session);
+ llist.AddLast(value);
+ }
+ return llist;
+ }
+
+ public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
+ ConcurrentDictionary<Type, ValueSerializer> typeMapping)
+ {
+ var arraySerializer = new ObjectSerializer(type);
+
+ var elementType = type.GetGenericArguments()[0];
+ var elementSerializer = serializer.GetSerializerByType(elementType);
+ var preserveObjectReferences = serializer.Options.PreserveObjectReferences;
+
+ var readGeneric = GetType().GetTypeInfo().GetMethod(nameof(ReadValues), BindingFlags.NonPublic | BindingFlags.Static).MakeGenericMethod(elementType);
+ var writeGeneric = GetType().GetTypeInfo().GetMethod(nameof(WriteValues), BindingFlags.NonPublic | BindingFlags.Static).MakeGenericMethod(elementType);
+
+ ObjectReader reader = (stream, session) =>
+ {
+ //Stream stream, DeserializerSession session, bool preserveObjectReferences
+ var res = readGeneric.Invoke(null, new object[] { stream, session, preserveObjectReferences });
+ return res;
+ };
+ ObjectWriter writer = (stream, arr, session) =>
+ {
+ //T[] array, Stream stream, Type elementType, ValueSerializer elementSerializer, SerializerSession session, bool preserveObjectReferences
+ writeGeneric.Invoke(null, new[] { arr, stream, elementType, elementSerializer, session, preserveObjectReferences });
+ };
+ arraySerializer.Initialize(reader, writer);
+ typeMapping.TryAdd(type, arraySerializer);
+ return arraySerializer;
+ }
+ }
+}
@@ -34,6 +34,7 @@ public class SerializerOptions
new ImmutableCollectionsSerializerFactory(),
new ExpandoObjectSerializerFactory(),
new DefaultDictionarySerializerFactory(),
+ new LinkedListSerializerFactory(),
new DictionarySerializerFactory(),
new HashSetSerializerFactory(),
new ArraySerializerFactory(),
View
@@ -69,6 +69,7 @@
<Compile Include="SerializerFactories\HashSetSerializerFactory.cs" />
<Compile Include="SerializerFactories\ImmutableCollectionsSerializerFactory.cs" />
<Compile Include="SerializerFactories\ISerializableSerializerFactory.cs" />
+ <Compile Include="SerializerFactories\LinkedListSerializerFactory.cs" />
<Compile Include="SerializerFactories\MethodInfoSerializerFactory.cs" />
<Compile Include="SerializerFactories\ToSurrogateSerializerFactory.cs" />
<Compile Include="SerializerFactories\ValueSerializerFactory.cs" />

0 comments on commit a69c6e0

Please sign in to comment.