예제 #1
0
        /// <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);
        }
예제 #2
0
 /// <summary>
 /// Serializing SuggestResults 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, SuggestResults <T> value, JsonSerializerOptions options) =>
 throw new NotSupportedException($"{nameof(SuggestResults<T>)} cannot be serialized to JSON.");