/// <summary> Initializes a new instance of SearchResultsPage. </summary>
 /// <typeparam name="T">
 /// The .NET type that maps to the index schema. Instances of this type can
 /// be retrieved as documents from the index.
 /// </typeparam>
 /// <param name="results">The search results for this page.</param>
 /// <returns>A new SearchResultsPage instance for mocking.</returns>
 public static SearchResultsPage <T> SearchResultsPage <T>(
     SearchResults <T> results) =>
 new SearchResultsPage <T>(results);
 public SearchPageable(SearchResults <T> results)
 {
     Debug.Assert(results != null);
     _results = results;
 }
        #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,
#if EXPERIMENTAL_SERIALIZER
            ObjectSerializer serializer,
#endif
            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.ValueKeyJson.EncodedUtf8Bytes))
                {
                    foreach (JsonElement element in prop.Value.EnumerateArray())
                    {
                        SearchResult <T> result = await SearchResult <T> .DeserializeAsync(
                            element,
#if EXPERIMENTAL_SERIALIZER
                            serializer,
#endif
                            defaultSerializerOptions,
                            async,
                            cancellationToken)
                                                  .ConfigureAwait(false);

                        results.Values.Add(result);
                    }
                }
            }
            return(results);
        }
 internal SearchResultsPage(SearchResults <T> results)
 {
     Debug.Assert(results != null);
     _results = results;
 }
Exemple #5
0
 /// <summary>
 /// Serializing SearchResults isn't supported as it's an output only
 /// model type.  This always fails.
 /// </summary>
 /// <param name="writer">The JSON writer.</param>
 /// <param name="value">The search result.</param>
 /// <param name="options">Serialization options.</param>
 public override void Write(Utf8JsonWriter writer, SearchResults <T> value, JsonSerializerOptions options) =>
 throw new NotSupportedException($"{nameof(SearchResults<T>)} cannot be serialized to JSON.");