/// <summary>
        /// DEVNOTE: Need to refactor to use CosmosJsonSerializer
        /// </summary>
        public static FeedResponse <T> ConvertCosmosElementFeed <T>(
            FeedResponse <CosmosElement> dynamicFeed,
            ResourceType resourceType,
            CosmosJsonSerializer jsonSerializer)
        {
            if (dynamicFeed.Count == 0)
            {
                return(new FeedResponse <T>(
                           new List <T>(),
                           dynamicFeed.Count,
                           dynamicFeed.Headers,
                           dynamicFeed.UseETagAsContinuation,
                           dynamicFeed.QueryMetrics,
                           dynamicFeed.RequestStatistics,
                           dynamicFeed.DisallowContinuationTokenMessage,
                           dynamicFeed.ResponseLengthBytes));
            }

            IJsonWriter jsonWriter = JsonWriter.Create(JsonSerializationFormat.Text);

            jsonWriter.WriteArrayStart();

            foreach (CosmosElement cosmosElement in dynamicFeed)
            {
                cosmosElement.WriteTo(jsonWriter);
            }

            jsonWriter.WriteArrayEnd();
            MemoryStream    stream = new MemoryStream(jsonWriter.GetResult());
            IEnumerable <T> typedResults;

            // If the resource type is an offer and the requested type is either a Offer or OfferV2 or dynamic
            // create a OfferV2 object and cast it to T. This is a temporary fix until offers is moved to v3 API.
            if (resourceType == ResourceType.Offer &&
                (typeof(T).IsSubclassOf(typeof(Resource)) || typeof(T) == typeof(object)))
            {
                typedResults = jsonSerializer.FromStream <List <OfferV2> >(stream).Cast <T>();
            }
            else
            {
                typedResults = jsonSerializer.FromStream <List <T> >(stream);
            }

            return(new FeedResponse <T>(
                       typedResults,
                       dynamicFeed.Count,
                       dynamicFeed.Headers,
                       dynamicFeed.UseETagAsContinuation,
                       dynamicFeed.QueryMetrics,
                       dynamicFeed.RequestStatistics,
                       dynamicFeed.DisallowContinuationTokenMessage,
                       dynamicFeed.ResponseLengthBytes));
        }
        internal static ReadFeedResponse <TInput> CreateResponse <TInput>(
            CosmosResponseMessageHeaders responseMessageHeaders,
            Stream stream,
            CosmosJsonSerializer jsonSerializer,
            bool hasMoreResults)
        {
            using (stream)
            {
                CosmosFeedResponseUtil <TInput> response         = jsonSerializer.FromStream <CosmosFeedResponseUtil <TInput> >(stream);
                IEnumerable <TInput>            resources        = response.Data;
                ReadFeedResponse <TInput>       readFeedResponse = new ReadFeedResponse <TInput>(
                    resource: resources,
                    responseMessageHeaders: responseMessageHeaders,
                    hasMoreResults: hasMoreResults);

                return(readFeedResponse);
            }
        }
        internal T ToObjectInternal <T>(CosmosResponseMessage cosmosResponseMessage, CosmosJsonSerializer jsonSerializer)
        {
            // Not finding something is part of a normal work-flow and should not be an exception.
            // This prevents the unnecessary overhead of an exception
            if (cosmosResponseMessage.StatusCode == HttpStatusCode.NotFound)
            {
                return(default(T));
            }

            //Throw the exception
            cosmosResponseMessage.EnsureSuccessStatusCode();

            if (cosmosResponseMessage.Content == null)
            {
                return(default(T));
            }

            return(jsonSerializer.FromStream <T>(cosmosResponseMessage.Content));
        }
Exemplo n.º 4
0
 private void InitializeResource(
     Stream stream,
     CosmosJsonSerializer jsonSerializer)
 {
     this.Resources = jsonSerializer.FromStream <CosmosFeedResponse <T> >(stream).Data;
 }