summaryrefslogtreecommitdiff
path: root/mcs/class/System.ServiceModel.Web/System.ServiceModel.Dispatcher/JsonQueryStringConverter.cs
blob: 5d255ac84461544d7d886c76adc9a670146b7922 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//
// JsonQueryStringConverter.cs
//
// Author:
//	Atsushi Enomoto  <atsushi@ximian.com>
//
// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Globalization;
using System.IO;
using System.Runtime.Serialization.Json;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using System.Xml;

namespace System.ServiceModel.Dispatcher
{
	public class JsonQueryStringConverter : QueryStringConverter
	{
		DataContractJsonSerializer serializer = new DataContractJsonSerializer (typeof (object));

		internal string CustomWrapperName { get; set; }

		public override bool CanConvert (Type type)
		{
			// almost copy from QueryStringConverter, except that DBNull and XmlQualifiedName are supported
			switch (Type.GetTypeCode (type)) {
			//case TypeCode.DBNull:
			case TypeCode.Empty:
				return false;
			case TypeCode.Object:
				if (type == typeof (TimeSpan))
					return true;
				if (type == typeof (DateTimeOffset))
					return true;
				if (type == typeof (Guid))
					return true;
				if (type == typeof (XmlQualifiedName))
					return true;
				if (type == typeof (object))
					return true;
//				if (type.GetCustomAttributes (typeof (TypeConverterAttribute), true).Length > 0)
//					return true;

				// FIXME: it should return false for things like List<OfPrivateType>.
				return type.IsPublic || type.IsNestedPublic;
			default:
				return true;
			}
		}

		public override object ConvertStringToValue (string parameter, Type parameterType)
		{
			if (parameterType == null)
				throw new ArgumentNullException ("parameterType");

			if (!CanConvert (parameterType))
				throw new NotSupportedException (String.Format ("Conversion from the argument parameterType '{0}' is not supported", parameterType));

			// In general .NET JSON parser is sloppy. It accepts 
			// such a string that is actually invalid in terms of
			// the target type in JSON context.

			switch (Type.GetTypeCode (parameterType)) {
			case TypeCode.String:
				// LAMESPEC LAMESPEC LAMESPEC: we cannot give "foo" as the string value input (even if they are escaped as %22!)
				if (parameter == null)
					return null;
				if (parameter.Length > 1 && parameter [0] == '"' && parameter [parameter.Length - 1] == '"')
					return parameter.Substring (1, parameter.Length - 2);
				else if (parameter [0] != '"')
					return parameter;
				break;
#if !NET_2_1
			case TypeCode.Char:
				return parameter != null ? Char.Parse (parameter): default (char);
#endif
			case TypeCode.SByte:
				return parameter != null ? SByte.Parse (parameter, CultureInfo.InvariantCulture): default (sbyte);
			case TypeCode.Byte:
				return parameter != null ? Byte.Parse (parameter, CultureInfo.InvariantCulture): default (byte);
			case TypeCode.Int16:
				return parameter != null ? Int16.Parse (parameter, CultureInfo.InvariantCulture): default (short);
			case TypeCode.Int32:
				return parameter != null ? Int32.Parse (parameter, CultureInfo.InvariantCulture): default (int);
			case TypeCode.Int64:
				return parameter != null ? Int64.Parse (parameter, CultureInfo.InvariantCulture): default (long);
			case TypeCode.UInt16:
				return parameter != null ? UInt16.Parse (parameter, CultureInfo.InvariantCulture): default (ushort);
			case TypeCode.UInt32:
				return parameter != null ? UInt32.Parse (parameter, CultureInfo.InvariantCulture): default (uint);
			case TypeCode.UInt64:
				return parameter != null ? UInt64.Parse (parameter, CultureInfo.InvariantCulture): default (ulong);
			case TypeCode.DateTime:
				return parameter != null ? DateTime.Parse (parameter, CultureInfo.InvariantCulture): default (DateTime);
			case TypeCode.Boolean:
				return parameter != null ? Boolean.Parse (parameter): default (bool);
			case TypeCode.Single:
				return parameter != null ? Single.Parse (parameter, CultureInfo.InvariantCulture): default (float);
			case TypeCode.Double:
				return parameter != null ? Double.Parse (parameter, CultureInfo.InvariantCulture): default (double);
			case TypeCode.Decimal:
				return parameter != null ? Decimal.Parse (parameter, CultureInfo.InvariantCulture): default (decimal);
			}

			if (parameter == null)
				return null;


			DataContractJsonSerializer serializer =
				new DataContractJsonSerializer (parameterType);
			// hmm, it costs so silly though.
			return serializer.ReadObject (new MemoryStream (Encoding.UTF8.GetBytes (parameter)));
		}

		bool IsKnownType (Type t)
		{
			switch (Type.GetTypeCode (t)) {
			case TypeCode.Object:
				if (t == typeof (Guid) ||
				    t == typeof (DBNull) ||
				    t == typeof (DateTimeOffset) ||
				    t == typeof (TimeSpan) ||
				    t == typeof (XmlQualifiedName))
					return true;
				return false;
			default:
				return true;
			}
		}

		public override string ConvertValueToString (object parameter, Type parameterType)
		{
			if (parameterType == null)
				throw new ArgumentNullException ("parameterType");

			if (!CanConvert (parameterType))
				throw new NotSupportedException (String.Format ("Conversion from the argument parameterType '{0}' is not supported", parameterType));

			if (parameter == null)
				return null;

			if (parameter is DBNull)
				return "{}";

			parameterType = ToActualType (parameterType);

			if (parameter is IConvertible)
				parameter = ((IConvertible) parameter).ToType (parameterType, CultureInfo.InvariantCulture);

			switch (Type.GetTypeCode (parameterType)) {
			case TypeCode.String:
				string s = parameter is IFormattable ?
					((IFormattable) parameter).ToString (null, CultureInfo.InvariantCulture) :
					parameter.ToString ();
				StringBuilder sb = new StringBuilder (s);
				sb.Replace ("\"", "\\\"");
				sb.Insert (0, "\"");
				sb.Append ('\"');
				return sb.ToString ();
			default:
				if (parameterType == typeof (XmlQualifiedName)) {
					var qname = (XmlQualifiedName) parameter;
					return String.Concat ("\"", qname.Name, ":", qname.Namespace, "\"");
				}
				return parameter.ToString ();
			}
		}

		Type ToActualType (Type t)
		{
			switch (Type.GetTypeCode (t)) {
			case TypeCode.DBNull: // though DBNull.Value input is converted to "{}". This result is used for String input.
			case TypeCode.Char:
			case TypeCode.String:
				return typeof (string);
			case TypeCode.SByte:
			case TypeCode.Int16:
			case TypeCode.Int32:
			case TypeCode.Int64:
//				return typeof (long);
				return typeof (decimal);
			case TypeCode.Byte:
			case TypeCode.UInt16:
			case TypeCode.UInt32:
			case TypeCode.UInt64:
//				return typeof (ulong);
				return typeof (decimal);
			case TypeCode.DateTime:
			case TypeCode.Boolean:
				return t;
			case TypeCode.Single:
			case TypeCode.Double:
//				return typeof (double);
				return typeof (decimal);
			case TypeCode.Decimal:
				return typeof (decimal);
			default:
				return t;
			}
		}
	}
}