Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
JsonRpcResult.cs
3
5{
9 public class JsonRpcResult
10 {
11 private readonly JsonRpcVersion version;
12 private readonly object? result;
13 private readonly JsonRpcError? error;
14 private readonly long? id;
15 private readonly bool hasResult;
16 private readonly bool hasError;
17
22 public JsonRpcResult(Dictionary<string,object> JsonResponse)
23 {
24 foreach (KeyValuePair<string, object> P in JsonResponse)
25 {
26 switch (P.Key)
27 {
28 case "jsonrpc":
29 if (P.Value is string s)
30 {
31 if (s == "2.0")
32 this.version = JsonRpcVersion.JsonRpcV2;
33 else
34 throw new JsonRpcException("Unexpected response received: Unsupported JSON-RPC version: " + s);
35 }
36 else
37 throw new JsonRpcException("Unexpected response received: JSON-RPC version property is not a string.");
38 break;
39
40 case "result":
41 this.result = P.Value;
42 this.hasResult = true;
43 break;
44
45 case "error": // Processed in DoNotification
46
47 if (P.Value is Dictionary<string, object> Error &&
48 Error.TryGetValue("code", out object Obj) &&
49 Obj is int ErrorCode &&
50 Error.TryGetValue("message", out Obj) &&
51 Obj is string ErrorMessage)
52 {
53 this.error = JsonRpcClient.GetError(ErrorCode, ErrorMessage);
54 this.hasError = true;
55 }
56 else
57 throw new JsonRpcException("Unexpected error response received.");
58 break;
59
60 case "id":
61 if (P.Value is long l)
62 this.id = l;
63 else if (P.Value is int i)
64 this.id = i;
65 else if (P.Value is string s2 && long.TryParse(s2, out l))
66 this.id = l;
67 else
68 throw new JsonRpcException("Unexpected response received: Id property is not a valid integer.");
69 break;
70
71 default:
72 throw new JsonRpcException("Unexpected response received: Unknown property: " + P.Key);
73 }
74 }
75 }
76
80 public JsonRpcVersion Version => this.version;
81
85 public object? Result => this.result;
86
90 public JsonRpcError? Error => this.error;
91
95 public long? Id => this.id;
96
100 public bool HasResult => this.hasResult;
101
105 public bool HasError => this.hasError;
106
107
108 }
109}
Base class for JSON-RPC error responses.
Definition: JsonRpcError.cs:7
Information about a JSON-RPC result.
bool HasError
If response has an error in Error, this property is true.
object? Result
Result of operation, if HasResult is true.
JsonRpcError? Error
Error of operation, if HasError is true.
long? Id
ID matching the request, if any.
bool HasResult
If response has a result in Result, this property is true.
JsonRpcVersion Version
JSON-RPC version.
JsonRpcResult(Dictionary< string, object > JsonResponse)
Information about a JSON-RPC result.
JsonRpcVersion
JSON-RPC version