forked from kerryjiang/WebSocket4Net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonWebSocket.JsonNotSet.cs
More file actions
55 lines (46 loc) · 1.72 KB
/
JsonWebSocket.JsonNotSet.cs
File metadata and controls
55 lines (46 loc) · 1.72 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
using System;
using System.IO;
using System.Text;
namespace WebSocket4Net
{
public partial class JsonWebSocket
{
private Func<object, string> m_JsonSerialzier;
private Func<string, Type, object> m_JsonDeserialzier;
/// <summary>
/// Configure json deserializer
/// </summary>
/// <param name="serialzier">the json serialzing method</param>
/// <param name="deserializer">the json deserializing method</param>
public void ConfigJsonSerialzier(Func<object, string> serialzier, Func<string, Type, object> deserializer)
{
m_JsonSerialzier = serialzier;
m_JsonDeserialzier = deserializer;
}
/// <summary>
/// Serializes the object.
/// </summary>
/// <param name="target">The target object is being serialized.</param>
/// <returns></returns>
protected virtual string SerializeObject(object target)
{
var serializer = m_JsonSerialzier;
if (serializer == null)
throw new Exception("Json serialzier is not configured yet.");
return serializer(target);
}
/// <summary>
/// Deserializes the json string to object.
/// </summary>
/// <param name="json">The json string.</param>
/// <param name="type">The type of the target object.</param>
/// <returns></returns>
protected virtual object DeserializeObject(string json, Type type)
{
var deserializer = m_JsonDeserialzier;
if (deserializer == null)
throw new Exception("Json serialzier is not configured yet.");
return deserializer(json, type);
}
}
}