serialize<T> method

T serialize<T>(
  1. Response response,
  2. String path, {
  3. T onSerializedCallback(
    1. Object json
    )?,
})

Convert Json response to T and throws ClientException for client error response.

onSerializedCallback returns the type you want.

for the Payment listAll method returns different type of json.

Implementation

T serialize<T>(
  Response response,
  String path, {
  T Function(Object json)? onSerializedCallback,
}) {
  final json = jsonDecode(response.body);
  if (json?['errors'] != null) {
    final jsonErrors =
        List<Map<String, dynamic>>.from(json['errors'] as List).toList();
    final errors = jsonErrors.map(PaymongoErrorCodes.fromMap).toList();
    throw PaymongoError(
      "Error ${response.statusCode}",
      paymongoErrors: errors,
    );
  }
  return onSerializedCallback?.call(json) ?? json?['data'] as T;
}