forked from msgpack/msgpack-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystem_VersionMessagePackSerializer.cs
More file actions
156 lines (133 loc) · 5.13 KB
/
System_VersionMessagePackSerializer.cs
File metadata and controls
156 lines (133 loc) · 5.13 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
#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 --
using System;
#if FEATURE_TAP
using System.Threading;
using System.Threading.Tasks;
#endif // FEATURE_TAP
namespace MsgPack.Serialization.DefaultSerializers
{
// ReSharper disable once InconsistentNaming
internal sealed class System_VersionMessagePackSerializer : MessagePackSerializer<Version>
{
public System_VersionMessagePackSerializer( SerializationContext ownerContext )
: base( ownerContext, SerializerCapabilities.PackTo | SerializerCapabilities.UnpackFrom ) { }
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods", MessageId = "0", Justification = "Validated internally" )]
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods", MessageId = "1", Justification = "Validated internally" )]
protected internal override void PackToCore( Packer packer, Version objectTree )
{
packer.PackArrayHeader( 4 );
packer.Pack( objectTree.Major );
packer.Pack( objectTree.Minor );
packer.Pack( objectTree.Build );
packer.Pack( objectTree.Revision );
}
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods", MessageId = "0", Justification = "Validated internally" )]
protected internal override Version UnpackFromCore( Unpacker unpacker )
{
if ( !unpacker.IsArrayHeader )
{
SerializationExceptions.ThrowInvalidArrayItemsCount( unpacker, typeof( Version ), 4 );
}
long length = UnpackHelpers.GetItemsCount( unpacker );
if ( length != 4 )
{
SerializationExceptions.ThrowInvalidArrayItemsCount( unpacker, typeof( Version ), 4 );
}
int major, minor, build, revision;
if ( !unpacker.ReadInt32( out major ) )
{
SerializationExceptions.ThrowMissingItem( 0, unpacker );
}
if ( !unpacker.ReadInt32( out minor ) )
{
SerializationExceptions.ThrowMissingItem( 1, unpacker );
}
if ( !unpacker.ReadInt32( out build ) )
{
SerializationExceptions.ThrowMissingItem( 2, unpacker );
}
if ( !unpacker.ReadInt32( out revision ) )
{
SerializationExceptions.ThrowMissingItem( 3, unpacker );
}
if (build < 0 && revision < 0)
{
return new Version(major, minor);
}
else if (revision < 0)
{
return new Version(major, minor, build);
}
return new Version( major, minor, build, revision );
}
#if FEATURE_TAP
protected internal override async Task PackToAsyncCore( Packer packer, Version objectTree, CancellationToken cancellationToken )
{
await packer.PackArrayHeaderAsync( 4, cancellationToken ).ConfigureAwait( false );
await packer.PackAsync( objectTree.Major, cancellationToken ).ConfigureAwait( false );
await packer.PackAsync( objectTree.Minor, cancellationToken ).ConfigureAwait( false );
await packer.PackAsync( objectTree.Build, cancellationToken ).ConfigureAwait( false );
await packer.PackAsync( objectTree.Revision, cancellationToken ).ConfigureAwait( false );
}
protected internal override async Task<Version> UnpackFromAsyncCore( Unpacker unpacker, CancellationToken cancellationToken )
{
if ( !unpacker.IsArrayHeader )
{
SerializationExceptions.ThrowInvalidArrayItemsCount( unpacker, typeof( Version ), 4 );
}
long length = unpacker.LastReadData.AsInt64();
if ( length != 4 )
{
SerializationExceptions.ThrowInvalidArrayItemsCount( unpacker, typeof( Version ), 4 );
}
var major = await unpacker.ReadInt32Async( cancellationToken ).ConfigureAwait( false );
if ( !major.Success )
{
SerializationExceptions.ThrowMissingItem( 0, unpacker );
}
var minor = await unpacker.ReadInt32Async( cancellationToken ).ConfigureAwait( false );
if ( !minor.Success )
{
SerializationExceptions.ThrowMissingItem( 1, unpacker );
}
var build = await unpacker.ReadInt32Async( cancellationToken ).ConfigureAwait( false );
if ( !build.Success )
{
SerializationExceptions.ThrowMissingItem( 2, unpacker );
}
var revision = await unpacker.ReadInt32Async( cancellationToken ).ConfigureAwait( false );
if ( !revision.Success )
{
SerializationExceptions.ThrowMissingItem( 3, unpacker );
}
if (build.Value < 0 && revision.Value < 0)
{
return new Version(major.Value, minor.Value);
}
else if (revision.Value < 0)
{
return new Version(major.Value, minor.Value, build.Value);
}
return new Version( major.Value, minor.Value, build.Value, revision.Value );
}
#endif // FEATURE_TAP
}
}