forked from geloczi/synologydotnet-audiostation-wpf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonSerialization.cs
97 lines (87 loc) · 2.72 KB
/
JsonSerialization.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
using System.IO;
using Newtonsoft.Json;
namespace SynCommon.Serialization
{
/// <summary>
/// Serialize/Deserialize objects using JSON format, including the type names.
/// On deserialaization, the correct object type will be returned.
/// When deserializing, the assembly need not to match the assembly used on serialization.
/// </summary>
public static class JsonSerialization
{
private static readonly JsonSerializerSettings _settings = new JsonSerializerSettings()
{
Formatting = Formatting.None,
MissingMemberHandling = MissingMemberHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore,
TypeNameHandling = TypeNameHandling.All,
TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple
};
private static readonly JsonSerializer _serializer = new JsonSerializer()
{
Formatting = _settings.Formatting,
MissingMemberHandling = _settings.MissingMemberHandling,
NullValueHandling = _settings.NullValueHandling,
TypeNameHandling = _settings.TypeNameHandling,
TypeNameAssemblyFormatHandling = _settings.TypeNameAssemblyFormatHandling
};
public static string SerializeToString(object o)
{
return JsonConvert.SerializeObject(o, _settings);
}
public static object DeserializeFromString(string json)
{
return JsonConvert.DeserializeObject(json, _settings);
}
public static void SerializeToStream(object o, Stream stream)
{
using (var sw = new StreamWriter(stream))
using (var jtw = new JsonTextWriter(sw))
{
_serializer.Serialize(jtw, o);
}
}
public static object DeserializeFromStream(Stream stream)
{
using (var sr = new StreamReader(stream))
using (var jtr = new JsonTextReader(sr))
{
return _serializer.Deserialize(jtr);
}
}
public static byte[] SerializeToBytes(object o)
{
using (var ms = new MemoryStream())
{
SerializeToStream(o, ms);
return ms.ToArray();
}
}
public static object DeserializeFromBytes(byte[] buffer)
=> DeserializeFromBytes(buffer, 0, buffer.Length);
public static object DeserializeFromBytes(byte[] buffer, int offset, int count)
{
using (var ms = new MemoryStream(buffer, offset, count, false))
{
return DeserializeFromStream(ms);
}
}
public static T DeserializeFromStream<T>(Stream stream)
{
using (var sr = new StreamReader(stream))
using (var jtr = new JsonTextReader(sr))
{
return _serializer.Deserialize<T>(jtr);
}
}
public static T DeserializeFromBytes<T>(byte[] buffer)
=> DeserializeFromBytes<T>(buffer, 0, buffer.Length);
public static T DeserializeFromBytes<T>(byte[] buffer, int offset, int count)
{
using (var ms = new MemoryStream(buffer, offset, count, false))
{
return DeserializeFromStream<T>(ms);
}
}
}
}