2 // JsonQueryStringConverter.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Globalization;
31 using System.Runtime.Serialization.Json;
32 using System.ServiceModel;
33 using System.ServiceModel.Description;
37 namespace System.ServiceModel.Dispatcher
39 public class JsonQueryStringConverter : QueryStringConverter
41 DataContractJsonSerializer serializer = new DataContractJsonSerializer (typeof (object));
43 public override bool CanConvert (Type type)
45 // almost copy from QueryStringConverter, except that DBNull and XmlQualifiedName are supported
46 switch (Type.GetTypeCode (type)) {
47 //case TypeCode.DBNull:
51 if (type == typeof (TimeSpan))
53 if (type == typeof (DateTimeOffset))
55 if (type == typeof (Guid))
57 if (type == typeof (XmlQualifiedName))
59 if (type == typeof (object))
61 // if (type.GetCustomAttributes (typeof (TypeConverterAttribute), true).Length > 0)
69 public override object ConvertStringToValue (string parameter, Type parameterType)
71 if (parameterType == null)
72 throw new ArgumentNullException ("parameterType");
74 if (!CanConvert (parameterType))
75 throw new NotSupportedException (String.Format ("Conversion from the argument parameterType '{0}' is not supported", parameterType));
77 // In general .NET JSON parser is sloppy. It accepts
78 // such a string that is actually invalid in terms of
79 // the target type in JSON context.
81 switch (Type.GetTypeCode (parameterType)) {
82 //case TypeCode.String:
85 return parameter != null ? Char.Parse (parameter): default (char);
87 return parameter != null ? SByte.Parse (parameter, CultureInfo.InvariantCulture): default (sbyte);
89 return parameter != null ? Byte.Parse (parameter, CultureInfo.InvariantCulture): default (byte);
91 return parameter != null ? Int16.Parse (parameter, CultureInfo.InvariantCulture): default (short);
93 return parameter != null ? Int32.Parse (parameter, CultureInfo.InvariantCulture): default (int);
95 return parameter != null ? Int64.Parse (parameter, CultureInfo.InvariantCulture): default (long);
97 return parameter != null ? UInt16.Parse (parameter, CultureInfo.InvariantCulture): default (ushort);
99 return parameter != null ? UInt32.Parse (parameter, CultureInfo.InvariantCulture): default (uint);
100 case TypeCode.UInt64:
101 return parameter != null ? UInt64.Parse (parameter, CultureInfo.InvariantCulture): default (ulong);
102 case TypeCode.DateTime:
103 return parameter != null ? DateTime.Parse (parameter, CultureInfo.InvariantCulture): default (DateTime);
104 case TypeCode.Boolean:
105 return parameter != null ? Boolean.Parse (parameter): default (bool);
106 case TypeCode.Single:
107 return parameter != null ? Single.Parse (parameter, CultureInfo.InvariantCulture): default (float);
108 case TypeCode.Double:
109 return parameter != null ? Double.Parse (parameter, CultureInfo.InvariantCulture): default (double);
110 case TypeCode.Decimal:
111 return parameter != null ? Decimal.Parse (parameter, CultureInfo.InvariantCulture): default (decimal);
114 if (parameter == null)
118 DataContractJsonSerializer serializer =
119 new DataContractJsonSerializer (parameterType);
120 // hmm, it costs so silly though.
121 return serializer.ReadObject (new MemoryStream (Encoding.UTF8.GetBytes (parameter)));
124 bool IsKnownType (Type t)
126 switch (Type.GetTypeCode (t)) {
127 case TypeCode.Object:
128 if (t == typeof (Guid) ||
129 t == typeof (DBNull) ||
130 t == typeof (DateTimeOffset) ||
131 t == typeof (TimeSpan) ||
132 t == typeof (XmlQualifiedName))
140 public override string ConvertValueToString (object parameter, Type parameterType)
142 if (parameterType == null)
143 throw new ArgumentNullException ("parameterType");
145 if (!CanConvert (parameterType))
146 throw new NotSupportedException (String.Format ("Conversion from the argument parameterType '{0}' is not supported", parameterType));
148 if (parameter == null)
151 if (parameter is DBNull)
154 parameterType = ToActualType (parameterType);
156 if (parameter is IConvertible)
157 parameter = ((IConvertible) parameter).ToType (parameterType, CultureInfo.InvariantCulture);
159 switch (Type.GetTypeCode (parameterType)) {
160 case TypeCode.String:
161 string s = parameter is IFormattable ?
162 ((IFormattable) parameter).ToString (null, CultureInfo.InvariantCulture) :
163 parameter.ToString ();
164 StringBuilder sb = new StringBuilder (s);
165 sb.Replace ("\"", "\\\"");
168 return sb.ToString ();
170 if (parameterType == typeof (XmlQualifiedName)) {
171 var qname = (XmlQualifiedName) parameter;
172 return String.Concat ("\"", qname.Name, ":", qname.Namespace, "\"");
174 return parameter.ToString ();
177 throw new NotImplementedException ();
180 Type ToActualType (Type t)
182 switch (Type.GetTypeCode (t)) {
183 case TypeCode.DBNull: // though DBNull.Value input is converted to "{}". This result is used for String input.
185 case TypeCode.String:
186 return typeof (string);
191 // return typeof (long);
192 return typeof (decimal);
194 case TypeCode.UInt16:
195 case TypeCode.UInt32:
196 case TypeCode.UInt64:
197 // return typeof (ulong);
198 return typeof (decimal);
199 case TypeCode.DateTime:
200 case TypeCode.Boolean:
202 case TypeCode.Single:
203 case TypeCode.Double:
204 // return typeof (double);
205 return typeof (decimal);
206 case TypeCode.Decimal:
207 return typeof (decimal);