response method
Parses the json
RPC response and returns the result.
Implementation
JsonRpcResponse<T> response(
final Map<String, dynamic> json,
) {
// Success responses contain a 'result' key.
if (json.containsKey(JsonRpcSuccessResponse.resultKey)) {
final S result = json[JsonRpcSuccessResponse.resultKey];
json[JsonRpcSuccessResponse.resultKey] = decoder(result);
return JsonRpcSuccessResponse<T>.fromJson(json);
}
// Error responses contain an 'error' key.
if (json.containsKey(JsonRpcErrorResponse.errorKey)) {
final Map<String, dynamic> error = json[JsonRpcErrorResponse.errorKey];
json[JsonRpcErrorResponse.errorKey] = JsonRpcException.fromJson(error);
return JsonRpcErrorResponse<T>.fromJson(json);
}
// A valid JSON RPC 2.0 response object must contain a 'result' or 'error', but not both.
// https://www.jsonrpc.org/specification#response_object
return const JsonRpcErrorResponse(
jsonrpc: JsonRpcRequest.version,
error: JsonRpcException('Unexpected response format.'),
);
}