public static Task <IEnumerable <TObjectType> > DeserializeCollectionAsync <TObjectType>(string json, CancellationToken cancellationToken = default)
        {
            if (json == null)
            {
                throw new ArgumentNullException(nameof(json), "json string must not be null");
            }

            if (json.Length == 0)
            {
                throw new ArgumentException(nameof(json), "json string must not be empty");
            }

            IArrayJsonReader <TObjectType> arrayJsonReader = JsonFactoryContainer.CreateArrayReader <TObjectType>();

            return(arrayJsonReader.ReadArrayAsync(json, cancellationToken));
        }
Esempio n. 2
0
        private async Task <TraktPagedResponse <TResponseContentType> > QueryPagedListAsync <TResponseContentType>(ExtendedHttpRequestMessage requestMessage, CancellationToken cancellationToken = default)
        {
            HttpResponseMessage responseMessage = null;

            try
            {
                responseMessage = await ExecuteRequestAsync(requestMessage, false, cancellationToken).ConfigureAwait(false);

                DebugAsserter.AssertResponseMessageIsNotNull(responseMessage);
                DebugAsserter.AssertHttpResponseCodeIsNotExpected(responseMessage.StatusCode, HttpStatusCode.NoContent, DebugAsserter.PAGED_LIST_RESPONSE_PRECONDITION_INVALID_STATUS_CODE);
                Stream responseContentStream = await ResponseMessageHelper.GetResponseContentStreamAsync(responseMessage).ConfigureAwait(false);

                DebugAsserter.AssertResponseContentStreamIsNotNull(responseContentStream);
                IArrayJsonReader <TResponseContentType> arrayJsonReader = JsonFactoryContainer.CreateArrayReader <TResponseContentType>();
                DebugAsserter.AssertArrayJsonReaderIsNotNull(arrayJsonReader);
                IEnumerable <TResponseContentType> contentObject = await arrayJsonReader.ReadArrayAsync(responseContentStream, cancellationToken).ConfigureAwait(false);

                var response = new TraktPagedResponse <TResponseContentType>
                {
                    IsSuccess = true,
                    HasValue  = contentObject != null,
                    Value     = contentObject
                };

                if (responseMessage.Headers != null)
                {
                    ResponseHeaderParser.ParsePagedResponseHeaderValues(response, responseMessage.Headers);
                }

                return(response);
            }
            catch (Exception ex)
            {
                if (_client.Configuration.ThrowResponseExceptions)
                {
                    throw;
                }

                return(new TraktPagedResponse <TResponseContentType> {
                    IsSuccess = false, Exception = ex
                });
            }
            finally
            {
                responseMessage?.Dispose();
            }
        }