internal static SearchDocumentsResult DeserializeSearchDocumentsResult(JsonElement element) { Optional <long> odataCount = default; Optional <double> searchCoverage = default; Optional <IReadOnlyDictionary <string, IList <FacetResult> > > searchFacets = default; Optional <IReadOnlyDictionary <string, IList <AnswerResult> > > searchAnswers = default; Optional <SearchOptions> searchNextPageParameters = default; IReadOnlyList <SearchResult> value = default; Optional <string> odataNextLink = default; foreach (var property in element.EnumerateObject()) { if (property.NameEquals("@odata.count")) { if (property.Value.ValueKind == JsonValueKind.Null) { property.ThrowNonNullablePropertyIsNull(); continue; } odataCount = property.Value.GetInt64(); continue; } if (property.NameEquals("@search.coverage")) { if (property.Value.ValueKind == JsonValueKind.Null) { property.ThrowNonNullablePropertyIsNull(); continue; } searchCoverage = property.Value.GetDouble(); continue; } if (property.NameEquals("@search.facets")) { if (property.Value.ValueKind == JsonValueKind.Null) { property.ThrowNonNullablePropertyIsNull(); continue; } Dictionary <string, IList <FacetResult> > dictionary = new Dictionary <string, IList <FacetResult> >(); foreach (var property0 in property.Value.EnumerateObject()) { List <FacetResult> array = new List <FacetResult>(); foreach (var item in property0.Value.EnumerateArray()) { array.Add(FacetResult.DeserializeFacetResult(item)); } dictionary.Add(property0.Name, array); } searchFacets = dictionary; continue; } if (property.NameEquals("@search.answers")) { if (property.Value.ValueKind == JsonValueKind.Null) { searchAnswers = null; continue; } Dictionary <string, IList <AnswerResult> > dictionary = new Dictionary <string, IList <AnswerResult> >(); foreach (var property0 in property.Value.EnumerateObject()) { List <AnswerResult> array = new List <AnswerResult>(); foreach (var item in property0.Value.EnumerateArray()) { array.Add(AnswerResult.DeserializeAnswerResult(item)); } dictionary.Add(property0.Name, array); } searchAnswers = dictionary; continue; } if (property.NameEquals("@search.nextPageParameters")) { if (property.Value.ValueKind == JsonValueKind.Null) { property.ThrowNonNullablePropertyIsNull(); continue; } searchNextPageParameters = SearchOptions.DeserializeSearchOptions(property.Value); continue; } if (property.NameEquals("value")) { List <SearchResult> array = new List <SearchResult>(); foreach (var item in property.Value.EnumerateArray()) { array.Add(SearchResult.DeserializeSearchResult(item)); } value = array; continue; } if (property.NameEquals("@odata.nextLink")) { odataNextLink = property.Value.GetString(); continue; } } return(new SearchDocumentsResult(Optional.ToNullable(odataCount), Optional.ToNullable(searchCoverage), Optional.ToDictionary(searchFacets), Optional.ToDictionary(searchAnswers), searchNextPageParameters.Value, value, odataNextLink.Value)); }
#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.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); }