forked from msgpack/msgpack-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArraySerializer`1.cs
More file actions
231 lines (207 loc) · 7.61 KB
/
ArraySerializer`1.cs
File metadata and controls
231 lines (207 loc) · 7.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#region -- License Terms --
//
// MessagePack for CLI
//
// Copyright (C) 2010-2016 FUJIWARA, Yusuke
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion -- License Terms --
#if UNITY_5 || UNITY_STANDALONE || UNITY_WEBPLAYER || UNITY_WII || UNITY_IPHONE || UNITY_ANDROID || UNITY_PS3 || UNITY_XBOX360 || UNITY_FLASH || UNITY_BKACKBERRY || UNITY_WINRT
#define UNITY
#endif
using System;
#if UNITY
using System.Collections;
#endif // UNITY
#if FEATURE_TAP
using System.Threading;
using System.Threading.Tasks;
#endif // FEATURE_TAP
namespace MsgPack.Serialization.DefaultSerializers
{
#if !UNITY
[Preserve( AllMembers = true )]
internal sealed class ArraySerializer<T> : MessagePackSerializer<T[]>
#else
#warning TODO: Use generic collection if possible for maintenancibility.
internal sealed class UnityArraySerializer : NonGenericMessagePackSerializer
#endif // !UNITY
{
#if !UNITY
private readonly MessagePackSerializer<T> _itemSerializer;
#else
private readonly MessagePackSerializer _itemSerializer;
private readonly Type _itemType;
#endif // !UNITY
#if !UNITY
public ArraySerializer( SerializationContext ownerContext, PolymorphismSchema itemsSchema )
: base( ownerContext, SerializerCapabilities.PackTo | SerializerCapabilities.UnpackFrom | SerializerCapabilities.UnpackTo )
{
this._itemSerializer = ownerContext.GetSerializer<T>( itemsSchema );
}
#else
public UnityArraySerializer( SerializationContext ownerContext, Type itemType, PolymorphismSchema itemsSchema )
: base( ownerContext, itemType.MakeArrayType(), SerializerCapabilities.PackTo | SerializerCapabilities.UnpackFrom | SerializerCapabilities.UnpackTo )
{
this._itemSerializer = ownerContext.GetSerializer( itemType, itemsSchema );
this._itemType = itemType;
}
#endif // !UNITY
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods", MessageId = "0", Justification = "Validated by caller in base class" )]
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods", MessageId = "1", Justification = "Validated by caller in base class" )]
#if !UNITY
protected internal override void PackToCore( Packer packer, T[] objectTree )
{
packer.PackArrayHeader( objectTree.Length );
foreach ( var item in objectTree )
{
this._itemSerializer.PackTo( packer, item );
}
}
#else
protected internal override void PackToCore( Packer packer, object objectTree )
{
var asList = objectTree as IList;
// ReSharper disable PossibleNullReferenceException
packer.PackArrayHeader( asList.Count );
foreach ( var item in asList )
{
this._itemSerializer.PackTo( packer, item );
}
// ReSharper restore PossibleNullReferenceException
}
#endif // !UNITY
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods", MessageId = "0", Justification = "Template method should not be validate parameters." )]
#if !UNITY
protected internal override T[] UnpackFromCore( Unpacker unpacker )
#else
protected internal override object UnpackFromCore( Unpacker unpacker )
#endif // !UNITY
{
if ( !unpacker.IsArrayHeader )
{
SerializationExceptions.ThrowIsNotArrayHeader( unpacker );
}
var count = UnpackHelpers.GetItemsCount( unpacker );
#if !UNITY
var result = new T[ count ];
#else
var result = Array.CreateInstance( this._itemType, count );
#endif // !UNITY
this.UnpackToCore( unpacker, result, count );
return result;
}
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods", MessageId = "0", Justification = "Template method should not be validate parameters." )]
#if !UNITY
protected internal override void UnpackToCore( Unpacker unpacker, T[] collection )
#else
protected internal override void UnpackToCore( Unpacker unpacker, object collection )
#endif // !UNITY
{
if ( !unpacker.IsArrayHeader )
{
SerializationExceptions.ThrowIsNotArrayHeader( unpacker );
}
#if !UNITY
this.UnpackToCore( unpacker, collection, UnpackHelpers.GetItemsCount( unpacker ) );
#else
this.UnpackToCore( unpacker, collection as IList, UnpackHelpers.GetItemsCount( unpacker ) );
#endif // !UNITY
}
#if !UNITY
private void UnpackToCore( Unpacker unpacker, T[] collection, int count )
#else
private void UnpackToCore( Unpacker unpacker, IList collection, int count )
#endif // !UNITY
{
for ( int i = 0; i < count; i++ )
{
if ( !unpacker.Read() )
{
SerializationExceptions.ThrowMissingItem( i, unpacker );
}
#if !UNITY
T item;
#else
object item;
#endif // !UNITY
if ( !unpacker.IsArrayHeader && !unpacker.IsMapHeader )
{
item = this._itemSerializer.UnpackFrom( unpacker );
}
else
{
using ( var subtreeUnpacker = unpacker.ReadSubtree() )
{
item = this._itemSerializer.UnpackFrom( subtreeUnpacker );
}
}
collection[ i ] = item;
}
}
#if FEATURE_TAP
protected internal override async Task PackToAsyncCore( Packer packer, T[] objectTree, CancellationToken cancellationToken )
{
await packer.PackArrayHeaderAsync( objectTree.Length, cancellationToken ).ConfigureAwait( false );
foreach ( var item in objectTree )
{
await this._itemSerializer.PackToAsync( packer, item, cancellationToken ).ConfigureAwait( false );
}
}
protected internal override async Task<T[]> UnpackFromAsyncCore( Unpacker unpacker, CancellationToken cancellationToken )
{
if ( !unpacker.IsArrayHeader )
{
SerializationExceptions.ThrowIsNotArrayHeader( unpacker );
}
var count = UnpackHelpers.GetItemsCount( unpacker );
var result = new T[ count ];
await this.UnpackToAsyncCore( unpacker, result, count, cancellationToken ).ConfigureAwait( false );
return result;
}
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods", MessageId = "0", Justification = "Template method should not be validate parameters." )]
protected internal override Task UnpackToAsyncCore( Unpacker unpacker, T[] collection, CancellationToken cancellationToken )
{
if ( !unpacker.IsArrayHeader )
{
SerializationExceptions.ThrowIsNotArrayHeader( unpacker );
}
return this.UnpackToAsyncCore( unpacker, collection, UnpackHelpers.GetItemsCount( unpacker ), cancellationToken );
}
private async Task UnpackToAsyncCore( Unpacker unpacker, T[] collection, int count, CancellationToken cancellationToken )
{
for ( int i = 0; i < count; i++ )
{
if ( !await unpacker.ReadAsync( cancellationToken ).ConfigureAwait( false ) )
{
SerializationExceptions.ThrowMissingItem( i, unpacker );
}
T item;
if ( !unpacker.IsArrayHeader && !unpacker.IsMapHeader )
{
item = await this._itemSerializer.UnpackFromAsync( unpacker, cancellationToken ).ConfigureAwait( false );
}
else
{
using ( var subtreeUnpacker = unpacker.ReadSubtree() )
{
item = await this._itemSerializer.UnpackFromAsync( subtreeUnpacker, cancellationToken ).ConfigureAwait( false );
}
}
collection[ i ] = item;
}
}
#endif // FEATURE_TAP
}
}