Exemple #1
0
 private static async Task <T> Deserialize <T>(string json)
 {
     using var ms = new MemoryStream();
     ms.Write(Encoding.UTF8.GetBytes(json));
     ms.Seek(0, SeekOrigin.Begin);
     return(await JsonSerialization.DeserializeAsync <T>(ms, CancellationToken.None));
 }
            protected virtual async Task <T?> DeserializeAsync <T>(CancellationToken cancellationToken)
            {
                if (!IsJsonResponse(ResponseMessage))
                {
                    throw new InvalidOperationException($"Content type must be application/json but is {ResponseMessage.Content.Headers.ContentType?.MediaType}");
                }

                var s = await ResponseMessage.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);

                try
                {
                    return(await JsonSerialization.DeserializeAsync <T>(s, cancellationToken).ConfigureAwait(false));
                }
                finally
                {
#if NET5_0
                    await s.DisposeAsync().ConfigureAwait(false);
#elif NETSTANDARD2_0
                    s.Dispose();
#else
#error Platform not supported
#endif
                }
            }