Repository navigation
Expand file tree
/
Copy pathHttpBuildQueryHelper.cs
More file actions
123 lines (109 loc) · 4.15 KB
/
Copy pathHttpBuildQueryHelper.cs
File metadata and controls
123 lines (109 loc) · 4.15 KB
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
114
115
116
117
118
119
120
121
122
123
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Reflection;
using System.Web;
namespace HttpBuildQuery
{
public class HttpBuildQueryHelper
{
public static string Format(object value, string prefix = "q")
{
if (value == null) return String.Format("{0}=", prefix);
var parts = new List<string>();
HandleItem(value, parts, prefix);
return String.Join("&", parts.ToArray());
}
public static string FormatValue(object value, string prefix = "q")
{
string strValue = HttpUtility.UrlEncode(value.ToString());
if (value is bool)
{
strValue = (bool)value ? "1" : "0";
}
return String.IsNullOrEmpty(strValue) ? String.Empty : String.Format("{0}={1}", prefix, strValue);
}
public static string FormatList(IList obj, string prefix = "q")
{
int count = obj.Count;
var parts = new List<string>();
for (int i = 0; i < count; i++)
{
string newPrefix = String.Format("{0}[{1}]", prefix, i);
HandleItem(obj[i], parts, newPrefix);
}
return String.Join("&", parts.ToArray());
}
public static string FormatDictionary(IDictionary<string, object> obj, string prefix = "")
{
var parts = new List<string>();
foreach (var entry in obj)
{
string newPrefix = string.IsNullOrEmpty(prefix) ?
String.Format("{0}{1}", prefix, entry.Key) :
String.Format("{0}[{1}]", prefix, entry.Key);
HandleItem(entry.Value, parts, newPrefix);
}
return String.Join("&", parts.ToArray());
}
private static void HandleItem(object value, List<string> parts, string prefix)
{
if (value == null) return;
if (IsStringable(value))
{
parts.Add(FormatValue(value, prefix));
}
else if (value is IList)
{
parts.Add(FormatList((IList)value, prefix));
}
else if (value is IDictionary<string, object>)
{
parts.Add(FormatDictionary((IDictionary<string, object>)value, prefix));
}
else
{
parts.Add(FormatValue(value, prefix));
}
}
public static object Convert(object obj, int depthLimit = 0)
{
if (depthLimit > 5) return obj; // prevent recursion from not ending
if (obj == null) return obj;
Type type = obj.GetType();
if (IsStringable(obj) || IsStringableArray(obj)) return obj;
if (type.IsArray)
{
var parts = new List<object>();
foreach (object e in (IList)obj)
{
parts.Add(Convert(e));
}
return parts.ToArray();
}
var dict = new Dictionary<string, object>();
PropertyInfo[] props = type.GetProperties();
foreach (PropertyInfo prop in props)
{
dict.Add(prop.Name, Convert(prop.GetValue(obj, null), depthLimit + 1));
}
return dict;
}
private static bool IsStringable(object o)
{
return (o is bool) || (o is byte) || (o is char) || (o is decimal) ||
(o is double) || (o is float) || (o is int) || (o is long) ||
(o is sbyte) || (o is short) || (o is uint) || (o is ulong) ||
(o is ushort) || (o is string);
}
private static bool IsStringableArray(object o)
{
return (o is bool[]) || (o is byte[]) || (o is char[]) || (o is decimal[]) ||
(o is double[]) || (o is float[]) || (o is int[]) || (o is long[]) ||
(o is sbyte[]) || (o is short[]) || (o is uint[]) || (o is ulong[]) ||
(o is ushort[]) || (o is string[]);
}
}
}