private static async Task <T> ReadFromJsonAsyncCore <T>(HttpContent content, Encoding?sourceEncoding, JsonSerializerOptions?options, CancellationToken cancellationToken)
        {
            Stream contentStream = await content.ReadAsStreamAsync().ConfigureAwait(false);

            // Wrap content stream into a transcoding stream that buffers the data transcoded from the sourceEncoding to utf-8.
            if (sourceEncoding != null && sourceEncoding != Encoding.UTF8)
            {
                contentStream = new TranscodingReadStream(contentStream, sourceEncoding);
            }

            using (contentStream)
            {
                return(await JsonSerializer.DeserializeAsync <T>(contentStream, options, cancellationToken).ConfigureAwait(false));
            }
        }
示例#2
0
        private static async Task <object?> ReadFromJsonAsyncCore(HttpContent content, Type type, Encoding?sourceEncoding, JsonSerializerOptions?options, CancellationToken cancellationToken)
        {
            Stream contentStream = await content.ReadAsStreamAsync().ConfigureAwait(false);

            // Wrap content stream into a transcoding stream that buffers the data transcoded from the sourceEncoding to utf-8.
            if (sourceEncoding != null && sourceEncoding != Encoding.UTF8)
            {
#if NETCOREAPP
                contentStream = Encoding.CreateTranscodingStream(contentStream, innerStreamEncoding: sourceEncoding, outerStreamEncoding: Encoding.UTF8);
#else
                contentStream = new TranscodingReadStream(contentStream, sourceEncoding);
#endif
            }

            using (contentStream)
            {
                return(await JsonSerializer.DeserializeAsync(contentStream, type, options ?? JsonContent.s_defaultSerializerOptions, cancellationToken).ConfigureAwait(false));
            }
        }