-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathSample05_PackableAndUnpackable.cs
164 lines (142 loc) · 4.35 KB
/
Sample05_PackableAndUnpackable.cs
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
#region -- License Terms --
//
// MessagePack for CLI
//
// Copyright (C) 2010-2012 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;
using System.IO;
using System.Runtime.Serialization;
using MsgPack;
using MsgPack.Serialization;
using NUnit.Framework; // For running checking
namespace Samples
{
/// <summary>
/// A sample code to describe SerializationContext usage.
/// </summary>
[TestFixture]
public class PackageAndUnpackableSample
{
[Test]
public void RegisterAndUseCustomSerializer()
{
var targetObject = new PackableUnpackableObject();
var stream = new MemoryStream();
// You can serialize/deserialize objects which implement IPackable and/or IUnpackable as usual.
var serializer = MessagePackSerializer.Get<PackableUnpackableObject>();
serializer.Pack( stream, targetObject );
stream.Position = 0;
var deserializedObject = serializer.Unpack( stream );
}
}
/// <summary>
/// A custom serializer sample: Serialize <see cref="System.DateTime"/> as UTC.
/// </summary>
public class PackableUnpackableObject : IPackable, IUnpackable
{
// Imagine when you cannot use auto-generated serializer so you have to implement custom serializer for own type easily.
public long Id { get; set; }
public string Name { get; set; }
public void PackToMessage( Packer packer, PackingOptions options )
{
// Pack fields are here:
// First, record total fields size.
packer.PackArrayHeader( 2 );
packer.Pack( this.Id );
packer.PackString( this.Name );
// ...Instead, you can pack as map as follows:
// packer.PackMapHeader( 2 );
// packer.Pack( "Id" );
// packer.Pack( this.Id );
// packer.Pack( "Name" );
// packer.Pack( this.Name );
}
public void UnpackFromMessage( Unpacker unpacker )
{
// Unpack fields are here:
// temp variables
long id;
string name;
// It should be packed as array because we use hand-made packing implementation above.
if ( !unpacker.IsArrayHeader )
{
throw new SerializationException( "Is not in array header." );
}
// Check items count.
if ( UnpackHelpers.GetItemsCount( unpacker ) != 2 )
{
throw new SerializationException( $"Array length should be 2, but {UnpackHelpers.GetItemsCount( unpacker )}" );
}
// Unpack fields here:
if ( !unpacker.ReadInt64( out id ) )
{
throw new SerializationException( "Property Id is not found." );
}
this.Id = id;
if ( !unpacker.ReadString( out name ) )
{
throw new SerializationException( "Property Name is not found." );
}
this.Name = name;
// ...Instead, you can unpack from map as follows:
//if ( !unpacker.IsMapHeader )
//{
// throw SerializationExceptions.NewIsNotMapHeader();
//}
//// Check items count.
//if ( UnpackHelpers.GetItemsCount( unpacker ) != 2 )
//{
// throw SerializationExceptions.NewUnexpectedArrayLength( 2, UnpackHelpers.GetItemsCount( unpacker ) );
//}
//// Unpack fields here:
//for ( int i = 0; i < 2 /* known count of fields */; i++ )
//{
// // Unpack and verify key of entry in map.
// string key;
// if ( !unpacker.ReadString( out key ) )
// {
// // Missing key, incorrect.
// throw SerializationExceptions.NewUnexpectedEndOfStream();
// }
// switch ( key )
// {
// case "Id":
// {
// if ( !unpacker.ReadInt64( out id ) )
// {
// throw SerializationExceptions.NewMissingProperty( "Id" );
// }
//
// this.Id = id;
// break;
// }
// case "Name":
// {
// if ( !unpacker.ReadString( out name ) )
// {
// throw SerializationExceptions.NewMissingProperty( "Name" );
// }
//
// this.Name = name;
// break;
// }
// // Note: You should ignore unknown fields for forward compatibility.
// }
//}
}
}
}