Moved ProviderCollectionTest.cs from System assembly to System.Configuration.
[mono.git] / mcs / class / System.ServiceModel.Web / System.ServiceModel.Dispatcher / JsonQueryStringConverter.cs
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.
27 //
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;
36
37 namespace System.ServiceModel.Dispatcher
38 {
39         public class JsonQueryStringConverter : QueryStringConverter
40         {
41                 DataContractJsonSerializer serializer = new DataContractJsonSerializer (typeof (object));
42
43                 public override bool CanConvert (Type type)
44                 {
45                         // almost copy from QueryStringConverter, except that DBNull and XmlQualifiedName are supported
46                         switch (Type.GetTypeCode (type)) {
47                         //case TypeCode.DBNull:
48                         case TypeCode.Empty:
49                                 return false;
50                         case TypeCode.Object:
51                                 if (type == typeof (TimeSpan))
52                                         return true;
53                                 if (type == typeof (DateTimeOffset))
54                                         return true;
55                                 if (type == typeof (Guid))
56                                         return true;
57                                 if (type == typeof (XmlQualifiedName))
58                                         return true;
59                                 if (type == typeof (object))
60                                         return true;
61 //                              if (type.GetCustomAttributes (typeof (TypeConverterAttribute), true).Length > 0)
62 //                                      return true;
63                                 return false;
64                         default:
65                                 return true;
66                         }
67                 }
68
69                 public override object ConvertStringToValue (string parameter, Type parameterType)
70                 {
71                         if (parameterType == null)
72                                 throw new ArgumentNullException ("parameterType");
73
74                         if (!CanConvert (parameterType))
75                                 throw new NotSupportedException (String.Format ("Conversion from the argument parameterType '{0}' is not supported", parameterType));
76
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.
80
81                         switch (Type.GetTypeCode (parameterType)) {
82                         //case TypeCode.String:
83                         //      return parameter;
84                         case TypeCode.Char:
85                                 return parameter != null ? Char.Parse (parameter): default (char);
86                         case TypeCode.SByte:
87                                 return parameter != null ? SByte.Parse (parameter, CultureInfo.InvariantCulture): default (sbyte);
88                         case TypeCode.Byte:
89                                 return parameter != null ? Byte.Parse (parameter, CultureInfo.InvariantCulture): default (byte);
90                         case TypeCode.Int16:
91                                 return parameter != null ? Int16.Parse (parameter, CultureInfo.InvariantCulture): default (short);
92                         case TypeCode.Int32:
93                                 return parameter != null ? Int32.Parse (parameter, CultureInfo.InvariantCulture): default (int);
94                         case TypeCode.Int64:
95                                 return parameter != null ? Int64.Parse (parameter, CultureInfo.InvariantCulture): default (long);
96                         case TypeCode.UInt16:
97                                 return parameter != null ? UInt16.Parse (parameter, CultureInfo.InvariantCulture): default (ushort);
98                         case TypeCode.UInt32:
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);
112                         }
113
114                         if (parameter == null)
115                                 return null;
116
117
118                         DataContractJsonSerializer serializer =
119                                 new DataContractJsonSerializer (parameterType);
120                         // hmm, it costs so silly though.
121                         return serializer.ReadObject (new MemoryStream (Encoding.UTF8.GetBytes (parameter)));
122                 }
123
124                 bool IsKnownType (Type t)
125                 {
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))
133                                         return true;
134                                 return false;
135                         default:
136                                 return true;
137                         }
138                 }
139
140                 public override string ConvertValueToString (object parameter, Type parameterType)
141                 {
142                         if (parameterType == null)
143                                 throw new ArgumentNullException ("parameterType");
144
145                         if (!CanConvert (parameterType))
146                                 throw new NotSupportedException (String.Format ("Conversion from the argument parameterType '{0}' is not supported", parameterType));
147
148                         if (parameter == null)
149                                 return null;
150
151                         if (parameter is DBNull)
152                                 return "{}";
153
154                         parameterType = ToActualType (parameterType);
155
156                         if (parameter is IConvertible)
157                                 parameter = ((IConvertible) parameter).ToType (parameterType, CultureInfo.InvariantCulture);
158
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 ("\"", "\\\"");
166                                 sb.Insert (0, '\"');
167                                 sb.Append ('\"');
168                                 return sb.ToString ();
169                         default:
170                                 if (parameterType == typeof (XmlQualifiedName)) {
171                                         var qname = (XmlQualifiedName) parameter;
172                                         return String.Concat ("\"", qname.Name, ":", qname.Namespace, "\"");
173                                 }
174                                 return parameter.ToString ();
175                         }
176
177                         throw new NotImplementedException ();
178                 }
179
180                 Type ToActualType (Type t)
181                 {
182                         switch (Type.GetTypeCode (t)) {
183                         case TypeCode.DBNull: // though DBNull.Value input is converted to "{}". This result is used for String input.
184                         case TypeCode.Char:
185                         case TypeCode.String:
186                                 return typeof (string);
187                         case TypeCode.SByte:
188                         case TypeCode.Int16:
189                         case TypeCode.Int32:
190                         case TypeCode.Int64:
191 //                              return typeof (long);
192                                 return typeof (decimal);
193                         case TypeCode.Byte:
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:
201                                 return t;
202                         case TypeCode.Single:
203                         case TypeCode.Double:
204 //                              return typeof (double);
205                                 return typeof (decimal);
206                         case TypeCode.Decimal:
207                                 return typeof (decimal);
208                         default:
209                                 return t;
210                         }
211                 }
212         }
213 }