-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathJsonRpcProcessor.cs
More file actions
184 lines (166 loc) · 7.18 KB
/
Copy pathJsonRpcProcessor.cs
File metadata and controls
184 lines (166 loc) · 7.18 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.Threading.Tasks;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace AustinHarris.JsonRpc
{
public static class JsonRpcProcessor
{
private static readonly JsonSerializerOptions DefaultOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
IgnoreNullValues = true,
Converters =
{
new JsonRpcExceptionConverter(),
new JsonResponseConverter(),
new JsonStringEnumConverter()
}
};
public static void Process(JsonRpcStateAsync async, object context = null)
{
Process(Handler.DefaultSessionId(), async, context);
}
public static void Process(string sessionId, JsonRpcStateAsync async, object context = null)
{
Process(sessionId, async.JsonRpc, context)
.ContinueWith(t =>
{
async.Result = t.Result;
async.SetCompleted();
});
}
public static Task<string> Process(string jsonRpc, object context = null, JsonSerializerOptions serializerOptions = null )
{
return Process(Handler.DefaultSessionId(), jsonRpc, context, serializerOptions);
}
public static Task<string> Process(string sessionId, string jsonRpc, object context = null, JsonSerializerOptions serializerOptions = null)
{
return Task<string>.Factory.StartNew((_) =>
{
var tuple = (Tuple<string, string, object>)_;
return ProcessInternal(tuple.Item1, tuple.Item2, tuple.Item3, serializerOptions ?? DefaultOptions);
}, new Tuple<string, string, object>(sessionId, jsonRpc, context));
}
// todo: Work with and return utf8 at this level. We can deal with converting to and from utf8 in the 'Process' overloads
private static string ProcessInternal(string sessionId, string jsonRpc, object jsonRpcContext, JsonSerializerOptions serializerOptions)
{
var handler = Handler.GetSessionHandler(sessionId);
JsonRequest[] batch = null;
try
{
if (isSingleRpc(jsonRpc))
{
var foo = JsonSerializer.Deserialize<JsonRequest>(jsonRpc, serializerOptions);
batch = new[] { foo };
}
else
{
batch = JsonSerializer.Deserialize<JsonRequest[]>(jsonRpc, serializerOptions);
}
}
catch (Exception ex)
{
return JsonSerializer.Serialize(new JsonResponse
{
Error = handler.ProcessParseException(jsonRpc, JsonRpcException.WithData( JsonRpcException.Ex_32700,ex))
}, serializerOptions);
}
if (batch.Length == 0)
{
return JsonSerializer.Serialize(new JsonResponse
{
Error = handler.ProcessParseException(jsonRpc, JsonRpcException.Ex_3200)
}, serializerOptions);
}
var singleBatch = batch.Length == 1;
StringBuilder sbResult = null;
for (var i = 0; i < batch.Length; i++)
{
var jsonRequest = batch[i];
var jsonResponse = new JsonResponse();
if (jsonRequest == null)
{
jsonResponse.Error = handler.ProcessParseException(jsonRpc,
JsonRpcException.WithData(JsonRpcException.Ex_32700,
"Invalid JSON was received by the server. An error occurred on the server while parsing the JSON text."));
}
else if (jsonRequest.Method == null)
{
jsonResponse.Error = handler.ProcessParseException(jsonRpc,
JsonRpcException.WithData(JsonRpcException.Ex_32600, "Missing property 'method'"));
}
else if (!isSimpleValueType(jsonRequest.Id))
{
jsonResponse.Error = handler.ProcessParseException(jsonRpc,
JsonRpcException.WithData(JsonRpcException.Ex_32600,"Id property must be either null or string or integer."));
}
else
{
jsonResponse.Id = jsonRequest.Id;
var data = handler.Handle(jsonRequest, jsonRpcContext);
if (data == null) continue;
jsonResponse.Jsonrpc = data.Jsonrpc;
jsonResponse.Error = data.Error;
jsonResponse.Result = data.Result;
}
if (jsonResponse.Result == null && jsonResponse.Error == null)
{
// Per json rpc 2.0 spec
// result : This member is REQUIRED on success.
// This member MUST NOT exist if there was an error invoking the method.
// Either the result member or error member MUST be included, but both members MUST NOT be included.
jsonResponse.Result = null;
}
// special case optimization for single Item batch
if (singleBatch && (jsonResponse.Id.ValueKind != JsonValueKind.Null || jsonResponse.Error != null))
{
return JsonSerializer.Serialize(jsonResponse, serializerOptions);
}
else if (jsonResponse.Id.ValueKind == JsonValueKind.Null && jsonResponse.Error == null)
{
// do nothing
sbResult = new StringBuilder(0);
}
else
{
// write out the response
if (i == 0)
{
sbResult = new StringBuilder("[");
}
sbResult.Append(JsonSerializer.Serialize(jsonResponse, serializerOptions));
if (i < batch.Length - 1)
{
sbResult.Append(',');
}
else if (i == batch.Length - 1)
{
sbResult.Append(']');
}
}
}
return sbResult.ToString();
}
private static bool isSingleRpc(string json)
{
for (int i = 0; i < json.Length; i++)
{
if (json[i] == '{') return true;
else if (json[i] == '[') return false;
}
return true;
}
private static bool isSimpleValueType(JsonElement property)
{
JsonElement p = (JsonElement)property;
return p.ValueKind == JsonValueKind.Number
|| p.ValueKind == JsonValueKind.String
|| p.ValueKind == JsonValueKind.True
|| p.ValueKind == JsonValueKind.False
|| p.ValueKind == JsonValueKind.Null;
return false;
}
}
}