/// <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 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); }
/// <summary> /// Serializing SearchSuggestion 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 suggestion.</param> /// <param name="options">Serialization options.</param> public override void Write(Utf8JsonWriter writer, SearchSuggestion <T> value, JsonSerializerOptions options) => throw new NotSupportedException($"{nameof(SearchSuggestion<T>)} cannot be serialized to JSON.");