Beispiel #1
0
        /// <summary>
        /// Read the @search.facets property value.
        /// </summary>
        /// <param name="reader">The JSON reader.</param>
        /// <param name="results">The SearchResults to add the facets to.</param>
        /// <param name="options">JSON serializer options.</param>
        private static void ReadFacets(
            ref Utf8JsonReader reader,
            SearchResults <T> results,
            JsonSerializerOptions options)
        {
            Debug.Assert(results != null);

            // Facets are optional, so short circuit if nothing is found
            if (reader.TokenType == JsonTokenType.Null)
            {
                return;
            }

            results.Facets = new Dictionary <string, IList <FacetResult> >();
            reader.Expects(JsonTokenType.StartObject);
            while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
            {
                // Get the name of the facet
                string facetName = reader.ExpectsPropertyName();

                // Get the values of the facet
                List <FacetResult> facets = new List <FacetResult>();
                reader.Expects(JsonTokenType.StartArray);
                while (reader.Read() && reader.TokenType != JsonTokenType.EndArray)
                {
                    FacetResult facet = ReadFacetResult(ref reader, options);
                    facets.Add(facet);
                }

                // Add the facet to the results
                results.Facets[facetName] = facets;
            }
        }
Beispiel #2
0
        internal static FacetResult DeserializeFacetResult(JsonElement element)
        {
            FacetResult result = new FacetResult();

            foreach (var property in element.EnumerateObject())
            {
                if (property.NameEquals("count"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    result.Count = property.Value.GetInt64();
                    continue;
                }
                result.Add(property.Name, property.Value.GetObject());
            }
            return(result);
        }
Beispiel #3
0
        /// <summary>
        /// Read the facet result value.
        /// </summary>
        /// <param name="reader">JSON reader.</param>
        /// <param name="options">Serializer options.</param>
        /// <returns>The facet result.</returns>
        private static FacetResult ReadFacetResult(
            ref Utf8JsonReader reader,
            JsonSerializerOptions options)
        {
            FacetResult facet = new FacetResult();

            reader.Expects(JsonTokenType.StartObject);
            while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
            {
                // Get the name of the facet property
                string facetName = reader.ExpectsPropertyName();
                if (facetName == Constants.CountKey)
                {
                    facet.Count = reader.ExpectsNullableLong();
                }
                else
                {
                    object value = reader.ReadObject(options);
                    facet[facetName] = value;
                }
            }
            return(facet);
        }
        internal static SearchDocumentsResult DeserializeSearchDocumentsResult(JsonElement element)
        {
            SearchDocumentsResult result = new SearchDocumentsResult();

            foreach (var property in element.EnumerateObject())
            {
                if (property.NameEquals("@odata.count"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    result.Count = property.Value.GetInt64();
                    continue;
                }
                if (property.NameEquals("@search.coverage"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    result.Coverage = property.Value.GetDouble();
                    continue;
                }
                if (property.NameEquals("@search.facets"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    result.Facets = new Dictionary <string, ICollection <FacetResult> >();
                    foreach (var property0 in property.Value.EnumerateObject())
                    {
                        ICollection <FacetResult> value = new List <FacetResult>();
                        foreach (var item in property0.Value.EnumerateArray())
                        {
                            value.Add(FacetResult.DeserializeFacetResult(item));
                        }
                        result.Facets.Add(property0.Name, value);
                    }
                    continue;
                }
                if (property.NameEquals("@search.nextPageParameters"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    result.NextPageParameters = SearchRequest.DeserializeSearchRequest(property.Value);
                    continue;
                }
                if (property.NameEquals("value"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    result.Results = new List <SearchResult>();
                    foreach (var item in property.Value.EnumerateArray())
                    {
                        result.Results.Add(SearchResult.DeserializeSearchResult(item));
                    }
                    continue;
                }
                if (property.NameEquals("@odata.nextLink"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    result.NextLink = property.Value.GetString();
                    continue;
                }
            }
            return(result);
        }