-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTextEncodingSerializer.cs
More file actions
148 lines (140 loc) · 5.64 KB
/
TextEncodingSerializer.cs
File metadata and controls
148 lines (140 loc) · 5.64 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
// =================================================================================================================================
// Copyright (c) RapidField LLC. Licensed under the MIT License. See LICENSE.txt in the project root for license information.
// =================================================================================================================================
using RapidField.SolidInstruments.Core.ArgumentValidation;
using System;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Text;
namespace RapidField.SolidInstruments.Serialization
{
/// <summary>
/// Supports text encoding and decoding in place of a serializer.
/// </summary>
public abstract class TextEncodingSerializer : Serializer<String>
{
/// <summary>
/// Initializes a new instance of the <see cref="TextEncodingSerializer" /> class.
/// </summary>
[DebuggerHidden]
internal TextEncodingSerializer()
: base()
{
CharacterEncoding = null;
}
/// <summary>
/// Initializes a new instance of the <see cref="TextEncodingSerializer" /> class.
/// </summary>
/// <param name="characterEncoding">
/// The character encoding that is used for serialization and deserialization.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="characterEncoding" /> is <see langword="null" />.
/// </exception>
protected TextEncodingSerializer(Encoding characterEncoding)
: base()
{
CharacterEncoding = characterEncoding.RejectIf().IsNull(nameof(characterEncoding));
}
/// <summary>
/// Converts the specified bit field to its typed equivalent.
/// </summary>
/// <param name="serializedObject">
/// A serialized object.
/// </param>
/// <param name="format">
/// The format to use for deserialization.
/// </param>
/// <returns>
/// The deserialized object.
/// </returns>
/// <exception cref="SerializationException">
/// <paramref name="serializedObject" /> is invalid or an error occurred during deserialization.
/// </exception>
protected override String Deserialize(Byte[] serializedObject, SerializationFormat format)
{
try
{
return Deserialize(serializedObject, CharacterEncoding);
}
catch (Exception exception)
{
throw new SerializationException("An error occurred during deserialization. See inner exception.", exception);
}
}
/// <summary>
/// Converts the specified object to a bit field.
/// </summary>
/// <param name="target">
/// An object to be serialized.
/// </param>
/// <param name="format">
/// The format to use for serialization.
/// </param>
/// <returns>
/// The serialized bit field.
/// </returns>
/// <exception cref="SerializationException">
/// <paramref name="target" /> is invalid or an error occurred during serialization.
/// </exception>
protected override Byte[] Serialize(String target, SerializationFormat format)
{
try
{
return Serialize(target, CharacterEncoding);
}
catch (Exception exception)
{
throw new SerializationException("An error occurred during serialization. See inner exception.", exception);
}
}
/// <summary>
/// Converts the specified bit field to its typed equivalent.
/// </summary>
/// <param name="serializedObject">
/// A serialized object.
/// </param>
/// <param name="characterEncoding">
/// The character encoding that is used for deserialization.
/// </param>
/// <returns>
/// The deserialized object.
/// </returns>
/// <exception cref="ArgumentException">
/// <paramref name="serializedObject" /> is invalid.
/// </exception>
/// <exception cref="ArgumentNullException">
/// <paramref name="serializedObject" /> is invalid.
/// </exception>
/// <exception cref="DecoderFallbackException">
/// A fallback occurred.
/// </exception>
[DebuggerHidden]
private static String Deserialize(Byte[] serializedObject, Encoding characterEncoding) => characterEncoding.GetString(serializedObject);
/// <summary>
/// Converts the specified object to a bit field.
/// </summary>
/// <param name="target">
/// An object to be serialized.
/// </param>
/// <param name="characterEncoding">
/// The character encoding that is used for serialization.
/// </param>
/// <returns>
/// The serialized output.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="target" /> is <see langword="null" />.
/// </exception>
/// <exception cref="EncoderFallbackException">
/// A fallback occurred.
/// </exception>
[DebuggerHidden]
private static Byte[] Serialize(String target, Encoding characterEncoding) => characterEncoding.GetBytes(target);
/// <summary>
/// Represents the character encoding that is used for serialization and deserialization.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly Encoding CharacterEncoding;
}
}