#pragma warning disable CS1572 // Not all parameters will be used depending on feature flags
        /// <summary>
        /// Deserialize the SuggestResults.
        /// </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 SuggestResults.</returns>
        internal static async Task <SuggestResults <T> > DeserializeAsync(
            Stream json,
#if EXPERIMENTAL_SERIALIZER
            ObjectSerializer serializer,
#endif
            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;

            SuggestResults <T> suggestions = new SuggestResults <T>();

            foreach (JsonProperty prop in doc.RootElement.EnumerateObject())
            {
                if (prop.NameEquals(Constants.SearchCoverageKeyJson.EncodedUtf8Bytes) &&
                    prop.Value.ValueKind != JsonValueKind.Null)
                {
                    suggestions.Coverage = prop.Value.GetDouble();
                }
                else if (prop.NameEquals(Constants.ValueKeyJson.EncodedUtf8Bytes))
                {
                    List <SearchSuggestion <T> > results = new List <SearchSuggestion <T> >();
                    foreach (JsonElement element in prop.Value.EnumerateArray())
                    {
                        SearchSuggestion <T> suggestion = await SearchSuggestion <T> .DeserializeAsync(
                            element,
#if EXPERIMENTAL_SERIALIZER
                            serializer,
#endif
                            defaultSerializerOptions,
                            async,
                            cancellationToken)
                                                          .ConfigureAwait(false);

                        results.Add(suggestion);
                    }
                    suggestions.Results = new ReadOnlyCollection <SearchSuggestion <T> >(results);
                }
            }
            return(suggestions);
        }
Пример #2
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);
        }
Пример #3
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.");