Skip to content

Commit 182c66e

Browse files
committed
Implement ICollectionInstanceFactory in built-in serializers.
1 parent b55f226 commit 182c66e

9 files changed

Lines changed: 80 additions & 17 deletions

src/MsgPack/Serialization/DefaultSerializers/MsgPack_MessagePackObjectDictionaryMessagePackSerializer.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// MessagePack for CLI
44
//
5-
// Copyright (C) 2014 FUJIWARA, Yusuke
5+
// Copyright (C) 2014-2015 FUJIWARA, Yusuke
66
//
77
// Licensed under the Apache License, Version 2.0 (the "License");
88
// you may not use this file except in compliance with the License.
@@ -20,10 +20,12 @@
2020

2121
using System;
2222

23+
using MsgPack.Serialization.CollectionSerializers;
24+
2325
namespace MsgPack.Serialization.DefaultSerializers
2426
{
2527
// ReSharper disable once InconsistentNaming
26-
internal sealed class MsgPack_MessagePackObjectDictionaryMessagePackSerializer : MessagePackSerializer<MessagePackObjectDictionary>
28+
internal sealed class MsgPack_MessagePackObjectDictionaryMessagePackSerializer : MessagePackSerializer<MessagePackObjectDictionary>, ICollectionInstanceFactory
2729
{
2830
public MsgPack_MessagePackObjectDictionaryMessagePackSerializer( SerializationContext ownerContext )
2931
: base( ownerContext ) { }
@@ -91,5 +93,10 @@ private static void UnpackToCore( Unpacker unpacker, int count, MessagePackObjec
9193
}
9294
}
9395
}
96+
97+
public object CreateInstance( int initialCapacity )
98+
{
99+
return new MessagePackObjectDictionary( initialCapacity );
100+
}
94101
}
95102
}

src/MsgPack/Serialization/DefaultSerializers/System_Collections_Generic_Dictionary_2MessagePackSerializer`2.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
using System;
2222
using System.Collections.Generic;
2323

24+
using MsgPack.Serialization.CollectionSerializers;
25+
2426
namespace MsgPack.Serialization.DefaultSerializers
2527
{
2628
/// <summary>
@@ -29,7 +31,7 @@ namespace MsgPack.Serialization.DefaultSerializers
2931
/// <typeparam name="TKey">The type of keys of the <see cref="Dictionary{TKey,TValue}"/>.</typeparam>
3032
/// <typeparam name="TValue">The type of values of the <see cref="Dictionary{TKey,TValue}"/>.</typeparam>
3133
// ReSharper disable once InconsistentNaming
32-
internal class System_Collections_Generic_Dictionary_2MessagePackSerializer<TKey, TValue> : MessagePackSerializer<Dictionary<TKey, TValue>>
34+
internal class System_Collections_Generic_Dictionary_2MessagePackSerializer<TKey, TValue> : MessagePackSerializer<Dictionary<TKey, TValue>>, ICollectionInstanceFactory
3335
{
3436
private readonly MessagePackSerializer<TKey> _keySerializer;
3537
private readonly MessagePackSerializer<TValue> _valueSerializer;
@@ -111,5 +113,10 @@ private void UnpackToCore( Unpacker unpacker, Dictionary<TKey, TValue> collectio
111113
}
112114
}
113115
}
116+
117+
public object CreateInstance( int initialCapacity )
118+
{
119+
return new Dictionary<TKey, TValue>( initialCapacity );
120+
}
114121
}
115122
}

src/MsgPack/Serialization/DefaultSerializers/System_Collections_Generic_ListOfMessagePackObjectMessagePackSerializer.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// MessagePack for CLI
44
//
5-
// Copyright (C) 2010-2014 FUJIWARA, Yusuke
5+
// Copyright (C) 2010-2015 FUJIWARA, Yusuke
66
//
77
// Licensed under the Apache License, Version 2.0 (the "License");
88
// you may not use this file except in compliance with the License.
@@ -21,10 +21,12 @@
2121
using System;
2222
using System.Collections.Generic;
2323

24+
using MsgPack.Serialization.CollectionSerializers;
25+
2426
namespace MsgPack.Serialization.DefaultSerializers
2527
{
2628
// ReSharper disable once InconsistentNaming
27-
internal class System_Collections_Generic_ListOfMessagePackObjectMessagePackSerializer : MessagePackSerializer<List<MessagePackObject>>
29+
internal class System_Collections_Generic_ListOfMessagePackObjectMessagePackSerializer : MessagePackSerializer<List<MessagePackObject>>, ICollectionInstanceFactory
2830
{
2931
public System_Collections_Generic_ListOfMessagePackObjectMessagePackSerializer( SerializationContext ownerContext )
3032
: base( ownerContext ) { }
@@ -77,5 +79,10 @@ private static void UnpackToCore( Unpacker unpacker, List<MessagePackObject> col
7779
collection.Add( unpacker.LastReadData );
7880
}
7981
}
82+
83+
public object CreateInstance( int initialCapacity )
84+
{
85+
return new List<MessagePackObject>( initialCapacity );
86+
}
8087
}
8188
}

src/MsgPack/Serialization/DefaultSerializers/System_Collections_Generic_List_1MessagePackSerializer`1.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// MessagePack for CLI
44
//
5-
// Copyright (C) 2014 FUJIWARA, Yusuke
5+
// Copyright (C) 2014-2015 FUJIWARA, Yusuke
66
//
77
// Licensed under the Apache License, Version 2.0 (the "License");
88
// you may not use this file except in compliance with the License.
@@ -21,14 +21,16 @@
2121
using System;
2222
using System.Collections.Generic;
2323

24+
using MsgPack.Serialization.CollectionSerializers;
25+
2426
namespace MsgPack.Serialization.DefaultSerializers
2527
{
2628
/// <summary>
2729
/// Provides default implementation for <see cref="List{T}"/>.
2830
/// </summary>
2931
/// <typeparam name="T">The type of items of the <see cref="List{T}"/>.</typeparam>
3032
// ReSharper disable once InconsistentNaming
31-
internal class System_Collections_Generic_List_1MessagePackSerializer<T> : MessagePackSerializer<List<T>>
33+
internal class System_Collections_Generic_List_1MessagePackSerializer<T> : MessagePackSerializer<List<T>>, ICollectionInstanceFactory
3234
{
3335
private readonly MessagePackSerializer<T> _itemSerializer;
3436

@@ -89,5 +91,10 @@ private void UnpackToCore( Unpacker unpacker, List<T> collection, int count )
8991
}
9092
}
9193
}
94+
95+
public object CreateInstance( int initialCapacity )
96+
{
97+
return new List<T>( initialCapacity );
98+
}
9299
}
93100
}

src/MsgPack/Serialization/DefaultSerializers/System_Collections_Generic_Queue_1MessagePackSerializer`1.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// MessagePack for CLI
44
//
5-
// Copyright (C) 2010-2014 FUJIWARA, Yusuke
5+
// Copyright (C) 2010-2015 FUJIWARA, Yusuke
66
//
77
// Licensed under the Apache License, Version 2.0 (the "License");
88
// you may not use this file except in compliance with the License.
@@ -21,10 +21,12 @@
2121
using System;
2222
using System.Collections.Generic;
2323

24+
using MsgPack.Serialization.CollectionSerializers;
25+
2426
namespace MsgPack.Serialization.DefaultSerializers
2527
{
2628
// ReSharper disable once InconsistentNaming
27-
internal sealed class System_Collections_Generic_Queue_1MessagePackSerializer<TItem> : MessagePackSerializer<Queue<TItem>>
29+
internal sealed class System_Collections_Generic_Queue_1MessagePackSerializer<TItem> : MessagePackSerializer<Queue<TItem>>, ICollectionInstanceFactory
2830
{
2931
private readonly MessagePackSerializer<TItem> _itemSerializer;
3032

@@ -87,5 +89,10 @@ protected internal override void UnpackToCore( Unpacker unpacker, Queue<TItem> c
8789
collection.Enqueue( item );
8890
}
8991
}
92+
93+
public object CreateInstance( int initialCapacity )
94+
{
95+
return new Queue<TItem>( initialCapacity );
96+
}
9097
}
9198
}

src/MsgPack/Serialization/DefaultSerializers/System_Collections_Generic_Stack_1MessagePackSerializer`1.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// MessagePack for CLI
44
//
5-
// Copyright (C) 2010-2014 FUJIWARA, Yusuke
5+
// Copyright (C) 2010-2015 FUJIWARA, Yusuke
66
//
77
// Licensed under the Apache License, Version 2.0 (the "License");
88
// you may not use this file except in compliance with the License.
@@ -21,10 +21,12 @@
2121
using System;
2222
using System.Collections.Generic;
2323

24+
using MsgPack.Serialization.CollectionSerializers;
25+
2426
namespace MsgPack.Serialization.DefaultSerializers
2527
{
2628
// ReSharper disable once InconsistentNaming
27-
internal sealed class System_Collections_Generic_Stack_1MessagePackSerializer<TItem> : MessagePackSerializer<Stack<TItem>>
29+
internal sealed class System_Collections_Generic_Stack_1MessagePackSerializer<TItem> : MessagePackSerializer<Stack<TItem>>, ICollectionInstanceFactory
2830
{
2931
private readonly MessagePackSerializer<TItem> _itemSerializer;
3032

@@ -91,5 +93,10 @@ private IEnumerable<TItem> UnpackItemsInReverseOrder( Unpacker unpacker, int cou
9193

9294
return buffer;
9395
}
96+
97+
public object CreateInstance( int initialCapacity )
98+
{
99+
return new Stack<TItem>( initialCapacity );
100+
}
94101
}
95102
}

src/MsgPack/Serialization/DefaultSerializers/System_Collections_QueueMessagePackSerializer.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// MessagePack for CLI
44
//
5-
// Copyright (C) 2010-2014 FUJIWARA, Yusuke
5+
// Copyright (C) 2010-2015 FUJIWARA, Yusuke
66
//
77
// Licensed under the Apache License, Version 2.0 (the "License");
88
// you may not use this file except in compliance with the License.
@@ -21,10 +21,12 @@
2121
using System;
2222
using System.Collections;
2323

24+
using MsgPack.Serialization.CollectionSerializers;
25+
2426
namespace MsgPack.Serialization.DefaultSerializers
2527
{
2628
// ReSharper disable once InconsistentNaming
27-
internal sealed class System_Collections_QueueMessagePackSerializer : MessagePackSerializer<Queue>
29+
internal sealed class System_Collections_QueueMessagePackSerializer : MessagePackSerializer<Queue>, ICollectionInstanceFactory
2830
{
2931
public System_Collections_QueueMessagePackSerializer( SerializationContext ownerContext )
3032
: base( ownerContext )
@@ -71,5 +73,10 @@ protected internal override void UnpackToCore( Unpacker unpacker, Queue collecti
7173
collection.Enqueue( unpacker.LastReadData );
7274
}
7375
}
76+
77+
public object CreateInstance( int initialCapacity )
78+
{
79+
return new Queue( initialCapacity );
80+
}
7481
}
7582
}

src/MsgPack/Serialization/DefaultSerializers/System_Collections_Specialized_NameValueCollectionMessagePackSerializer.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// MessagePack for CLI
44
//
5-
// Copyright (C) 2010-2014 FUJIWARA, Yusuke
5+
// Copyright (C) 2010-2015 FUJIWARA, Yusuke
66
//
77
// Licensed under the Apache License, Version 2.0 (the "License");
88
// you may not use this file except in compliance with the License.
@@ -23,10 +23,12 @@
2323
using System.Collections.Specialized;
2424
using System.Runtime.Serialization;
2525

26+
using MsgPack.Serialization.CollectionSerializers;
27+
2628
namespace MsgPack.Serialization.DefaultSerializers
2729
{
2830
// ReSharper disable once InconsistentNaming
29-
internal sealed class System_Collections_Specialized_NameValueCollectionMessagePackSerializer : MessagePackSerializer<NameValueCollection>
31+
internal sealed class System_Collections_Specialized_NameValueCollectionMessagePackSerializer : MessagePackSerializer<NameValueCollection>, ICollectionInstanceFactory
3032
{
3133
public System_Collections_Specialized_NameValueCollectionMessagePackSerializer( SerializationContext ownerContext )
3234
: base( ownerContext ) { }
@@ -128,6 +130,11 @@ private static void UnpackToCore( Unpacker unpacker, NameValueCollection collect
128130
}
129131
}
130132
}
133+
134+
public object CreateInstance( int initialCapacity )
135+
{
136+
return new NameValueCollection( initialCapacity );
137+
}
131138
}
132139
}
133140
#endif

src/MsgPack/Serialization/DefaultSerializers/System_Collections_StackMessagePackSerializer.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// MessagePack for CLI
44
//
5-
// Copyright (C) 2010-2014 FUJIWARA, Yusuke
5+
// Copyright (C) 2010-2015 FUJIWARA, Yusuke
66
//
77
// Licensed under the Apache License, Version 2.0 (the "License");
88
// you may not use this file except in compliance with the License.
@@ -21,10 +21,12 @@
2121
using System;
2222
using System.Collections;
2323

24+
using MsgPack.Serialization.CollectionSerializers;
25+
2426
namespace MsgPack.Serialization.DefaultSerializers
2527
{
2628
// ReSharper disable once InconsistentNaming
27-
internal sealed class System_Collections_StackMessagePackSerializer : MessagePackSerializer<Stack>
29+
internal sealed class System_Collections_StackMessagePackSerializer : MessagePackSerializer<Stack>, ICollectionInstanceFactory
2830
{
2931
public System_Collections_StackMessagePackSerializer( SerializationContext ownerContext )
3032
: base( ownerContext ) { }
@@ -86,5 +88,10 @@ private static ICollection UnpackItemsInReverseOrder( Unpacker unpacker, int cou
8688

8789
return buffer;
8890
}
91+
92+
public object CreateInstance( int initialCapacity )
93+
{
94+
return new Stack( initialCapacity );
95+
}
8996
}
9097
}

0 commit comments

Comments
 (0)