FormatterExtensionsSerializer.cs
5.2 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace UniJSON
{
public static class FormatterExtensionsSerializer
{
public static void SerializeDictionary(this IFormatter f, IDictionary<string, object> dictionary)
{
f.BeginMap(dictionary.Count);
foreach (var kv in dictionary)
{
f.Key(kv.Key);
f.SerializeObject(kv.Value);
}
f.EndMap();
}
public static void SerializeArray<T>(this IFormatter f, IEnumerable<T> values)
{
f.BeginList(values.Count());
foreach (var value in values)
{
f.Serialize(value);
}
f.EndList();
}
public static void SerializeObjectArray(this IFormatter f, object[] array)
{
f.BeginList(array.Length);
foreach (var x in array)
{
f.SerializeObject(x);
}
f.EndList();
}
public static void SerializeObject(this IFormatter f, object value)
{
if (value == null)
{
f.Null();
}
else
{
typeof(FormatterExtensionsSerializer).GetMethod("Serialize")
.MakeGenericMethod(value.GetType()).Invoke(null, new object[] { f, value });
}
}
public static void Serialize<T>(this IFormatter f, T arg)
{
if (arg == null)
{
f.Null();
return;
}
GenericSerializer<T>.Serialize(f, arg);
}
public static void SetCustomSerializer<T>(Action<IFormatter, T> serializer)
{
GenericSerializer<T>.Set(serializer);
}
public static MethodInfo GetMethod(string name)
{
return typeof(FormatterExtensionsSerializer).GetMethod(name);
}
}
static class GenericSerializer<T>
{
delegate void Serializer(IFormatter f, T t);
static Action<IFormatter, T> GetSerializer()
{
var t = typeof(T);
// object
if (typeof(T) == typeof(object) && t.GetType() != typeof(object))
{
var mi = FormatterExtensionsSerializer.GetMethod("SerializeObject");
return GenericInvokeCallFactory.StaticAction<IFormatter, T>(mi);
}
try
{
// primitive
var mi = typeof(IFormatter).GetMethod("Value", new Type[] { t });
if (mi != null)
{
return GenericInvokeCallFactory.OpenAction<IFormatter, T>(mi);
}
}
catch (AmbiguousMatchException)
{
// do nothing
}
{
// dictionary
var idictionary = t.GetInterfaces().FirstOrDefault(x =>
x.IsGenericType
&& x.GetGenericTypeDefinition() == typeof(IDictionary<,>)
&& x.GetGenericArguments()[0] == typeof(string)
);
if (idictionary != null)
{
var mi = FormatterExtensionsSerializer.GetMethod("SerializeDictionary");
return GenericInvokeCallFactory.StaticAction<IFormatter, T>(mi);
}
}
{
// object[]
if (t == typeof(object[]))
{
var mi = FormatterExtensionsSerializer.GetMethod("SerializeObjectArray");
return GenericInvokeCallFactory.StaticAction<IFormatter, T>(mi);
}
}
{
// list
var ienumerable = t.GetInterfaces().FirstOrDefault(x =>
x.IsGenericType
&& x.GetGenericTypeDefinition() == typeof(IEnumerable<>)
);
if (ienumerable != null)
{
var g = FormatterExtensionsSerializer.GetMethod("SerializeArray");
var mi = g.MakeGenericMethod(ienumerable.GetGenericArguments());
return GenericInvokeCallFactory.StaticAction<IFormatter, T>(mi);
}
}
{
// reflection
var schema = JsonSchema.FromType<T>();
return (IFormatter f, T value) =>
{
var c = new JsonSchemaValidationContext(value)
{
EnableDiagnosisForNotRequiredFields = true
};
schema.Serialize(f, value, c);
};
}
//throw new NotImplementedException();
}
static Serializer s_serializer;
public static void Set(Action<IFormatter, T> serializer)
{
s_serializer = new Serializer(serializer);
}
public static void Serialize(IFormatter f, T t)
{
if (s_serializer == null)
{
s_serializer = new Serializer(GetSerializer());
}
s_serializer(f, t);
}
}
}