Skip to content

Commit c5bd067

Browse files
committed
Add singleton pattern calls
1 parent 15ed477 commit c5bd067

File tree

7 files changed

+58
-19
lines changed

7 files changed

+58
-19
lines changed

src/Es.Serializer.Jil/JilSerializer.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ public class JilSerializer : ObjectSerializerBase
77
{
88
private Jil.Options _options;
99

10+
/// <summary>
11+
/// JilSerializer Instance
12+
/// </summary>
13+
public static JilSerializer Instance = new JilSerializer();
14+
1015
public JilSerializer(Jil.Options options) {
1116
_options = options;
1217
}

src/Es.Serializer.JsonNet/JsonNetSerializer.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ public class JsonNetSerializer : ObjectSerializerBase
1212
#else
1313
private static readonly Formatting _format = Formatting.None;
1414
#endif
15+
16+
/// <summary>
17+
/// JsonNetSerializer Instance
18+
/// </summary>
19+
public static JsonNetSerializer Instance = new JsonNetSerializer();
20+
21+
1522
private readonly JsonSerializerSettings _setting;
1623

1724
public JsonNetSerializer(JsonSerializerSettings setting) {

src/Es.Serializer.NetSerializer/NetSerializer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace Es.Serializer
2424
/// </summary>
2525
public class NETSerializer : ObjectSerializerBase
2626
{
27+
2728
/// <summary>
2829
/// The _serializer
2930
/// </summary>

src/Es.Serializer.ProtoBuf/ProtoBufSerializer.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ namespace Es.Serializer
66
{
77
public class ProtoBufSerializer : ObjectSerializerBase
88
{
9+
/// <summary>
10+
/// ProtoBufSerializer Instance
11+
/// </summary>
12+
public static ProtoBufSerializer Instance = new ProtoBufSerializer();
13+
914
public override void Serialize(object value, Stream output) {
1015
RuntimeTypeModel.Default.Serialize(output, value);
1116
}

src/Es.Serializer/BinarySerializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public override string SerializeToString(object value)
7979
public override object DeserializeFromString(string serializedText, Type type)
8080
{
8181
var data = FromHex(serializedText);
82-
using (MemoryStream mem = new MemoryStream(data))
82+
using (MemoryStream mem = MemoryStreamFactory.GetStream(data))
8383
{
8484
return Deserialize(mem, type);
8585
}

src/Es.Serializer/ObjectSerializerBase.cs

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ public abstract class ObjectSerializerBase : IObjectSerializer, IStringSerialize
5555
/// <param name="reader">The reader.</param>
5656
/// <param name="type">The type.</param>
5757
/// <returns>System.Object.</returns>
58-
public object Deserialize(TextReader reader, Type type) {
58+
public object Deserialize(TextReader reader, Type type)
59+
{
5960
return DeserializeCore(reader, type);
6061
}
6162

@@ -65,7 +66,8 @@ public object Deserialize(TextReader reader, Type type) {
6566
/// <param name="stream">The stream.</param>
6667
/// <param name="type">The type.</param>
6768
/// <returns>System.Object.</returns>
68-
public virtual object Deserialize(Stream stream, Type type) {
69+
public virtual object Deserialize(Stream stream, Type type)
70+
{
6971
#if NET40
7072
using (StreamReader reader = new StreamReader(stream, Encoding, true, bufferSize))
7173
#else
@@ -80,8 +82,10 @@ public virtual object Deserialize(Stream stream, Type type) {
8082
/// <param name="data">The data.</param>
8183
/// <param name="type">The type.</param>
8284
/// <returns>System.Object.</returns>
83-
public virtual object Deserialize(byte[] data, Type type) {
84-
using (var mem = MemoryStreamFactory.GetStream(data)) {
85+
public virtual object Deserialize(byte[] data, Type type)
86+
{
87+
using (var mem = MemoryStreamFactory.GetStream(data))
88+
{
8589
return Deserialize(mem, type);
8690
}
8791
}
@@ -91,7 +95,8 @@ public virtual object Deserialize(byte[] data, Type type) {
9195
/// </summary>
9296
/// <param name="value">The value.</param>
9397
/// <param name="writer">The writer.</param>
94-
public void Serialize(object value, TextWriter writer) {
98+
public void Serialize(object value, TextWriter writer)
99+
{
95100
SerializeCore(value, writer);
96101
}
97102

@@ -100,7 +105,8 @@ public void Serialize(object value, TextWriter writer) {
100105
/// </summary>
101106
/// <param name="value">The value.</param>
102107
/// <param name="output">The output.</param>
103-
public virtual void Serialize(object value, Stream output) {
108+
public virtual void Serialize(object value, Stream output)
109+
{
104110
#if NET40
105111
using (StreamWriter sw = new StreamWriter(output, Encoding, bufferSize))
106112
#else
@@ -114,8 +120,10 @@ public virtual void Serialize(object value, Stream output) {
114120
/// </summary>
115121
/// <param name="value">The value.</param>
116122
/// <param name="output">The output.</param>
117-
public virtual void Serialize(object value, out byte[] output) {
118-
using (var mem = MemoryStreamFactory.GetStream()) {
123+
public virtual void Serialize(object value, out byte[] output)
124+
{
125+
using (var mem = MemoryStreamFactory.GetStream())
126+
{
119127
Serialize(value, mem);
120128
output = mem.ToArray();
121129
}
@@ -126,10 +134,20 @@ public virtual void Serialize(object value, out byte[] output) {
126134
/// </summary>
127135
/// <param name="value">The value.</param>
128136
/// <returns>System.String.</returns>
129-
public virtual string SerializeToString(object value) {
130-
using (StringWriter writer = new StringWriter()) {
131-
SerializeCore(value, writer);
132-
return writer.ToString();
137+
public virtual string SerializeToString(object value)
138+
{
139+
using (var ms = MemoryStreamFactory.GetStream())
140+
{
141+
using (StreamWriter writer = new StreamWriter(ms))
142+
{
143+
SerializeCore(value, writer);
144+
145+
writer.Flush();
146+
147+
ms.Seek(0, SeekOrigin.Begin);
148+
var reader = new StreamReader(ms);
149+
return reader.ReadToEnd();
150+
}
133151
}
134152
}
135153

@@ -139,8 +157,10 @@ public virtual string SerializeToString(object value) {
139157
/// <param name="serializedText">The serialized text.</param>
140158
/// <param name="type">The type.</param>
141159
/// <returns>System.Object.</returns>
142-
public virtual object DeserializeFromString(string serializedText, Type type) {
143-
using (StringReader reader = new StringReader(serializedText)) {
160+
public virtual object DeserializeFromString(string serializedText, Type type)
161+
{
162+
using (StringReader reader = new StringReader(serializedText))
163+
{
144164
return DeserializeCore(reader, type);
145165
}
146166
}
@@ -151,7 +171,8 @@ public virtual object DeserializeFromString(string serializedText, Type type) {
151171
/// </summary>
152172
/// <param name="data">The data.</param>
153173
/// <returns>System.String.</returns>
154-
protected static string ToHex(byte[] data) {
174+
protected static string ToHex(byte[] data)
175+
{
155176
return BitConverter.ToString(data).Replace("-", string.Empty);
156177
}
157178

@@ -161,7 +182,8 @@ protected static string ToHex(byte[] data) {
161182
/// </summary>
162183
/// <param name="hex">The hexadecimal.</param>
163184
/// <returns>System.Byte[].</returns>
164-
protected static byte[] FromHex(string hex) {
185+
protected static byte[] FromHex(string hex)
186+
{
165187
if (string.IsNullOrEmpty(hex)) return new byte[0];
166188
if ((hex.Length % 2) != 0)
167189
hex += " ";

src/Es.Serializer/SerializerExtensions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ public static T DeepClone<T>(this ObjectSerializerBase self, T obj) {
8888
/// <param name="obj">The object.</param>
8989
/// <returns>T.</returns>
9090
public static T DeepClone<T>(this T obj) {
91-
var serializer = SerializerFactory.Get("xml");
92-
return (T)serializer.DeepClone(obj);
91+
return (T)XmlSerializer.Instance.DeepClone(obj);
9392
}
9493
}
9594
}

0 commit comments

Comments
 (0)