1+ #region -- License Terms --
2+ //
3+ // MessagePack for CLI
4+ //
5+ // Copyright (C) 2015 FUJIWARA, Yusuke
6+ //
7+ // Licensed under the Apache License, Version 2.0 (the "License");
8+ // you may not use this file except in compliance with the License.
9+ // You may obtain a copy of the License at
10+ //
11+ // http://www.apache.org/licenses/LICENSE-2.0
12+ //
13+ // Unless required by applicable law or agreed to in writing, software
14+ // distributed under the License is distributed on an "AS IS" BASIS,
15+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+ // See the License for the specific language governing permissions and
17+ // limitations under the License.
18+ //
19+ #endregion -- License Terms --
20+
21+ using System ;
22+ using System . Collections ;
23+ using System . Linq ;
24+ using System . Runtime . Serialization ;
25+
26+ namespace MsgPack . Serialization . CollectionSerializers
27+ {
28+ /// <summary>
29+ /// Provides common implementation of <see cref="NonGenericEnumerableMessagePackSerializerBase{TCollection}"/>
30+ /// for collection types which implement <see cref="IEnumerable"/>.
31+ /// </summary>
32+ /// <typeparam name="TCollection">The type of the collection.</typeparam>
33+ public abstract class NonGenericEnumerableMessagePackSerializer < TCollection > : NonGenericEnumerableMessagePackSerializerBase < TCollection >
34+ where TCollection : IEnumerable
35+ {
36+ /// <summary>
37+ /// Initializes a new instance of the <see cref="NonGenericCollectionMessagePackSerializer{TCollection}"/> class.
38+ /// </summary>
39+ /// <param name="ownerContext">A <see cref="SerializationContext"/> which owns this serializer.</param>
40+ /// <param name="schema">
41+ /// The schema for collection itself or its items for the member this instance will be used to.
42+ /// <c>null</c> will be considered as <see cref="PolymorphismSchema.Default"/>.
43+ /// </param>
44+ /// <exception cref="ArgumentNullException">
45+ /// <paramref name="ownerContext"/> is <c>null</c>.
46+ /// </exception>
47+ protected NonGenericEnumerableMessagePackSerializer ( SerializationContext ownerContext , PolymorphismSchema schema )
48+ : base ( ownerContext , schema ) { }
49+
50+ /// <summary>
51+ /// Serializes specified object with specified <see cref="Packer"/>.
52+ /// </summary>
53+ /// <param name="packer"><see cref="Packer"/> which packs values in <paramref name="objectTree"/>. This value will not be <c>null</c>.</param>
54+ /// <param name="objectTree">Object to be serialized.</param>
55+ /// <exception cref="SerializationException">
56+ /// <typeparamref name="TCollection"/> is not serializable etc.
57+ /// </exception>
58+ protected internal sealed override void PackToCore ( Packer packer , TCollection objectTree )
59+ {
60+ ICollection asICollection ;
61+ if ( ( asICollection = objectTree as ICollection ) == null )
62+ {
63+ asICollection = objectTree . Cast < object > ( ) . ToArray ( ) ;
64+ }
65+
66+ packer . PackArrayHeader ( asICollection . Count ) ;
67+ }
68+ }
69+ }
0 commit comments