parse<T, U> static method

JsonRpcResponse<T> parse<T, U>(
  1. Map<String, dynamic> json,
  2. JsonRpcParser<T, U> parse
)

Creates an instance of this class from the constructor parameters defined in the json object.

A success response will set JsonRpcResponse.result to the return value of parse(json['result']).

JsonRpcResponse.parse({ '<parameter>': <value> }, (U) => T);

Implementation

static JsonRpcResponse<T> parse<T, U>(
  final Map<String, dynamic> json,
  final JsonRpcParser<T, U> parse,
) {

  /// Success responses contain a 'result' key.
  if (json.containsKey(resultKey)) {
    json[resultKey] = parse.call(json[resultKey]);
    return JsonRpcResponse<T>.fromJson(json);
  }

  /// Error responses contain an 'error' key.
  if (json.containsKey(errorKey)) {
    json[errorKey] = JsonRpcException.fromJson(json[errorKey]);
    return JsonRpcResponse<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 JsonRpcResponse<T>(jsonrpc: '', error: const _JsonRpcResponseObjectException());
}