deocde<S, T> static method

JsonRpcResponse<T> deocde<S, T>(
  1. Map<String, dynamic> json,
  2. T decoder(
    1. S value
    )
)

Parses the json RPC response and returns the result.

Implementation

static JsonRpcResponse<T> deocde<S, T>(
  final Map<String, dynamic> json,
  final T Function(S value) decoder,
) {
  // 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.'),
  );
}