2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.ServiceModel.Web / System.ServiceModel.Dispatcher / JsonQueryStringConverter.cs
blob744069b0731e489c5bf82cb3c9bf4f73412b8e15
1 //
2 // JsonQueryStringConverter.cs
3 //
4 // Author:
5 // Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
8 //
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:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
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.
28 using System;
29 using System.Globalization;
30 using System.IO;
31 using System.Runtime.Serialization.Json;
32 using System.ServiceModel;
33 using System.ServiceModel.Description;
34 using System.Text;
35 using System.Xml;
37 namespace System.ServiceModel.Dispatcher
39 public class JsonQueryStringConverter : QueryStringConverter
41 DataContractJsonSerializer serializer = new DataContractJsonSerializer (typeof (object));
43 internal string CustomWrapperName { get; set; }
45 public override bool CanConvert (Type type)
47 // almost copy from QueryStringConverter, except that DBNull and XmlQualifiedName are supported
48 switch (Type.GetTypeCode (type)) {
49 //case TypeCode.DBNull:
50 case TypeCode.Empty:
51 return false;
52 case TypeCode.Object:
53 if (type == typeof (TimeSpan))
54 return true;
55 if (type == typeof (DateTimeOffset))
56 return true;
57 if (type == typeof (Guid))
58 return true;
59 if (type == typeof (XmlQualifiedName))
60 return true;
61 if (type == typeof (object))
62 return true;
63 // if (type.GetCustomAttributes (typeof (TypeConverterAttribute), true).Length > 0)
64 // return true;
65 return false;
66 default:
67 return true;
71 public override object ConvertStringToValue (string parameter, Type parameterType)
73 if (parameterType == null)
74 throw new ArgumentNullException ("parameterType");
76 if (!CanConvert (parameterType))
77 throw new NotSupportedException (String.Format ("Conversion from the argument parameterType '{0}' is not supported", parameterType));
79 // In general .NET JSON parser is sloppy. It accepts
80 // such a string that is actually invalid in terms of
81 // the target type in JSON context.
83 switch (Type.GetTypeCode (parameterType)) {
84 case TypeCode.String:
85 // LAMESPEC LAMESPEC LAMESPEC: we cannot give "foo" as the string value input (even if they are escaped as %22!)
86 if (parameter == null)
87 return null;
88 if (parameter.Length > 1 && parameter [0] == '"' && parameter [parameter.Length - 1] == '"')
89 return parameter.Substring (1, parameter.Length - 2);
90 else if (parameter [0] != '"')
91 return parameter;
92 break;
93 #if !NET_2_1
94 case TypeCode.Char:
95 return parameter != null ? Char.Parse (parameter): default (char);
96 #endif
97 case TypeCode.SByte:
98 return parameter != null ? SByte.Parse (parameter, CultureInfo.InvariantCulture): default (sbyte);
99 case TypeCode.Byte:
100 return parameter != null ? Byte.Parse (parameter, CultureInfo.InvariantCulture): default (byte);
101 case TypeCode.Int16:
102 return parameter != null ? Int16.Parse (parameter, CultureInfo.InvariantCulture): default (short);
103 case TypeCode.Int32:
104 return parameter != null ? Int32.Parse (parameter, CultureInfo.InvariantCulture): default (int);
105 case TypeCode.Int64:
106 return parameter != null ? Int64.Parse (parameter, CultureInfo.InvariantCulture): default (long);
107 case TypeCode.UInt16:
108 return parameter != null ? UInt16.Parse (parameter, CultureInfo.InvariantCulture): default (ushort);
109 case TypeCode.UInt32:
110 return parameter != null ? UInt32.Parse (parameter, CultureInfo.InvariantCulture): default (uint);
111 case TypeCode.UInt64:
112 return parameter != null ? UInt64.Parse (parameter, CultureInfo.InvariantCulture): default (ulong);
113 case TypeCode.DateTime:
114 return parameter != null ? DateTime.Parse (parameter, CultureInfo.InvariantCulture): default (DateTime);
115 case TypeCode.Boolean:
116 return parameter != null ? Boolean.Parse (parameter): default (bool);
117 case TypeCode.Single:
118 return parameter != null ? Single.Parse (parameter, CultureInfo.InvariantCulture): default (float);
119 case TypeCode.Double:
120 return parameter != null ? Double.Parse (parameter, CultureInfo.InvariantCulture): default (double);
121 case TypeCode.Decimal:
122 return parameter != null ? Decimal.Parse (parameter, CultureInfo.InvariantCulture): default (decimal);
125 if (parameter == null)
126 return null;
129 DataContractJsonSerializer serializer =
130 new DataContractJsonSerializer (parameterType);
131 // hmm, it costs so silly though.
132 return serializer.ReadObject (new MemoryStream (Encoding.UTF8.GetBytes (parameter)));
135 bool IsKnownType (Type t)
137 switch (Type.GetTypeCode (t)) {
138 case TypeCode.Object:
139 if (t == typeof (Guid) ||
140 t == typeof (DBNull) ||
141 t == typeof (DateTimeOffset) ||
142 t == typeof (TimeSpan) ||
143 t == typeof (XmlQualifiedName))
144 return true;
145 return false;
146 default:
147 return true;
151 public override string ConvertValueToString (object parameter, Type parameterType)
153 if (parameterType == null)
154 throw new ArgumentNullException ("parameterType");
156 if (!CanConvert (parameterType))
157 throw new NotSupportedException (String.Format ("Conversion from the argument parameterType '{0}' is not supported", parameterType));
159 if (parameter == null)
160 return null;
162 if (parameter is DBNull)
163 return "{}";
165 parameterType = ToActualType (parameterType);
167 if (parameter is IConvertible)
168 parameter = ((IConvertible) parameter).ToType (parameterType, CultureInfo.InvariantCulture);
170 switch (Type.GetTypeCode (parameterType)) {
171 case TypeCode.String:
172 string s = parameter is IFormattable ?
173 ((IFormattable) parameter).ToString (null, CultureInfo.InvariantCulture) :
174 parameter.ToString ();
175 StringBuilder sb = new StringBuilder (s);
176 sb.Replace ("\"", "\\\"");
177 sb.Insert (0, "\"");
178 sb.Append ('\"');
179 return sb.ToString ();
180 default:
181 if (parameterType == typeof (XmlQualifiedName)) {
182 var qname = (XmlQualifiedName) parameter;
183 return String.Concat ("\"", qname.Name, ":", qname.Namespace, "\"");
185 return parameter.ToString ();
188 throw new NotImplementedException ();
191 Type ToActualType (Type t)
193 switch (Type.GetTypeCode (t)) {
194 case TypeCode.DBNull: // though DBNull.Value input is converted to "{}". This result is used for String input.
195 case TypeCode.Char:
196 case TypeCode.String:
197 return typeof (string);
198 case TypeCode.SByte:
199 case TypeCode.Int16:
200 case TypeCode.Int32:
201 case TypeCode.Int64:
202 // return typeof (long);
203 return typeof (decimal);
204 case TypeCode.Byte:
205 case TypeCode.UInt16:
206 case TypeCode.UInt32:
207 case TypeCode.UInt64:
208 // return typeof (ulong);
209 return typeof (decimal);
210 case TypeCode.DateTime:
211 case TypeCode.Boolean:
212 return t;
213 case TypeCode.Single:
214 case TypeCode.Double:
215 // return typeof (double);
216 return typeof (decimal);
217 case TypeCode.Decimal:
218 return typeof (decimal);
219 default:
220 return t;