#pragma warning disable CS1572 // Not all parameters will be used depending on feature flags
        /// <summary>
        /// Deserialize the SearchResults.
        /// </summary>
        /// <param name="json">A JSON stream.</param>
        /// <param name="serializer">
        /// Optional serializer that can be used to customize the serialization
        /// of strongly typed models.
        /// </param>
        /// <param name="async">Whether to execute sync or async.</param>
        /// <param name="cancellationToken">
        /// Optional <see cref="CancellationToken"/> to propagate notifications
        /// that the operation should be canceled.
        /// </param>
        /// <returns>Deserialized SearchResults.</returns>
        internal static async Task <SearchResults <T> > DeserializeAsync(
            Stream json,
            ObjectSerializer serializer,
            bool async,
            CancellationToken cancellationToken)
        #pragma warning restore CS1572
        {
            // Parse the JSON
            using JsonDocument doc = async ?
                                     await JsonDocument.ParseAsync(json, cancellationToken : cancellationToken).ConfigureAwait(false) :
                                     JsonDocument.Parse(json);

            JsonSerializerOptions defaultSerializerOptions = JsonSerialization.SerializerOptions;

            SearchResults <T> results = new SearchResults <T>();

            foreach (JsonProperty prop in doc.RootElement.EnumerateObject())
            {
                if (prop.NameEquals(Constants.ODataCountKeyJson.EncodedUtf8Bytes) &&
                    prop.Value.ValueKind != JsonValueKind.Null)
                {
                    results.TotalCount = prop.Value.GetInt64();
                }
                else if (prop.NameEquals(Constants.SearchCoverageKeyJson.EncodedUtf8Bytes) &&
                         prop.Value.ValueKind != JsonValueKind.Null)
                {
                    results.Coverage = prop.Value.GetDouble();
                }
                else if (prop.NameEquals(Constants.SearchFacetsKeyJson.EncodedUtf8Bytes))
                {
                    results.Facets = new Dictionary <string, IList <FacetResult> >();
                    foreach (JsonProperty facetObject in prop.Value.EnumerateObject())
                    {
                        // Get the values of the facet
                        List <FacetResult> facets = new List <FacetResult>();
                        foreach (JsonElement facetValue in facetObject.Value.EnumerateArray())
                        {
                            Dictionary <string, object> facetValues = new Dictionary <string, object>();
                            long?facetCount = null;
                            foreach (JsonProperty facetProperty in facetValue.EnumerateObject())
                            {
                                if (facetProperty.NameEquals(Constants.CountKeyJson.EncodedUtf8Bytes))
                                {
                                    if (facetProperty.Value.ValueKind != JsonValueKind.Null)
                                    {
                                        facetCount = facetProperty.Value.GetInt64();
                                    }
                                }
                                else
                                {
                                    object value = facetProperty.Value.GetSearchObject();
                                    facetValues[facetProperty.Name] = value;
                                }
                            }
                            facets.Add(new FacetResult(facetCount, facetValues));
                        }
                        // Add the facet to the results
                        results.Facets[facetObject.Name] = facets;
                    }
                }
                else if (prop.NameEquals(Constants.ODataNextLinkKeyJson.EncodedUtf8Bytes))
                {
                    results.NextUri = new Uri(prop.Value.GetString());
                }
                else if (prop.NameEquals(Constants.SearchNextPageKeyJson.EncodedUtf8Bytes))
                {
                    results.NextOptions = SearchOptions.DeserializeSearchOptions(prop.Value);
                }
                else if (prop.NameEquals(Constants.SearchAnswersKeyJson.EncodedUtf8Bytes) &&
                         prop.Value.ValueKind != JsonValueKind.Null)
                {
                    results.Answers = new List <AnswerResult>();
                    foreach (JsonElement answerValue in prop.Value.EnumerateArray())
                    {
                        results.Answers.Add(AnswerResult.DeserializeAnswerResult(answerValue));
                    }
                }
                else if (prop.NameEquals(Constants.SearchCaptionsKeyJson.EncodedUtf8Bytes) &&
                         prop.Value.ValueKind != JsonValueKind.Null)
                {
                    results.Captions = new List <CaptionResult>();
                    foreach (JsonElement captionValue in prop.Value.EnumerateArray())
                    {
                        results.Captions.Add(CaptionResult.DeserializeCaptionResult(captionValue));
                    }
                }
                else if (prop.NameEquals(Constants.ValueKeyJson.EncodedUtf8Bytes))
                {
                    foreach (JsonElement element in prop.Value.EnumerateArray())
                    {
                        SearchResult <T> result = await SearchResult <T> .DeserializeAsync(
                            element,
                            serializer,
                            defaultSerializerOptions,
                            async,
                            cancellationToken)
                                                  .ConfigureAwait(false);

                        results.Values.Add(result);
                    }
                }
            }
            return(results);
        }
        internal static SearchResult DeserializeSearchResult(JsonElement element)
        {
            double             searchScore         = default;
            Optional <double?> searchRerankerScore = default;
            Optional <IReadOnlyDictionary <string, IList <string> > >        searchHighlights = default;
            Optional <IReadOnlyDictionary <string, IList <CaptionResult> > > searchCaptions   = default;
            IReadOnlyDictionary <string, object> additionalProperties           = default;
            Dictionary <string, object>          additionalPropertiesDictionary = new Dictionary <string, object>();

            foreach (var property in element.EnumerateObject())
            {
                if (property.NameEquals("@search.score"))
                {
                    searchScore = property.Value.GetDouble();
                    continue;
                }
                if (property.NameEquals("@search.rerankerScore"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        searchRerankerScore = null;
                        continue;
                    }
                    searchRerankerScore = property.Value.GetDouble();
                    continue;
                }
                if (property.NameEquals("@search.highlights"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        property.ThrowNonNullablePropertyIsNull();
                        continue;
                    }
                    Dictionary <string, IList <string> > dictionary = new Dictionary <string, IList <string> >();
                    foreach (var property0 in property.Value.EnumerateObject())
                    {
                        List <string> array = new List <string>();
                        foreach (var item in property0.Value.EnumerateArray())
                        {
                            array.Add(item.GetString());
                        }
                        dictionary.Add(property0.Name, array);
                    }
                    searchHighlights = dictionary;
                    continue;
                }
                if (property.NameEquals("@search.captions"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        searchCaptions = null;
                        continue;
                    }
                    Dictionary <string, IList <CaptionResult> > dictionary = new Dictionary <string, IList <CaptionResult> >();
                    foreach (var property0 in property.Value.EnumerateObject())
                    {
                        List <CaptionResult> array = new List <CaptionResult>();
                        foreach (var item in property0.Value.EnumerateArray())
                        {
                            array.Add(CaptionResult.DeserializeCaptionResult(item));
                        }
                        dictionary.Add(property0.Name, array);
                    }
                    searchCaptions = dictionary;
                    continue;
                }
                additionalPropertiesDictionary.Add(property.Name, property.Value.GetObject());
            }
            additionalProperties = additionalPropertiesDictionary;
            return(new SearchResult(searchScore, Optional.ToNullable(searchRerankerScore), Optional.ToDictionary(searchHighlights), Optional.ToDictionary(searchCaptions), additionalProperties));
        }