/// <summary> /// Parse a SearchSuggestion 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 suggestion.</returns> public override SearchSuggestion <T> Read( ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { Debug.Assert(options != null); SearchSuggestion <T> suggestion = new SearchSuggestion <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; clone.Expects(JsonTokenType.StartObject); while (clone.Read() && clone.TokenType != JsonTokenType.EndObject) { string name = clone.ExpectsPropertyName(); if (name == Constants.SearchTextKey) { suggestion.Text = clone.ExpectsString(); break; } } // Deserialize the model T document = JsonSerializer.Deserialize <T>(ref reader, options); suggestion.Document = document; return(suggestion); }
/// <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> /// Read the @search.highlights property value. /// </summary> /// <param name="reader">The JSON reader.</param> /// <param name="result">The SearchResult to add the highlights to.</param> private static void ReadHighlights(ref Utf8JsonReader reader, SearchResult <T> result) { Debug.Assert(result != null); result.Highlights = new Dictionary <string, IList <string> >(); reader.Expects(JsonTokenType.StartObject); while (reader.Read() && reader.TokenType != JsonTokenType.EndObject) { // Get the highlight field name string name = reader.ExpectsPropertyName(); // Get the highlight values List <string> values = new List <string>(); reader.Expects(JsonTokenType.StartArray); while (reader.Read() && reader.TokenType != JsonTokenType.EndArray) { values.Add(reader.ExpectsString()); } // Add the highlight result.Highlights[name] = values; } }