-
Notifications
You must be signed in to change notification settings - Fork 325
/
Copy pathSerializer.cs
98 lines (88 loc) · 3.38 KB
/
Serializer.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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace Bond
{
using System;
using System.Linq;
using Bond.Expressions;
/// <summary>
/// Serialize objects
/// </summary>
public static class Serialize
{
static class Cache<W, T>
{
public static readonly Serializer<W> Instance = new Serializer<W>(typeof(T));
}
/// <summary>
/// Serialize object of type T to protocol writer of type W
/// </summary>
/// <typeparam name="W">Protocol writer</typeparam>
/// <typeparam name="T">Type representing a Bond schema</typeparam>
/// <param name="writer">Writer instance</param>
/// <param name="obj">Object to serialize</param>
public static void To<W, T>(W writer, T obj)
{
Cache<W, T>.Instance.Serialize(obj, writer);
}
/// <summary>
/// Serialize IBonded<T> to protocol writer of type W
/// </summary>
/// <typeparam name="W">Protocol writer</typeparam>
/// <typeparam name="T">Type representing a Bond schema</typeparam>
/// <param name="writer">Writer instance</param>
/// <param name="bonded">IBonded instance</param>
public static void To<W, T>(W writer, IBonded<T> bonded)
{
bonded.Serialize(writer);
}
/// <summary>
/// Serialize IBonded to protocol writer of type W
/// </summary>
/// <typeparam name="W">Protocol writer</typeparam>
/// <param name="writer">Writer instance</param>
/// <param name="bonded">IBonded instance</param>
public static void To<W>(W writer, IBonded bonded)
{
bonded.Serialize(writer);
}
}
/// <summary>
/// Serializer for protocol writer W
/// </summary>
/// <typeparam name="W">Protocol writer</typeparam>
public class Serializer<W>
{
readonly Action<object, W>[] serialize;
/// <summary>
/// Create a serializer for specified type
/// </summary>
/// <param name="type">Type representing a Bond schema</param>
public Serializer(Type type) : this(type, inlineNested: true) { }
/// <summary>
/// Create a serializer for specified type
/// </summary>
/// <param name="type">Type representing a Bond schema</param>
/// <param name="inlineNested">Indicates whether nested struct serialization code may be inlined</param>
public Serializer(Type type, bool inlineNested)
{
var parser = new ObjectParser(type);
serialize = SerializerGeneratorFactory<object, W>.Create(
(o, w, i) => serialize[i](o, w), type, inlineNested)
.Generate(parser)
.Select(lambda => lambda.Compile()).ToArray();
}
/// <summary>
/// Serialize object using protocol writer of type W
/// </summary>
/// <param name="obj">Object to serialize</param>
/// <param name="writer">Writer instance</param>
/// <remarks>
/// The object must be of type used to create the Serializer, otherwise behavior is undefined
/// </remarks>
public void Serialize(object obj, W writer)
{
serialize[0](obj, writer);
}
}
}