summaryrefslogtreecommitdiff
path: root/external/aspnetwebstack/src/System.Net.Http.Formatting/UriExtensions.cs
blob: ebb252741faba09172534a7e6af0eb34d6ae5a0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// 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
{
    /// <summary>
    /// Extension methods to allow strongly typed objects to be read from the query component of <see cref="Uri"/> instances.
    /// </summary>
    [EditorBrowsable(EditorBrowsableState.Never)]
    public static class UriExtensions
    {
        /// <summary>
        /// Parses the query portion of the specified <see cref="Uri"/>.
        /// </summary>
        /// <param name="address">The <see cref="Uri"/> instance from which to read.</param>
        /// <returns>A <see cref="NameValueCollection"/> containing the parsed result.</returns>
        public static NameValueCollection ParseQueryString(this Uri address)
        {
            if (address == null)
            {
                throw new ArgumentNullException("address");
            }

            return new FormDataCollection(address).ReadAsNameValueCollection();
        }

        /// <summary>
        /// Reads HTML form URL encoded data provided in the <see cref="Uri"/> query component as a <see cref="JObject"/> object.
        /// </summary>
        /// <param name="address">The <see cref="Uri"/> instance from which to read.</param>
        /// <param name="value">An object to be initialized with this instance or null if the conversion cannot be performed.</param>
        /// <returns><c>true</c> if the query component can be read as <see cref="JObject"/>; otherwise <c>false</c>.</returns>
        public static bool TryReadQueryAsJson(this Uri address, out JObject value)
        {
            if (address == null)
            {
                throw new ArgumentNullException("address");
            }

            IEnumerable<KeyValuePair<string, string>> query = new FormDataCollection(address);
            return FormUrlEncodedJson.TryParse(query, out value);
        }

        /// <summary>
        /// Reads HTML form URL encoded data provided in the <see cref="Uri"/> query component as an <see cref="Object"/> of the given <paramref name="type"/>.
        /// </summary>
        /// <param name="address">The <see cref="Uri"/> instance from which to read.</param>
        /// <param name="type">The type of the object to read.</param>
        /// <param name="value">An object to be initialized with this instance or null if the conversion cannot be performed.</param>
        /// <returns><c>true</c> if the query component can be read as the specified type; otherwise <c>false</c>.</returns>
        [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<KeyValuePair<string, string>> 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;
        }

        /// <summary>
        /// Reads HTML form URL encoded data provided in the <see cref="Uri"/> query component as an <see cref="Object"/> of type <typeparamref name="T"/>.
        /// </summary>
        /// <typeparam name="T">The type of the object to read.</typeparam>
        /// <param name="address">The <see cref="Uri"/> instance from which to read.</param>
        /// <param name="value">An object to be initialized with this instance or null if the conversion cannot be performed.</param>
        /// <returns><c>true</c> if the query component can be read as the specified type; otherwise <c>false</c>.</returns>
        [SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "The T represents the output parameter, not an input parameter.")]
        public static bool TryReadQueryAs<T>(this Uri address, out T value)
        {
            if (address == null)
            {
                throw new ArgumentNullException("address");
            }

            IEnumerable<KeyValuePair<string, string>> query = new FormDataCollection(address);
            JObject jsonObject;
            if (FormUrlEncodedJson.TryParse(query, out jsonObject))
            {
                value = jsonObject.ToObject<T>();
                return true;
            }

            value = default(T);
            return false;
        }
    }
}