-
Notifications
You must be signed in to change notification settings - Fork 8
/
DataContractJsonSer.cs
67 lines (57 loc) · 1.78 KB
/
DataContractJsonSer.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
///
/// System.Runtime.Serialization.dll
///
using System;
using System.IO;
using System.Runtime.Serialization.Json;
namespace GLD.SerializerBenchmark.Serializers
{
internal class DataContractJsonSer : SerDeser
{
private DataContractJsonSerializer _serializer;
private void Initialize()
{
if (!JustInitialized) return;
_serializer = new DataContractJsonSerializer(_primaryType, _secondaryTypes);
JustInitialized = false;
}
#region ISerDeser Members
public override string Name
{
get { return "MS DataContract Json"; }
}
public override string Serialize(object serializable)
{
Initialize();
using (var stream = new MemoryStream())
{
_serializer.WriteObject(stream, serializable);
stream.Flush();
stream.Position = 0;
return Convert.ToBase64String(stream.ToArray());
}
}
public override object Deserialize(string serialized)
{
Initialize();
var b = Convert.FromBase64String(serialized);
using (var stream = new MemoryStream(b))
{
stream.Seek(0, SeekOrigin.Begin);
return _serializer.ReadObject(stream);
}
}
public override void Serialize(object serializable, Stream outputStream)
{
Initialize();
_serializer.WriteObject(outputStream, serializable);
}
public override object Deserialize(Stream inputStream)
{
Initialize();
inputStream.Seek(0, SeekOrigin.Begin);
return _serializer.ReadObject(inputStream);
}
#endregion
}
}