JsonQueryStringConverter.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. //
  2. // JsonQueryStringConverter.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  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. namespace System.ServiceModel.Dispatcher
  37. {
  38. public class JsonQueryStringConverter : QueryStringConverter
  39. {
  40. DataContractJsonSerializer serializer = new DataContractJsonSerializer (typeof (object));
  41. internal string CustomWrapperName { get; set; }
  42. public override bool CanConvert (Type type)
  43. {
  44. // almost copy from QueryStringConverter, except that DBNull and XmlQualifiedName are supported
  45. switch (Type.GetTypeCode (type)) {
  46. //case TypeCode.DBNull:
  47. case TypeCode.Empty:
  48. return false;
  49. case TypeCode.Object:
  50. if (type == typeof (TimeSpan))
  51. return true;
  52. if (type == typeof (DateTimeOffset))
  53. return true;
  54. if (type == typeof (Guid))
  55. return true;
  56. if (type == typeof (XmlQualifiedName))
  57. return true;
  58. if (type == typeof (object))
  59. return true;
  60. // if (type.GetCustomAttributes (typeof (TypeConverterAttribute), true).Length > 0)
  61. // return true;
  62. return false;
  63. default:
  64. return true;
  65. }
  66. }
  67. public override object ConvertStringToValue (string parameter, Type parameterType)
  68. {
  69. if (parameterType == null)
  70. throw new ArgumentNullException ("parameterType");
  71. if (!CanConvert (parameterType))
  72. throw new NotSupportedException (String.Format ("Conversion from the argument parameterType '{0}' is not supported", parameterType));
  73. // In general .NET JSON parser is sloppy. It accepts
  74. // such a string that is actually invalid in terms of
  75. // the target type in JSON context.
  76. switch (Type.GetTypeCode (parameterType)) {
  77. case TypeCode.String:
  78. // LAMESPEC LAMESPEC LAMESPEC: we cannot give "foo" as the string value input (even if they are escaped as %22!)
  79. if (parameter == null)
  80. return null;
  81. if (parameter.Length > 1 && parameter [0] == '"' && parameter [parameter.Length - 1] == '"')
  82. return parameter.Substring (1, parameter.Length - 2);
  83. else if (parameter [0] != '"')
  84. return parameter;
  85. break;
  86. #if !NET_2_1
  87. case TypeCode.Char:
  88. return parameter != null ? Char.Parse (parameter): default (char);
  89. #endif
  90. case TypeCode.SByte:
  91. return parameter != null ? SByte.Parse (parameter, CultureInfo.InvariantCulture): default (sbyte);
  92. case TypeCode.Byte:
  93. return parameter != null ? Byte.Parse (parameter, CultureInfo.InvariantCulture): default (byte);
  94. case TypeCode.Int16:
  95. return parameter != null ? Int16.Parse (parameter, CultureInfo.InvariantCulture): default (short);
  96. case TypeCode.Int32:
  97. return parameter != null ? Int32.Parse (parameter, CultureInfo.InvariantCulture): default (int);
  98. case TypeCode.Int64:
  99. return parameter != null ? Int64.Parse (parameter, CultureInfo.InvariantCulture): default (long);
  100. case TypeCode.UInt16:
  101. return parameter != null ? UInt16.Parse (parameter, CultureInfo.InvariantCulture): default (ushort);
  102. case TypeCode.UInt32:
  103. return parameter != null ? UInt32.Parse (parameter, CultureInfo.InvariantCulture): default (uint);
  104. case TypeCode.UInt64:
  105. return parameter != null ? UInt64.Parse (parameter, CultureInfo.InvariantCulture): default (ulong);
  106. case TypeCode.DateTime:
  107. return parameter != null ? DateTime.Parse (parameter, CultureInfo.InvariantCulture): default (DateTime);
  108. case TypeCode.Boolean:
  109. return parameter != null ? Boolean.Parse (parameter): default (bool);
  110. case TypeCode.Single:
  111. return parameter != null ? Single.Parse (parameter, CultureInfo.InvariantCulture): default (float);
  112. case TypeCode.Double:
  113. return parameter != null ? Double.Parse (parameter, CultureInfo.InvariantCulture): default (double);
  114. case TypeCode.Decimal:
  115. return parameter != null ? Decimal.Parse (parameter, CultureInfo.InvariantCulture): default (decimal);
  116. }
  117. if (parameter == null)
  118. return null;
  119. DataContractJsonSerializer serializer =
  120. new DataContractJsonSerializer (parameterType);
  121. // hmm, it costs so silly though.
  122. return serializer.ReadObject (new MemoryStream (Encoding.UTF8.GetBytes (parameter)));
  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. public override string ConvertValueToString (object parameter, Type parameterType)
  140. {
  141. if (parameterType == null)
  142. throw new ArgumentNullException ("parameterType");
  143. if (!CanConvert (parameterType))
  144. throw new NotSupportedException (String.Format ("Conversion from the argument parameterType '{0}' is not supported", parameterType));
  145. if (parameter == null)
  146. return null;
  147. if (parameter is DBNull)
  148. return "{}";
  149. parameterType = ToActualType (parameterType);
  150. if (parameter is IConvertible)
  151. parameter = ((IConvertible) parameter).ToType (parameterType, CultureInfo.InvariantCulture);
  152. switch (Type.GetTypeCode (parameterType)) {
  153. case TypeCode.String:
  154. string s = parameter is IFormattable ?
  155. ((IFormattable) parameter).ToString (null, CultureInfo.InvariantCulture) :
  156. parameter.ToString ();
  157. StringBuilder sb = new StringBuilder (s);
  158. sb.Replace ("\"", "\\\"");
  159. sb.Insert (0, "\"");
  160. sb.Append ('\"');
  161. return sb.ToString ();
  162. default:
  163. if (parameterType == typeof (XmlQualifiedName)) {
  164. var qname = (XmlQualifiedName) parameter;
  165. return String.Concat ("\"", qname.Name, ":", qname.Namespace, "\"");
  166. }
  167. return parameter.ToString ();
  168. }
  169. throw new NotImplementedException ();
  170. }
  171. Type ToActualType (Type t)
  172. {
  173. switch (Type.GetTypeCode (t)) {
  174. case TypeCode.DBNull: // though DBNull.Value input is converted to "{}". This result is used for String input.
  175. case TypeCode.Char:
  176. case TypeCode.String:
  177. return typeof (string);
  178. case TypeCode.SByte:
  179. case TypeCode.Int16:
  180. case TypeCode.Int32:
  181. case TypeCode.Int64:
  182. // return typeof (long);
  183. return typeof (decimal);
  184. case TypeCode.Byte:
  185. case TypeCode.UInt16:
  186. case TypeCode.UInt32:
  187. case TypeCode.UInt64:
  188. // return typeof (ulong);
  189. return typeof (decimal);
  190. case TypeCode.DateTime:
  191. case TypeCode.Boolean:
  192. return t;
  193. case TypeCode.Single:
  194. case TypeCode.Double:
  195. // return typeof (double);
  196. return typeof (decimal);
  197. case TypeCode.Decimal:
  198. return typeof (decimal);
  199. default:
  200. return t;
  201. }
  202. }
  203. }
  204. }