forked from MessagePack-CSharp/MessagePack-CSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Definitions.cs
163 lines (145 loc) · 4.97 KB
/
Definitions.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MessagePack.CodeGenerator
{
public interface IResolverRegisterInfo
{
string FullName { get; }
string FormatterName { get; }
}
public class ObjectSerializationInfo : IResolverRegisterInfo
{
public string Name { get; set; }
public string FullName { get; set; }
public string Namespace { get; set; }
public bool IsIntKey { get; set; }
public bool IsStringKey { get { return !IsIntKey; } }
public bool IsClass { get; set; }
public bool IsStruct { get { return !IsClass; } }
public MemberSerializationInfo[] ConstructorParameters { get; set; }
public MemberSerializationInfo[] Members { get; set; }
public bool HasIMessagePackSerializationCallbackReceiver { get; set; }
public bool NeedsCastOnBefore { get; set; }
public bool NeedsCastOnAfter { get; set; }
public string FormatterName => (Namespace == null ? Name : Namespace + "." + Name) + "Formatter";
public int WriteCount
{
get
{
if (IsStringKey)
{
return Members.Count(x => x.IsReadable);
}
else
{
return MaxKey;
}
}
}
public int MaxKey
{
get
{
return Members.Where(x => x.IsReadable).Select(x => x.IntKey).DefaultIfEmpty(-1).Max();
}
}
public MemberSerializationInfo GetMember(int index)
{
return Members.FirstOrDefault(x => x.IntKey == index);
}
public string GetConstructorString()
{
var args = string.Join(", ", ConstructorParameters.Select(x => "__" + x.Name + "__"));
return $"{FullName}({args})";
}
}
public class MemberSerializationInfo
{
public bool IsProperty { get; set; }
public bool IsField { get; set; }
public bool IsWritable { get; set; }
public bool IsReadable { get; set; }
public int IntKey { get; set; }
public string StringKey { get; set; }
public string Type { get; set; }
public string Name { get; set; }
public string ShortTypeName { get; set; }
readonly HashSet<string> primitiveTypes = new HashSet<string>(new string[]
{
"short",
"int",
"long",
"ushort",
"uint",
"ulong",
"float",
"double",
"bool",
"byte",
"sbyte",
"char",
//"global::System.DateTime",
//"byte[]",
//"string",
});
public string GetSerializeMethodString()
{
if (primitiveTypes.Contains(Type))
{
return $"MessagePackBinary.Write{ShortTypeName.Replace("[]", "s")}(ref bytes, offset, value.{Name})";
}
else
{
return $"formatterResolver.GetFormatterWithVerify<{Type}>().Serialize(ref bytes, offset, value.{Name}, formatterResolver)";
}
}
public string GetDeserializeMethodString()
{
if (primitiveTypes.Contains(Type))
{
return $"MessagePackBinary.Read{ShortTypeName.Replace("[]", "s")}(bytes, offset, out readSize)";
}
else
{
return $"formatterResolver.GetFormatterWithVerify<{Type}>().Deserialize(bytes, offset, formatterResolver, out readSize)";
}
}
}
public class EnumSerializationInfo : IResolverRegisterInfo
{
public string Namespace { get; set; }
public string Name { get; set; }
public string FullName { get; set; }
public string UnderlyingType { get; set; }
public string FormatterName => (Namespace == null ? Name : Namespace + "." + Name) + "Formatter";
}
public class GenericSerializationInfo : IResolverRegisterInfo, IEquatable<GenericSerializationInfo>
{
public string FullName { get; set; }
public string FormatterName { get; set; }
public bool Equals(GenericSerializationInfo other)
{
return FullName.Equals(other.FullName);
}
public override int GetHashCode()
{
return FullName.GetHashCode();
}
}
public class UnionSerializationInfo : IResolverRegisterInfo
{
public string Namespace { get; set; }
public string Name { get; set; }
public string FullName { get; set; }
public string FormatterName => (Namespace == null ? Name : Namespace + "." + Name) + "Formatter";
public UnionSubTypeInfo[] SubTypes { get; set; }
}
public class UnionSubTypeInfo
{
public string Type { get; set; }
public int Key { get; set; }
}
}