-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleJSONRequest.cs
95 lines (90 loc) · 3.63 KB
/
SimpleJSONRequest.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
#if NETSTANDARD1_3_OR_GREATER
using System;
#endif
using System;
using System.Diagnostics.CodeAnalysis;
using Newtonsoft.Json.Linq;
using System.Runtime.Serialization;
using Newtonsoft.Json;
namespace ConnectorLib.JSON;
#if NETSTANDARD1_3_OR_GREATER
[Serializable]
#endif
[SuppressMessage("ReSharper", "InconsistentNaming")]
public class SimpleJSONRequest : SimpleJSONMessage
{
public uint id = NextID;
public RequestType type;
/// <inheritdoc/>
/// <remarks>
/// This simply returns the value of <see cref="id">id</see>, as required by <see cref="SimpleJSONMessage"/>.
/// </remarks>
[JsonIgnore] public override uint ID => id;
[JsonIgnore] public override bool IsKeepAlive => type == RequestType.KeepAlive;
/// <summary>
/// Parses a response object from a serialized string.
/// </summary>
/// <param name="json">The JSON string containing the response message.</param>
/// <param name="request">A <see cref="SimpleJSONRequest"/> object corresponding to the supplied JSON.</param>
/// <returns>True if the operation succeeded, false otherwise.</returns>
#if NETSTANDARD2_1_OR_GREATER
public static bool TryParse(string json, [MaybeNullWhen(false)] out SimpleJSONRequest request)
#else
public static bool TryParse(string json, out SimpleJSONRequest? request)
#endif
=> TryParse(JObject.Parse(json), out request);
/// <summary>
/// Parses a response object from a serialized string.
/// </summary>
/// <param name="j">The JSON object containing the response message.</param>
/// <param name="request">A <see cref="SimpleJSONRequest"/> object corresponding to the supplied JSON.</param>
/// <returns>True if the operation succeeded, false otherwise.</returns>
#if NETSTANDARD2_1_OR_GREATER
public static bool TryParse(JObject j, [MaybeNullWhen(false)] out SimpleJSONRequest request)
#else
public static bool TryParse(JObject j, out SimpleJSONRequest? request)
#endif
{
try
{
JToken? typeToken = j.GetValue("type");
RequestType type;
if (typeToken != null) type = (RequestType)CamelCaseStringEnumConverter.ReadJToken(typeToken, typeof(RequestType));
else type = default;
switch (type)
{
case RequestType.EffectTest:
case RequestType.EffectStart:
request = j.ToObject<EffectRequest>(JSON_SERIALIZER)!;
return true;
case RequestType.EffectStop:
request = j.ToObject<EffectRequest>(JSON_SERIALIZER)!;
return true;
case RequestType.DataRequest:
request = j.ToObject<DataRequest>(JSON_SERIALIZER)!;
return true;
case RequestType.RpcResponse:
request = j.ToObject<RpcResponse>(JSON_SERIALIZER)!;
return true;
case RequestType.PlayerInfo:
request = j.ToObject<PlayerInfo>(JSON_SERIALIZER)!;
return true;
case RequestType.Login:
request = j.ToObject<MessageRequest>(JSON_SERIALIZER)!;
return true;
case RequestType.GameUpdate:
request = j.ToObject<EmptyRequest>(JSON_SERIALIZER)!;
return true;
case RequestType.KeepAlive:
request = j.ToObject<EmptyRequest>(JSON_SERIALIZER)!;
return true;
default:
goto fail;
}
}
catch { /**/ }
fail:
request = null;
return false;
}
}