forked from reactiveui/ReactiveUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonNetObjectSerializationProvider.cs
290 lines (255 loc) · 10.3 KB
/
JsonNetObjectSerializationProvider.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Threading;
using Newtonsoft.Json;
using Newtonsoft.Json.Bson;
using Newtonsoft.Json.Serialization;
namespace ReactiveUI.Serialization
{
public class JsonNetObjectSerializationProvider : IObjectSerializationProvider
{
ThreadLocal<SerializedItemsToGuidResolver> _guidResolver;
ThreadLocal<JsonSerializer> _serializer;
/// <summary>
/// Constructs a new JsonNetObjectSerializationProvider.
/// </summary>
/// <param name="engine">The engine to de/serialize dependent sub-objects.</param>
public JsonNetObjectSerializationProvider(IStorageEngine engine = null)
{
_guidResolver = new ThreadLocal<SerializedItemsToGuidResolver>(() =>
new SerializedItemsToGuidResolver(engine));
_serializer = new ThreadLocal<JsonSerializer>(() =>
JsonSerializer.Create(new JsonSerializerSettings() {ContractResolver = _guidResolver.Value }));
}
/// <summary>
/// Write an object to memory, including serializing all of the child
/// objects in the object graph. In Debug mode, this writes out JSON
/// using Unicode encoding, and in Release mode this writes out BSON.
/// </summary>
/// <param name="obj">The root object to serialize to disk.</param>
/// <returns>A byte representation of the object.</returns>
public byte[] Serialize(object obj)
{
var ms = new MemoryStream();
using (var writer = createWriterFromMemoryStream(ms)) {
_guidResolver.Value.InitializeWithRootObject(obj);
JsonSerializer
.Create(new JsonSerializerSettings() {ContractResolver = _guidResolver.Value})
.Serialize(writer, obj);
}
var ret = ms.ToArray();
return ret;
}
/// <summary>
/// Reads an object from the data returned by Serialize.
/// </summary>
/// <param name="data">The byte representation of the object.</param>
/// <param name="type">The type of the object to reconstruct.</param>
/// <returns>The deserialized object.</returns>
public object Deserialize(byte[] data, Type type)
{
using (var reader = createReaderFromBytes(data)) {
var dummy = Activator.CreateInstance(type);
_guidResolver.Value.InitializeWithRootObject(dummy);
return JsonSerializer
.Create(new JsonSerializerSettings() {ContractResolver = _guidResolver.Value})
.Deserialize(reader, type);
}
}
/// <summary>
/// SerializedDataToString is a method used for debugging purposes to
/// dump a serialized object out as a string. Production
/// implementations are free to return an empty string.
/// </summary>
/// <param name="data">A serialized object to examine.</param>
/// <returns>The string representation of the byte data (i.e. the JSON
/// string).</returns>
public string SerializedDataToString(byte[] data)
{
#if DEBUG
return (new StreamReader(new MemoryStream(data))).ReadToEnd();
#else
return "Run me under Debug mode";
#endif
}
static JsonReader createReaderFromBytes(byte[] data)
{
#if DEBUG
return new JsonTextReader(new StreamReader(new MemoryStream(data)));
#else
return new BsonReader(new MemoryStream(data));
#endif
}
static JsonWriter createWriterFromMemoryStream(MemoryStream ms)
{
#if DEBUG
return new JsonTextWriter(new StreamWriter(ms));
#else
return new BsonWriter(ms);
#endif
}
}
/// <summary>
/// This class decides what properties to serialize, and for each property,
/// whether we should hijack the serializer/deserializer.
/// </summary>
class SerializedItemsToGuidResolver : DefaultContractResolver
{
SerializableItemConverter _itemConverter;
SerializableListConverter _listConverter;
object _rootObject;
public SerializedItemsToGuidResolver(IStorageEngine engine = null)
{
_itemConverter = new SerializableItemConverter(engine);
_listConverter = new SerializableListConverter(engine);
}
protected override JsonContract CreateContract(Type objectType)
{
var ret = base.CreateContract(objectType);
if (typeof(ISerializableList).IsAssignableFrom(objectType)) {
return ret;
}
if ((typeof(ISerializableItem).IsAssignableFrom(objectType)) &&
(_rootObject == null || _rootObject.GetType() != objectType)) {
_rootObject = null;
ret.Converter = _itemConverter;
}
return ret;
}
protected override JsonArrayContract CreateArrayContract(Type objectType)
{
var ret = base.CreateArrayContract(objectType);
if (typeof(ISerializableList).IsAssignableFrom(objectType)) {
if (_rootObject.GetType() == objectType) {
_rootObject = null;
ret.Converter = _listConverter;
} else {
ret.Converter = _itemConverter;
}
}
return ret;
}
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization serialization)
{
return base.CreateProperties(type, serialization).Where(x => {
// XXX: This is massively slow and dumb
var prop = type.GetProperty(x.PropertyName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
object[] attrs;
if (prop != null) {
attrs = prop.GetCustomAttributes(typeof (IgnoreDataMemberAttribute), true);
} else {
var field = type.GetField(x.PropertyName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
attrs = field.GetCustomAttributes(typeof(IgnoreDataMemberAttribute), true);
}
return attrs.Length == 0;
}).ToList();
}
public void InitializeWithRootObject(object rootObject)
{
_rootObject = rootObject;
}
}
/// <summary>
/// This class hijacks ISerializableItems and replaces them with their
/// content hash written out as bytes.
/// </summary>
class SerializableItemConverter : JsonConverter
{
IStorageEngine _engine;
public SerializableItemConverter(IStorageEngine engine = null)
{
_engine = engine ?? RxStorage.Engine;
}
public override bool CanConvert(Type objectType)
{
return (typeof (ISerializableItem).IsAssignableFrom(objectType));
}
public override object ReadJson(
JsonReader reader,
Type objectType,
object existingValue,
JsonSerializer serializer)
{
Guid contentHash;
var bytes = serializer.Deserialize<byte[]>(reader);
if (bytes == null || bytes.Length == 0 || (contentHash = new Guid(bytes)) == Guid.Empty) {
return null;
}
return _engine.Load(contentHash);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var si = (ISerializableItem)value;
if (si == null) {
serializer.Serialize(writer, Guid.Empty.ToByteArray());
return;
}
_engine.Save(si);
serializer.Serialize(writer, si.ContentHash.ToByteArray());
}
}
class RawSerializedListData {
public string CollectionTypeFullName { get; set; }
public Dictionary<Guid, DateTimeOffset> CreatedOn { get; set; }
public Dictionary<Guid, DateTimeOffset> UpdatedOn { get; set; }
public Guid[] Items { get; set; }
}
/// <summary>
/// This class hijacks ISerializableLists and replaces them with a custom
/// class representing the actual data.
/// </summary>
class SerializableListConverter : JsonConverter
{
IStorageEngine _engine;
public SerializableListConverter(IStorageEngine engine = null)
{
_engine = engine ?? RxStorage.Engine;
}
public override bool CanConvert(Type objectType)
{
return (typeof (ISerializableList).IsAssignableFrom(objectType));
}
public override object ReadJson(
JsonReader reader,
Type objectType,
object existingValue,
JsonSerializer serializer)
{
var toRead = serializer.Deserialize<RawSerializedListData>(reader);
var type = Type.GetType(toRead.CollectionTypeFullName);
var list = (ISerializableList) Activator.CreateInstance(type);
list.ChangeTrackingEnabled = false;
foreach(var hash in toRead.Items) {
list.Add(_engine.Load(hash));
}
foreach(var kvp in toRead.CreatedOn) {
list.CreatedOn.Add(kvp.Key, kvp.Value);
}
foreach(var kvp in toRead.UpdatedOn) {
list.UpdatedOn.Add(kvp.Key, kvp.Value);
}
list.ChangeTrackingEnabled = true;
return list;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var slist = (ISerializableList)value;
var toWrite = new RawSerializedListData() {
CreatedOn = slist.CreatedOn.ToConcreteDictionary(),
UpdatedOn = slist.UpdatedOn.ToConcreteDictionary(),
CollectionTypeFullName = slist.GetType().FullName,
Items = slist.OfType<ISerializableItem>().Select(x => x.ContentHash).ToArray(),
};
foreach(ISerializableItem v in slist) {
_engine.Save(v);
}
serializer.Serialize(writer, toWrite);
}
}
}
// vim: tw=120 ts=4 sw=4 et :