2121using System ;
2222using System . Collections . ObjectModel ;
2323
24+ using MsgPack . Serialization . CollectionSerializers ;
25+
2426namespace MsgPack . Serialization
2527{
2628 /// <summary>
@@ -29,66 +31,14 @@ namespace MsgPack.Serialization
2931 /// <typeparam name="TKey">The type of keys of the <see cref="EchoKeyedCollection{TKey, TValue}"/>.</typeparam>
3032 /// <typeparam name="T">The type of items of the <see cref="EchoKeyedCollection{TKey, TValue}"/>.</typeparam>
3133 // ReSharper disable once InconsistentNaming
32- internal class EchoKeyedCollection_2MessagePackSerializer < TKey , T > : MessagePackSerializer < EchoKeyedCollection < TKey , T > >
34+ internal class EchoKeyedCollection_2MessagePackSerializer < TKey , T > : CollectionMessagePackSerializer < EchoKeyedCollection < TKey , T > , T >
3335 {
34- private readonly MessagePackSerializer < T > _itemSerializer ;
35-
3636 public EchoKeyedCollection_2MessagePackSerializer ( SerializationContext ownerContext , PolymorphismSchema itemsSchema )
37- : base ( ownerContext )
38- {
39- this . _itemSerializer = ownerContext . GetSerializer < T > ( itemsSchema ) ;
40- }
41-
42- protected internal override void PackToCore ( Packer packer , EchoKeyedCollection < TKey , T > objectTree )
43- {
44- PackerUnpackerExtensions . PackCollectionCore ( packer , objectTree , this . _itemSerializer ) ;
45- }
46-
47- [ System . Diagnostics . CodeAnalysis . SuppressMessage ( "Microsoft.Design" , "CA1062:ValidateArgumentsOfPublicMethods" , MessageId = "0" , Justification = "Asserted internally" ) ]
48- protected internal override EchoKeyedCollection < TKey , T > UnpackFromCore ( Unpacker unpacker )
49- {
50- if ( ! unpacker . IsArrayHeader )
51- {
52- throw SerializationExceptions . NewIsNotArrayHeader ( ) ;
53- }
54-
55- var count = UnpackHelpers . GetItemsCount ( unpacker ) ;
56- var collection = new EchoKeyedCollection < TKey , T > ( ) ;
57- this . UnpackToCore ( unpacker , collection , count ) ;
58- return collection ;
59- }
60-
61- [ System . Diagnostics . CodeAnalysis . SuppressMessage ( "Microsoft.Design" , "CA1062:ValidateArgumentsOfPublicMethods" , MessageId = "0" , Justification = "Asserted internally" ) ]
62- protected internal override void UnpackToCore ( Unpacker unpacker , EchoKeyedCollection < TKey , T > collection )
63- {
64- if ( ! unpacker . IsArrayHeader )
65- {
66- throw SerializationExceptions . NewIsNotArrayHeader ( ) ;
67- }
68-
69- this . UnpackToCore ( unpacker , collection , UnpackHelpers . GetItemsCount ( unpacker ) ) ;
70- }
37+ : base ( ownerContext , itemsSchema ) { }
7138
72- private void UnpackToCore ( Unpacker unpacker , KeyedCollection < TKey , T > collection , int count )
39+ protected override EchoKeyedCollection < TKey , T > CreateInstance ( int initialCapacity )
7340 {
74- for ( int i = 0 ; i < count ; i ++ )
75- {
76- if ( ! unpacker . Read ( ) )
77- {
78- throw SerializationExceptions . NewUnexpectedEndOfStream ( ) ;
79- }
80- if ( unpacker . IsCollectionHeader )
81- {
82- using ( var subTreeUnpacker = unpacker . ReadSubtree ( ) )
83- {
84- collection . Add ( this . _itemSerializer . UnpackFromCore ( subTreeUnpacker ) ) ;
85- }
86- }
87- else
88- {
89- collection . Add ( this . _itemSerializer . UnpackFromCore ( unpacker ) ) ;
90- }
91- }
41+ return new EchoKeyedCollection < TKey , T > ( ) ;
9242 }
9343 }
9444}
0 commit comments