/// <summary> /// Parse the SearchResults and its search results. /// </summary> /// <param name="reader">The JSON reader.</param> /// <param name="typeToConvert">The type to parse into.</param> /// <param name="options">Serialization options.</param> /// <returns>The deserialized search results.</returns> public override SearchResults <T> Read( ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { Debug.Assert(typeToConvert != null); Debug.Assert(typeToConvert.IsAssignableFrom(typeof(SearchResults <T>))); Debug.Assert(options != null); SearchResults <T> results = new SearchResults <T>(); reader.Expects(JsonTokenType.StartObject); while (reader.Read() && reader.TokenType != JsonTokenType.EndObject) { switch (reader.ExpectsPropertyName()) { case Constants.ODataCountKey: results.TotalCount = reader.ExpectsNullableLong(); break; case Constants.SearchCoverageKey: results.Coverage = reader.ExpectsNullableDouble(); break; case Constants.SearchFacetsKey: ReadFacets(ref reader, results, options); break; case Constants.ODataNextLinkKey: results.NextUri = new Uri(reader.ExpectsString()); break; case Constants.SearchNextPageKey: results.NextOptions = ReadNextPageOptions(ref reader); break; case Constants.ValueKey: reader.Expects(JsonTokenType.StartArray); while (reader.Read() && reader.TokenType != JsonTokenType.EndArray) { SearchResult <T> result = _resultConverter.Read(ref reader, _resultType, options); results.Values.Add(result); } break; default: // Ignore other properties (including OData context, etc.) reader.Skip(); break; } } return(results); }
/// <summary> /// Parse a SearchResult and its model. /// </summary> /// <param name="reader">The JSON reader.</param> /// <param name="typeToConvert">The type to parse into.</param> /// <param name="options">Serialization options.</param> /// <returns>The deserialized search result.</returns> public override SearchResult <T> Read( ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { Debug.Assert(options != null); SearchResult <T> result = new SearchResult <T>(); // Clone the reader so we can get the search text property without // advancing the reader over any properties needed to deserialize // the user's model type. Utf8JsonReader clone = reader; // Keep track of the properties we've found so we can stop reading // through the cloned object bool parsedScore = false; bool parsedHighlights = false; clone.Expects(JsonTokenType.StartObject); while ( (!parsedScore || !parsedHighlights) && clone.Read() && clone.TokenType != JsonTokenType.EndObject) { string name = clone.ExpectsPropertyName(); if (name == Constants.SearchScoreKey) { parsedScore = true; result.Score = clone.ExpectsNullableDouble(); } else if (name == Constants.SearchHighlightsKey) { parsedHighlights = true; ReadHighlights(ref clone, result); } else { // Skip the rest of the next property's value clone.Skip(); } } // Deserialize the model T document = JsonSerializer.Deserialize <T>(ref reader, options); result.Document = document; return(result); }
/// <summary> /// Parse the SuggestResults and its suggestions. /// </summary> /// <param name="reader">The JSON reader.</param> /// <param name="typeToConvert">The type to parse into.</param> /// <param name="options">Serialization options.</param> /// <returns>The deserialized suggestions.</returns> public override SuggestResults <T> Read( ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { Debug.Assert(typeToConvert != null); Debug.Assert(typeToConvert.IsAssignableFrom(typeof(SuggestResults <T>))); Debug.Assert(options != null); SuggestResults <T> suggestions = new SuggestResults <T>(); reader.Expects(JsonTokenType.StartObject); while (reader.Read() && reader.TokenType != JsonTokenType.EndObject) { switch (reader.ExpectsPropertyName()) { case Constants.SearchCoverageKey: suggestions.Coverage = reader.ExpectsNullableDouble(); break; case Constants.ValueKey: reader.Expects(JsonTokenType.StartArray); while (reader.Read() && reader.TokenType != JsonTokenType.EndArray) { SearchSuggestion <T> suggestion = _suggestionConverter.Read(ref reader, _suggestionType, options); suggestions.Results.Add(suggestion); } break; default: // Ignore other properties (including OData context, etc.) reader.Skip(); break; } } return(suggestions); }