// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information. using System.Collections.Generic; using System.Collections.Specialized; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.Net.Http.Formatting; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace System.Net.Http { /// /// Extension methods to allow strongly typed objects to be read from the query component of instances. /// [EditorBrowsable(EditorBrowsableState.Never)] public static class UriExtensions { /// /// Parses the query portion of the specified . /// /// The instance from which to read. /// A containing the parsed result. public static NameValueCollection ParseQueryString(this Uri address) { if (address == null) { throw new ArgumentNullException("address"); } return new FormDataCollection(address).ReadAsNameValueCollection(); } /// /// Reads HTML form URL encoded data provided in the query component as a object. /// /// The instance from which to read. /// An object to be initialized with this instance or null if the conversion cannot be performed. /// true if the query component can be read as ; otherwise false. public static bool TryReadQueryAsJson(this Uri address, out JObject value) { if (address == null) { throw new ArgumentNullException("address"); } IEnumerable> query = new FormDataCollection(address); return FormUrlEncodedJson.TryParse(query, out value); } /// /// Reads HTML form URL encoded data provided in the query component as an of the given . /// /// The instance from which to read. /// The type of the object to read. /// An object to be initialized with this instance or null if the conversion cannot be performed. /// true if the query component can be read as the specified type; otherwise false. [SuppressMessage("Microsoft.Design", "CA1007:UseGenericsWhereAppropriate", Justification = "This is the non-generic version.")] public static bool TryReadQueryAs(this Uri address, Type type, out object value) { if (address == null) { throw new ArgumentNullException("address"); } if (type == null) { throw new ArgumentNullException("type"); } IEnumerable> query = new FormDataCollection(address); JObject jsonObject; if (FormUrlEncodedJson.TryParse(query, out jsonObject)) { using (JTokenReader jsonReader = new JTokenReader(jsonObject)) { value = new JsonSerializer().Deserialize(jsonReader, type); } return true; } value = null; return false; } /// /// Reads HTML form URL encoded data provided in the query component as an of type . /// /// The type of the object to read. /// The instance from which to read. /// An object to be initialized with this instance or null if the conversion cannot be performed. /// true if the query component can be read as the specified type; otherwise false. [SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "The T represents the output parameter, not an input parameter.")] public static bool TryReadQueryAs(this Uri address, out T value) { if (address == null) { throw new ArgumentNullException("address"); } IEnumerable> query = new FormDataCollection(address); JObject jsonObject; if (FormUrlEncodedJson.TryParse(query, out jsonObject)) { value = jsonObject.ToObject(); return true; } value = default(T); return false; } } }