-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathSample06_CustomAttributes.cs
82 lines (70 loc) · 2.93 KB
/
Sample06_CustomAttributes.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
#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.Serialization;
namespace Samples
{
/// <summary>
/// Sample code to describe MessagePackMember usage.
/// </summary>
/// <remarks>You can tweak serialization behavior via custom attributes.</remarks>
public class MessagePackMemberSample
{
[MessagePackMember(
0, // Specify 0 based index for serialized array. You should specify this value to ensure interoperability with other platform bindings.
NilImplication = NilImplication.MemberDefault // Specify the behavior when unpacked value is nil -- default is Null for reference types (set null), Prohibit for value types (throw exception).
)]
public long FileId { get; set; }
[MessagePackMember( 1 )] // NilImplication = Null
public string Name { get; set; }
[MessagePackMember( 2 )] // NilImplication = Prohibit
[MessagePackEnumMember( SerializationMethod = EnumMemberSerializationMethod.ByUnderlyingValue )]// Specify enum serialization method for this member. This value overrides the setting of the context and the setting of the enum type itself.
public FileAttributes Attributes { get; set; }
// When there are marked members, non-marked members are excluded from serialization/deserialization candidates.
public int PropertyWillNotBeSerialized { get; set; }
}
// ... Or you can use DataMemberAttribute for compabitility among other libraries.
public class DataContractSample
{
[DataMember( Name = "Id", Order = 1 )] // Order can be used to interop too.
public long Id { get; set; }
[DataMember( Name = "Title", Order = 2 )]
public string Title { get; set; }
}
// ... You can "opt-out" with MessagePackIgnore
public class OptOutSample
{
[MessagePackIgnore]
public string ShouldNotEmit { get; set; }
public int ShouldEmit { get; set; }
}
}
namespace System.Runtime.Serialization
{
// Tips: You don't have to refer System.Runtime.Serialization assembly. MessagePack for CLI just see Type.FullName.
public sealed class DataMemberAttribute : Attribute
{
public string Name { get; set; }
public int Order { get; set; }
}
// This is also about MessagePackIgnoreAttribute, MessagePackMemberAttribute, and MessagePackDeserializationConstructorAttribute.
}