forked from msgpack/msgpack-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerializationExceptions.cs
More file actions
968 lines (884 loc) · 37 KB
/
SerializationExceptions.cs
File metadata and controls
968 lines (884 loc) · 37 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
#region -- License Terms --
//
// MessagePack for CLI
//
// Copyright (C) 2010-2018 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
#define AOT
#endif
using System;
#if !UNITY || MSGPACK_UNITY_FULL
using System.ComponentModel;
#endif // !UNITY || MSGPACK_UNITY_FULL
#if FEATURE_MPCONTRACT
using Contract = MsgPack.MPContract;
#else
using System.Diagnostics.Contracts;
#endif // FEATURE_MPCONTRACT
using System.Globalization;
#if !UNITY
using System.Reflection;
#endif // !UNITY
using System.Runtime.Serialization;
using MsgPack.Serialization.CollectionSerializers;
using MsgPack.Serialization.Reflection;
namespace MsgPack.Serialization
{
/// <summary>
/// <strong>This is intended to MsgPack for CLI internal use. Do not use this type from application directly.</strong>
/// Defines common exception factory methods.
/// </summary>
#if !UNITY || MSGPACK_UNITY_FULL
[EditorBrowsable( EditorBrowsableState.Never )]
#endif // !UNITY || MSGPACK_UNITY_FULL
public static class SerializationExceptions
{
#if !AOT
internal static readonly MethodInfo ThrowValueTypeCannotBeNull3Method = typeof( SerializationExceptions ).GetMethod( nameof( ThrowValueTypeCannotBeNull ), new[] { typeof( string ), typeof( Type ), typeof( Type ) } );
#endif // !AOT
/// <summary>
/// <strong>This is intended to MsgPack for CLI internal use. Do not use this type from application directly.</strong>
/// Returns new exception to notify that value type cannot be <c>null</c> on deserialization.
/// </summary>
/// <param name="name">The name of the member.</param>
/// <param name="memberType">The type of the member.</param>
/// <param name="declaringType">The type that declares the member.</param>
/// <returns><see cref="Exception"/> instance. It will not be <c>null</c>.</returns>
public static Exception NewValueTypeCannotBeNull( string name, Type memberType, Type declaringType )
{
#if DEBUG
Contract.Requires( !String.IsNullOrEmpty( name ) );
Contract.Requires( memberType != null );
Contract.Requires( declaringType != null );
Contract.Ensures( Contract.Result<Exception>() != null );
#endif // DEBUG
return new SerializationException( String.Format( CultureInfo.CurrentCulture, "Member '{0}' of type '{1}' cannot be null because it is value type('{2}').", name, declaringType, memberType ) );
}
/// <summary>
/// <strong>This is intended to MsgPack for CLI internal use. Do not use this type from application directly.</strong>
/// Throws an exception to notify that value type cannot be <c>null</c> on deserialization.
/// </summary>
/// <param name="name">The name of the member.</param>
/// <param name="memberType">The type of the member.</param>
/// <param name="declaringType">The type that declares the member.</param>
/// <exception cref="Exception">Always thrown.</exception>
/// <returns><see cref="Exception"/> instance. It will not be <c>null</c>.</returns>
public static void ThrowValueTypeCannotBeNull( string name, Type memberType, Type declaringType )
{
throw NewValueTypeCannotBeNull( name, memberType, declaringType );
}
/// <summary>
/// <strong>This is intended to MsgPack for CLI internal use. Do not use this type from application directly.</strong>
/// Returns new exception to notify that value type cannot be <c>null</c> on deserialization.
/// </summary>
/// <param name="type">The target type.</param>
/// <returns><see cref="Exception"/> instance. It will not be <c>null</c>.</returns>
public static Exception NewValueTypeCannotBeNull( Type type )
{
#if DEBUG
Contract.Requires( type != null );
Contract.Ensures( Contract.Result<Exception>() != null );
#endif // DEBUG
return new SerializationException( String.Format( CultureInfo.CurrentCulture, "Cannot be null '{0}' type value.", type ) );
}
/// <summary>
/// <strong>This is intended to MsgPack for CLI internal use. Do not use this type from application directly.</strong>
/// Returns new exception to notify that value type cannot serialize.
/// </summary>
/// <param name="type">The target type.</param>
/// <returns><see cref="Exception"/> instance. It will not be <c>null</c>.</returns>
public static Exception NewTypeCannotSerialize( Type type )
{
#if DEBUG
Contract.Requires( type != null );
Contract.Ensures( Contract.Result<Exception>() != null );
#endif // DEBUG
return new SerializationException( String.Format( CultureInfo.CurrentCulture, "Cannot serialize '{0}' type.", type ) );
}
/// <summary>
/// <strong>This is intended to MsgPack for CLI internal use. Do not use this type from application directly.</strong>
/// Returns new exception to notify that value type cannot deserialize.
/// </summary>
/// <param name="type">The target type.</param>
/// <returns><see cref="Exception"/> instance. It will not be <c>null</c>.</returns>
public static Exception NewTypeCannotDeserialize( Type type )
{
#if DEBUG
Contract.Requires( type != null );
Contract.Ensures( Contract.Result<Exception>() != null );
#endif // DEBUG
return new SerializationException( String.Format( CultureInfo.CurrentCulture, "Cannot deserialize '{0}' type.", type ) );
}
/// <summary>
/// <strong>This is intended to MsgPack for CLI internal use. Do not use this type from application directly.</strong>
/// Returns new exception to notify that value type cannot deserialize.
/// </summary>
/// <param name="type">The target type.</param>
/// <param name="memberName">The name of deserializing member.</param>
/// <param name="inner">The inner exception.</param>
/// <returns><see cref="Exception"/> instance. It will not be <c>null</c>.</returns>
public static Exception NewTypeCannotDeserialize( Type type, string memberName, Exception inner )
{
#if DEBUG
Contract.Requires( type != null );
Contract.Requires( !String.IsNullOrEmpty( memberName ) );
Contract.Requires( inner != null );
Contract.Ensures( Contract.Result<Exception>() != null );
#endif // DEBUG
return new SerializationException( String.Format( CultureInfo.CurrentCulture, "Cannot deserialize member '{1}' of type '{0}'.", type, memberName ), inner );
}
/// <summary>
/// <strong>This is intended to MsgPack for CLI internal use. Do not use this type from application directly.</strong>
/// Returns new exception to notify that item is not found on the unpacking stream.
/// </summary>
/// <param name="index">The index to be unpacking.</param>
/// <returns><see cref="Exception"/> instance. It will not be <c>null</c>.</returns>
#if DEBUG
[Obsolete( "Use ThrowMissingItem(int, Unpacker) instead." )]
#endif
public static Exception NewMissingItem( int index ) // For compatibility only.
{
#if DEBUG
Contract.Requires( index >= 0 );
Contract.Ensures( Contract.Result<Exception>() != null );
#endif // DEBUG
return new InvalidMessagePackStreamException( String.Format( CultureInfo.CurrentCulture, "Items at index '{0}' is missing.", index ) );
}
/// <summary>
/// <strong>This is intended to MsgPack for CLI internal use. Do not use this type from application directly.</strong>
/// Throws a exception to notify that item is not found on the unpacking stream.
/// </summary>
/// <param name="index">The index to be unpacking.</param>
/// <param name="unpacker">The unpacker for pretty message.</param>
/// <exception cref="Exception">Always thrown.</exception>
public static void ThrowMissingItem( int index, Unpacker unpacker )
{
ThrowMissingItem( index, null, unpacker );
}
/// <summary>
/// <strong>This is intended to MsgPack for CLI internal use. Do not use this type from application directly.</strong>
/// Throws a exception to notify that item is not found on the unpacking stream.
/// </summary>
/// <param name="index">The index to be unpacking.</param>
/// <param name="name">The name of the item to be unpacking.</param>
/// <param name="unpacker">The unpacker for pretty message.</param>
/// <exception cref="Exception">Always thrown.</exception>
public static void ThrowMissingItem( int index, string name, Unpacker unpacker )
{
long offsetOrPosition = -1;
bool isRealPosition = false;
if ( unpacker != null )
{
isRealPosition = unpacker.GetPreviousPosition( out offsetOrPosition );
}
if ( String.IsNullOrEmpty( name ) )
{
if ( offsetOrPosition >= 0L )
{
if ( isRealPosition )
{
throw new InvalidMessagePackStreamException(
String.Format(
CultureInfo.CurrentCulture,
"Value for '{0}' at index {1} is missing, at position {2}",
name,
index,
offsetOrPosition
)
);
}
else
{
throw new InvalidMessagePackStreamException(
String.Format(
CultureInfo.CurrentCulture,
"Value for '{0}' at index {1} is missing, at offset {2}",
name,
index,
offsetOrPosition
)
);
}
}
else
{
throw new InvalidMessagePackStreamException(
String.Format( CultureInfo.CurrentCulture, "Value for '{0}' at index {1} is missing.", name, index )
);
}
}
else
{
if ( offsetOrPosition >= 0L )
{
if ( isRealPosition )
{
throw new InvalidMessagePackStreamException(
String.Format(
CultureInfo.CurrentCulture,
"Item at index {0} is missing, at position {1}",
index,
offsetOrPosition
)
);
}
else
{
throw new InvalidMessagePackStreamException(
String.Format(
CultureInfo.CurrentCulture,
"Item at index {0} is missing, at offset {1}",
index,
offsetOrPosition
)
);
}
}
else
{
throw new InvalidMessagePackStreamException(
String.Format( CultureInfo.CurrentCulture, "Item at index '{0}' is missing.", index )
);
}
}
}
internal static void ThrowMissingKey( int index, Unpacker unpacker )
{
long offsetOrPosition;
var isRealPosition = unpacker.GetPreviousPosition( out offsetOrPosition );
if ( offsetOrPosition >= 0L )
{
if ( isRealPosition )
{
throw new InvalidMessagePackStreamException(
String.Format(
CultureInfo.CurrentCulture,
"Key of map entry at index {0} is missing, at position {1}",
index,
offsetOrPosition
)
);
}
else
{
throw new InvalidMessagePackStreamException(
String.Format(
CultureInfo.CurrentCulture,
"Key of map entry at index {0} is missing, at offset {1}",
index,
offsetOrPosition
)
);
}
}
else
{
throw new InvalidMessagePackStreamException(
String.Format(
CultureInfo.CurrentCulture,
"Key of map entry at index {0} is missing.",
index
)
);
}
}
/// <summary>
/// <strong>This is intended to MsgPack for CLI internal use. Do not use this type from application directly.</strong>
/// Returns new exception to notify that target type is not serializable because it does not have public default constructor.
/// </summary>
/// <param name="type">The target type.</param>
/// <returns><see cref="Exception"/> instance. It will not be <c>null</c>.</returns>
internal static Exception NewTargetDoesNotHavePublicDefaultConstructor( Type type )
{
#if DEBUG
Contract.Requires( type != null );
Contract.Ensures( Contract.Result<Exception>() != null );
#endif // DEBUG
return new SerializationException( String.Format( CultureInfo.CurrentCulture, "Type '{0}' does not have default (parameterless) public constructor.", type ) );
}
/// <summary>
/// <strong>This is intended to MsgPack for CLI internal use. Do not use this type from application directly.</strong>
/// Returns new exception to notify that target type is not serializable because it does not have both of public default constructor and public constructor with an Int32 parameter.
/// </summary>
/// <param name="type">The target type.</param>
/// <returns><see cref="Exception"/> instance. It will not be <c>null</c>.</returns>
internal static Exception NewTargetDoesNotHavePublicDefaultConstructorNorInitialCapacity( Type type )
{
#if DEBUG
Contract.Requires( type != null );
Contract.Ensures( Contract.Result<Exception>() != null );
#endif // DEBUG
return new SerializationException( String.Format( CultureInfo.CurrentCulture, "Type '{0}' does not have both of default (parameterless) public constructor and public constructor with an Int32 parameter.", type ) );
}
internal static void ThrowTargetDoesNotHavePublicDefaultConstructorNorInitialCapacity( Type type )
{
throw NewTargetDoesNotHavePublicDefaultConstructorNorInitialCapacity( type );
}
/// <summary>
/// <strong>This is intended to MsgPack for CLI internal use. Do not use this type from application directly.</strong>
/// Returns new exception to notify that required field is not found on the unpacking stream.
/// </summary>
/// <param name="name">The name of the property.</param>
/// <returns><see cref="Exception"/> instance. It will not be <c>null</c>.</returns>
public static Exception NewMissingProperty( string name )
{
#if DEBUG
Contract.Requires( !String.IsNullOrEmpty( name ) );
Contract.Ensures( Contract.Result<Exception>() != null );
#endif // DEBUG
return new SerializationException( String.Format( CultureInfo.CurrentCulture, "Property '{0}' is missing.", name ) );
}
internal static void ThrowMissingProperty( string name )
{
#pragma warning disable 612
throw NewMissingProperty( name );
#pragma warning restore 612
}
/// <summary>
/// <strong>This is intended to MsgPack for CLI internal use. Do not use this type from application directly.</strong>
/// Returns new exception to notify that unpacking stream ends on unexpectedly position.
/// </summary>
/// <returns><see cref="Exception"/> instance. It will not be <c>null</c>.</returns>
[Obsolete( "This method is no longer used internally. So this internal API will be removed in future." )]
public static Exception NewUnexpectedEndOfStream()
{
#if DEBUG
Contract.Ensures( Contract.Result<Exception>() != null );
#endif // DEBUG
return new SerializationException( "Stream unexpectedly ends." );
}
internal static void ThrowUnexpectedEndOfStream( Unpacker unpacker )
{
long offsetOrPosition;
var isRealPosition = unpacker.GetPreviousPosition( out offsetOrPosition );
if ( offsetOrPosition >= 0L )
{
if ( isRealPosition )
{
throw new InvalidMessagePackStreamException(
String.Format(
CultureInfo.CurrentCulture,
"Stream unexpectedly ends at position {0}",
offsetOrPosition
)
);
}
else
{
throw new InvalidMessagePackStreamException(
String.Format(
CultureInfo.CurrentCulture,
"Stream unexpectedly ends at offset {0}",
offsetOrPosition
)
);
}
}
else
{
throw new InvalidMessagePackStreamException( "Stream unexpectedly ends." );
}
}
/// <summary>
/// <strong>This is intended to MsgPack for CLI internal use. Do not use this type from application directly.</strong>
/// Returns new exception to notify that target collection type does not declare appropriate Add(T) method.
/// </summary>
/// <param name="type">The target type.</param>
/// <returns><see cref="Exception"/> instance. It will not be <c>null</c>.</returns>
public static Exception NewMissingAddMethod( Type type )
{
#if DEBUG
Contract.Requires( type != null );
Contract.Ensures( Contract.Result<Exception>() != null );
#endif // DEBUG
return new SerializationException( String.Format( CultureInfo.CurrentCulture, "Type '{0}' does not have appropriate Add method.", type ) );
}
#if !AOT
internal static readonly MethodInfo ThrowIsNotArrayHeaderMethod =
typeof( SerializationExceptions ).GetMethod( nameof( ThrowIsNotArrayHeader ), new[] { typeof( Unpacker ) } );
#endif // !AOT
/// <summary>
/// <strong>This is intended to MsgPack for CLI internal use. Do not use this type from application directly.</strong>
/// Returns new exception to notify that unpacker is not in the array header, that is the state is invalid.
/// </summary>
/// <returns><see cref="Exception"/> instance. It will not be <c>null</c>.</returns>
[Obsolete( "This method is no longer used internally. So this internal API will be removed in future." )]
public static Exception NewIsNotArrayHeader()
{
return new SerializationException( "Unpacker is not in the array header. The stream may not be array." );
}
/// <summary>
/// <strong>This is intended to MsgPack for CLI internal use. Do not use this type from application directly.</strong>
/// Throws an exception to notify that unpacker is not in the array header, that is the state is invalid.
/// </summary>
/// <param name="unpacker">The unpacker for pretty message.</param>
/// <exception cref="Exception">Always thrown.</exception>
public static void ThrowIsNotArrayHeader( Unpacker unpacker )
{
long offsetOrPosition;
if ( unpacker != null )
{
if ( unpacker.GetPreviousPosition( out offsetOrPosition ) )
{
throw new SerializationException(
String.Format(
CultureInfo.CurrentCulture,
"Unpacker is not in the array header at position {0}. The stream may not be array.",
offsetOrPosition
)
);
}
else
{
throw new SerializationException(
String.Format(
CultureInfo.CurrentCulture,
"Unpacker is not in the array header at offset {0}. The stream may not be array.",
offsetOrPosition
)
);
}
}
else
{
throw new SerializationException(
"Unpacker is not in the array header. The stream may not be array."
);
}
}
#if !AOT
internal static readonly MethodInfo ThrowIsNotMapHeaderMethod =
typeof( SerializationExceptions ).GetMethod( nameof( ThrowIsNotMapHeader ), new[] { typeof( Unpacker ) } );
#endif // !AOT
/// <summary>
/// <strong>This is intended to MsgPack for CLI internal use. Do not use this type from application directly.</strong>
/// Returns new exception to notify that unpacker is not in the array header, that is the state is invalid.
/// </summary>
/// <returns><see cref="Exception"/> instance. It will not be <c>null</c>.</returns>
#if DEBUG
[Obsolete]
#endif // DEBUG
public static Exception NewIsNotMapHeader()
{
#if DEBUG
Contract.Ensures( Contract.Result<Exception>() != null );
#endif // DEBUG
return new SerializationException( "Unpacker is not in the map header. The stream may not be map." );
}
/// <summary>
/// <strong>This is intended to MsgPack for CLI internal use. Do not use this type from application directly.</strong>
/// Throws an exception to notify that unpacker is not in the map header, that is the state is invalid.
/// </summary>
/// <param name="unpacker">The unpacker for pretty message.</param>
/// <exception cref="Exception">Always thrown.</exception>
public static void ThrowIsNotMapHeader( Unpacker unpacker )
{
long offsetOrPosition;
if ( unpacker != null )
{
if ( unpacker.GetPreviousPosition( out offsetOrPosition ) )
{
throw new SerializationException(
String.Format(
CultureInfo.CurrentCulture,
"Unpacker is not in the map header at position {0}. The stream may not be map.",
offsetOrPosition
)
);
}
else
{
throw new SerializationException(
String.Format(
CultureInfo.CurrentCulture,
"Unpacker is not in the map header at offset {0}. The stream may not be map.",
offsetOrPosition
)
);
}
}
else
{
throw new SerializationException(
"Unpacker is not in the map header. The stream may not be map."
);
}
}
/// <summary>
/// <strong>This is intended to MsgPack for CLI internal use. Do not use this type from application directly.</strong>
/// Returns new exception to notify that operation is not supported because <paramref name="type"/> cannot be instanciated.
/// </summary>
/// <param name="type">Type.</param>
/// <returns><see cref="Exception"/> instance. It will not be <c>null</c>.</returns>
public static Exception NewNotSupportedBecauseCannotInstanciateAbstractType( Type type )
{
#if DEBUG
Contract.Requires( type != null );
Contract.Ensures( Contract.Result<Exception>() != null );
#endif // DEBUG
return new NotSupportedException( String.Format( CultureInfo.CurrentCulture, "This operation is not supported because '{0}' cannot be instanciated.", type ) );
}
#if !AOT
/// <summary>
/// <see cref="ThrowTupleCardinarityIsNotMatch(int,long,Unpacker)"/>
/// </summary>
internal static readonly MethodInfo ThrowTupleCardinarityIsNotMatchMethod =
typeof( SerializationExceptions ).GetMethod( nameof( ThrowTupleCardinarityIsNotMatch ), new[] { typeof( int ), typeof( long ), typeof( Unpacker ) } );
#endif // !AOT
/// <summary>
/// <strong>This is intended to MsgPack for CLI internal use. Do not use this type from application directly.</strong>
/// Returns new exception to notify that the array length does not match to expected tuple cardinality.
/// </summary>
/// <param name="expectedTupleCardinality">The expected cardinality of the tuple.</param>
/// <param name="actualArrayLength">The actual serialized array length.</param>
/// <returns><see cref="Exception"/> instance. It will not be <c>null</c>.</returns>
#if DEBUG
[Obsolete]
#endif // DEBUG
public static Exception NewTupleCardinarityIsNotMatch( int expectedTupleCardinality, int actualArrayLength )
{
#if DEBUG
Contract.Requires( expectedTupleCardinality > 0 );
Contract.Ensures( Contract.Result<Exception>() != null );
#endif // DEBUG
return new SerializationException( String.Format( CultureInfo.CurrentCulture, "The length of array ({0}) does not match to tuple cardinality ({1}).", actualArrayLength, expectedTupleCardinality ) );
}
/// <summary>
/// <strong>This is intended to MsgPack for CLI internal use. Do not use this type from application directly.</strong>
/// Throws an exception to notify that the array length does not match to expected tuple cardinality.
/// </summary>
/// <param name="expectedTupleCardinality">The expected cardinality of the tuple.</param>
/// <param name="actualArrayLength">The actual serialized array length.</param>
/// <param name="unpacker">The unpacker for pretty message.</param>
/// <exception cref="Exception">Always thrown.</exception>
/// <returns><see cref="Exception"/> instance. It will not be <c>null</c>.</returns>
public static void ThrowTupleCardinarityIsNotMatch(
int expectedTupleCardinality,
long actualArrayLength,
Unpacker unpacker
)
{
#if DEBUG
Contract.Requires( expectedTupleCardinality > 0 );
#endif // DEBUG
long offsetOrPosition = -1;
bool isRealPosition = false;
if ( unpacker != null )
{
isRealPosition = unpacker.GetPreviousPosition( out offsetOrPosition );
}
if ( offsetOrPosition >= 0L )
{
if ( isRealPosition )
{
throw new InvalidMessagePackStreamException(
String.Format(
CultureInfo.CurrentCulture,
"The length of array ({0}) does not match to tuple cardinality ({1}), at position {2}",
actualArrayLength,
expectedTupleCardinality,
offsetOrPosition
)
);
}
else
{
throw new InvalidMessagePackStreamException(
String.Format(
CultureInfo.CurrentCulture,
"The length of array ({0}) does not match to tuple cardinality ({1}), at offset {2}",
actualArrayLength,
expectedTupleCardinality,
offsetOrPosition
)
);
}
}
else
{
throw new InvalidMessagePackStreamException(
String.Format(
CultureInfo.CurrentCulture,
"The length of array ({0}) does not match to tuple cardinality ({1}).",
actualArrayLength,
expectedTupleCardinality
)
);
}
}
/// <summary>
/// <strong>This is intended to MsgPack for CLI internal use. Do not use this type from application directly.</strong>
/// Returns new exception to notify that the underlying stream is not correct semantically because failed to unpack items count of array/map.
/// </summary>
/// <param name="innerException">The inner exception for the debug. The value is implementation specific.</param>
/// <returns><see cref="Exception"/> instance. It will not be <c>null</c>.</returns>
public static Exception NewIsIncorrectStream( Exception innerException )
{
#if DEBUG
Contract.Ensures( Contract.Result<Exception>() != null );
#endif // DEBUG
return new SerializationException( "Failed to unpack items count of the collection.", innerException );
}
internal static void ThrowIsIncorrectStream( Exception innerException )
{
throw NewIsIncorrectStream( innerException );
}
/// <summary>
/// <strong>This is intended to MsgPack for CLI internal use. Do not use this type from application directly.</strong>
/// Returns new exception to notify that the unpacking collection is too large to represents in the current runtime environment.
/// </summary>
/// <returns><see cref="Exception"/> instance. It will not be <c>null</c>.</returns>
public static Exception NewIsTooLargeCollection()
{
#if DEBUG
Contract.Ensures( Contract.Result<Exception>() != null );
#endif // DEBUG
return new MessageNotSupportedException( "The collection which has more than Int32.MaxValue items is not supported." );
}
internal static void ThrowIsTooLargeCollection()
{
throw NewIsTooLargeCollection();
}
#if !AOT
internal static readonly MethodInfo ThrowNullIsProhibitedMethod = typeof( SerializationExceptions ).GetMethod( nameof( ThrowNullIsProhibited ), new[] { typeof( string ) } );
#endif // !AOT
/// <summary>
/// <strong>This is intended to MsgPack for CLI internal use. Do not use this type from application directly.</strong>
/// Returns new exception to notify that the member cannot be <c>null</c> or the unpacking value cannot be nil because nil value is explicitly prohibitted.
/// </summary>
/// <param name="memberName">The name of the member.</param>
/// <returns><see cref="Exception"/> instance. It will not be <c>null</c>.</returns>
public static Exception NewNullIsProhibited( string memberName )
{
#if DEBUG
Contract.Requires( !String.IsNullOrEmpty( memberName ) );
Contract.Ensures( Contract.Result<Exception>() != null );
#endif // DEBUG
return new SerializationException( String.Format( CultureInfo.CurrentCulture, "The member '{0}' cannot be nil.", memberName ) );
}
/// <summary>
/// <strong>This is intended to MsgPack for CLI internal use. Do not use this type from application directly.</strong>
/// Throws an exception to notify that the member cannot be <c>null</c> or the unpacking value cannot be nil because nil value is explicitly prohibitted.
/// </summary>
/// <param name="memberName">The name of the member.</param>
/// <exception cref="Exception">Always thrown.</exception>
public static void ThrowNullIsProhibited( string memberName )
{
throw NewNullIsProhibited( memberName );
}
/// <summary>
/// <strong>This is intended to MsgPack for CLI internal use. Do not use this type from application directly.</strong>
/// Returns new exception to notify that the unpacking value cannot be nil because the target member is read only and its type is collection.
/// </summary>
/// <param name="memberName">The name of the member.</param>
/// <returns><see cref="Exception"/> instance. It will not be <c>null</c>.</returns>
public static Exception NewReadOnlyMemberItemsMustNotBeNull( string memberName )
{
#if DEBUG
Contract.Requires( !String.IsNullOrEmpty( memberName ) );
Contract.Ensures( Contract.Result<Exception>() != null );
#endif // DEBUG
return new SerializationException( String.Format( CultureInfo.CurrentCulture, "The member '{0}' cannot be nil because it is read only member.", memberName ) );
}
/// <summary>
/// <strong>This is intended to MsgPack for CLI internal use. Do not use this type from application directly.</strong>
/// Returns new exception to notify that the unpacking collection value is not a collection.
/// </summary>
/// <param name="memberName">The name of the member.</param>
/// <returns><see cref="Exception"/> instance. It will not be <c>null</c>.</returns>
public static Exception NewStreamDoesNotContainCollectionForMember( string memberName )
{
#if DEBUG
Contract.Requires( !String.IsNullOrEmpty( memberName ) );
Contract.Ensures( Contract.Result<Exception>() != null );
#endif // DEBUG
return new SerializationException( String.Format( CultureInfo.CurrentCulture, "Cannot deserialize member '{0}' because the underlying stream does not contain collection.", memberName ) );
}
/// <summary>
/// <strong>This is intended to MsgPack for CLI internal use. Do not use this type from application directly.</strong>
/// Returns new exception to notify that the unpacking array size is not expected length.
/// </summary>
/// <param name="expectedLength">Expected, required for deserialization array length.</param>
/// <param name="actualLength">Actual array length.</param>
/// <returns><see cref="Exception"/> instance. It will not be <c>null</c>.</returns>
public static Exception NewUnexpectedArrayLength( int expectedLength, int actualLength )
{
#if DEBUG
Contract.Requires( expectedLength >= 0 );
Contract.Requires( actualLength >= 0 );
Contract.Ensures( Contract.Result<Exception>() != null );
#endif // DEBUG
return new SerializationException( String.Format( CultureInfo.CurrentCulture, "The MessagePack stream is invalid. Expected array length is {0}, but actual is {1}.", expectedLength, actualLength ) );
}
/// <summary>
/// <strong>This is intended to MsgPack for CLI internal use. Do not use this type from application directly.</strong>
/// Returns new exception to notify that it is failed to deserialize member.
/// </summary>
/// <param name="targetType">Deserializing type.</param>
/// <param name="memberName">The name of the deserializing member.</param>
/// <param name="inner">The exception which caused current error.</param>
/// <returns><see cref="Exception"/> instance. It will not be <c>null</c>.</returns>
public static Exception NewFailedToDeserializeMember( Type targetType, string memberName, Exception inner )
{
#if DEBUG
Contract.Requires( targetType != null );
Contract.Requires( !String.IsNullOrEmpty( memberName ) );
Contract.Requires( inner != null );
Contract.Ensures( Contract.Result<Exception>() != null );
#endif // DEBUG
return new SerializationException( String.Format( CultureInfo.CurrentCulture, "Cannot deserialize member '{0}' of type '{1}'.", memberName, targetType ), inner );
}
/// <summary>
/// Throws an exception to notify that it is failed to deserialize member.
/// </summary>
/// <param name="targetType">Deserializing type.</param>
/// <param name="memberName">The name of the deserializing member.</param>
/// <param name="inner">The exception which caused current error.</param>
internal static void ThrowFailedToDeserializeMember( Type targetType, string memberName, Exception inner )
{
throw NewFailedToDeserializeMember( targetType, memberName, inner );
}
#if !AOT
/// <summary>
/// <see cref="NewUnpackFromIsNotSupported(Type)"/>
/// </summary>
internal static readonly MethodInfo NewUnpackFromIsNotSupportedMethod =
typeof( SerializationExceptions ).GetMethod( nameof( NewUnpackFromIsNotSupported ), new[] { typeof( Type ) } );
#endif // !AOT
/// <summary>
/// Returns a new exception which represents <c>UnpackFrom</c> is not supported in this asymmetric serializer.
/// </summary>
/// <param name="targetType">Deserializing type.</param>
/// <returns>The exception. This value will not be <c>null</c>.</returns>
public static Exception NewUnpackFromIsNotSupported( Type targetType )
{
#if DEBUG
Contract.Requires( targetType != null );
#endif // DEBUG
return new NotSupportedException( String.Format( CultureInfo.CurrentCulture, "This operation is not supported for '{0}' because the serializer does not support UnpackFrom method.", targetType ) );
}
#if !AOT
/// <summary>
/// <see cref="NewCreateInstanceIsNotSupported(Type)"/>
/// </summary>
internal static readonly MethodInfo NewCreateInstanceIsNotSupportedMethod =
typeof( SerializationExceptions ).GetMethod( nameof( NewCreateInstanceIsNotSupported ), new[] { typeof( Type ) } );
#endif // !AOT
/// <summary>
/// Returns a new exception which represents <c>UnpackFrom</c> is not supported in this asymmetric serializer.
/// </summary>
/// <param name="targetType">Deserializing type.</param>
/// <returns>The exception. This value will not be <c>null</c>.</returns>
public static Exception NewCreateInstanceIsNotSupported( Type targetType )
{
#if DEBUG
Contract.Requires( targetType != null );
#endif // DEBUG
return new NotSupportedException( String.Format( CultureInfo.CurrentCulture, "This operation is not supported for '{0}' because the serializer does not support CreateInstance method.", targetType ) );
}
internal static Exception NewUnpackToIsNotSupported( Type type, Exception inner )
{
#if DEBUG
Contract.Requires( type != null );
#endif // DEBUG
return new NotSupportedException( String.Format( CultureInfo.CurrentCulture, "This operation is not supported for '{0}' because it does not have accesible Add(T) method.", type ), inner );
}
internal static Exception NewValueTypeCannotBePolymorphic( Type type )
{
return
new SerializationException(
String.Format( CultureInfo.CurrentCulture, "Value type '{0}' cannot be polymorphic.", type )
);
}
internal static Exception NewUnknownTypeEmbedding()
{
return new SerializationException( "Cannot deserialize with type-embedding based serializer. Root object must be 3 element array." );
}
internal static Exception NewIncompatibleCollectionSerializer( Type targetType, Type incompatibleType, Type exampleClass )
{
return
new SerializationException(
String.Format(
CultureInfo.CurrentCulture,
"Cannot serialize type '{0}' because registered or generated serializer '{1}' does not implement '{2}', which is implemented by '{3}', for example.",
targetType.GetFullName(),
incompatibleType.GetFullName(),
typeof( ICollectionInstanceFactory ),
exampleClass.GetFullName()
)
);
}
internal static void ThrowArgumentNullException( string parameterName )
{
throw new ArgumentNullException( parameterName );
}
internal static void ThrowArgumentNullException( string parameterName, string fieldName )
{
throw new ArgumentNullException( parameterName, String.Format( CultureInfo.CurrentCulture, "Field '{0}' of parameter '{1}' cannot be null.", fieldName, parameterName ) );
}
internal static void ThrowArgumentCannotBeNegativeException( string parameterName )
{
throw new ArgumentOutOfRangeException( parameterName, "The value cannot be negative number." );
}
internal static void ThrowArgumentCannotBeNegativeException( string parameterName, string fieldName )
{
throw new ArgumentOutOfRangeException( parameterName, String.Format( CultureInfo.CurrentCulture, "Field '{0}' of parameter '{1}' cannot be negative number.", fieldName, parameterName ) );
}
internal static void ThrowArgumentException( string parameterName, string message )
{
throw new ArgumentException( message, parameterName );
}
internal static void ThrowSerializationException( string message )
{
throw new SerializationException( message );
}
internal static void ThrowSerializationException( string message, Exception innerException )
{
throw new SerializationException( message, innerException );
}
#if UNITY && DEBUG
public
#else
internal
#endif
static void ThrowInvalidArrayItemsCount( Unpacker unpacker, Type targetType, int requiredCount )
{
throw
unpacker.IsCollectionHeader
? new SerializationException(
String.Format(
CultureInfo.CurrentCulture,
"Cannot deserialize type '{0}' because stream is not {1} elements array. Current type is {2} and its element count is {3}.",
targetType,
requiredCount,
unpacker.IsArrayHeader ? "array" : "map",
unpacker.LastReadData.AsInt64()
)
)
: new SerializationException(
String.Format(
CultureInfo.CurrentCulture,
"Cannot deserialize type '{0}' because stream is not {1} elements array. Current type is {2}.",
targetType,
requiredCount,
unpacker.LastReadData.UnderlyingType
)
);
}
}
}