2
0

JsonQueryStringConverter.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. case TypeCode.Char:
  87. return parameter != null ? Char.Parse (parameter): default (char);
  88. case TypeCode.SByte:
  89. return parameter != null ? SByte.Parse (parameter, CultureInfo.InvariantCulture): default (sbyte);
  90. case TypeCode.Byte:
  91. return parameter != null ? Byte.Parse (parameter, CultureInfo.InvariantCulture): default (byte);
  92. case TypeCode.Int16:
  93. return parameter != null ? Int16.Parse (parameter, CultureInfo.InvariantCulture): default (short);
  94. case TypeCode.Int32:
  95. return parameter != null ? Int32.Parse (parameter, CultureInfo.InvariantCulture): default (int);
  96. case TypeCode.Int64:
  97. return parameter != null ? Int64.Parse (parameter, CultureInfo.InvariantCulture): default (long);
  98. case TypeCode.UInt16:
  99. return parameter != null ? UInt16.Parse (parameter, CultureInfo.InvariantCulture): default (ushort);
  100. case TypeCode.UInt32:
  101. return parameter != null ? UInt32.Parse (parameter, CultureInfo.InvariantCulture): default (uint);
  102. case TypeCode.UInt64:
  103. return parameter != null ? UInt64.Parse (parameter, CultureInfo.InvariantCulture): default (ulong);
  104. case TypeCode.DateTime:
  105. return parameter != null ? DateTime.Parse (parameter, CultureInfo.InvariantCulture): default (DateTime);
  106. case TypeCode.Boolean:
  107. return parameter != null ? Boolean.Parse (parameter): default (bool);
  108. case TypeCode.Single:
  109. return parameter != null ? Single.Parse (parameter, CultureInfo.InvariantCulture): default (float);
  110. case TypeCode.Double:
  111. return parameter != null ? Double.Parse (parameter, CultureInfo.InvariantCulture): default (double);
  112. case TypeCode.Decimal:
  113. return parameter != null ? Decimal.Parse (parameter, CultureInfo.InvariantCulture): default (decimal);
  114. }
  115. if (parameter == null)
  116. return null;
  117. DataContractJsonSerializer serializer =
  118. new DataContractJsonSerializer (parameterType);
  119. // hmm, it costs so silly though.
  120. return serializer.ReadObject (new MemoryStream (Encoding.UTF8.GetBytes (parameter)));
  121. }
  122. bool IsKnownType (Type t)
  123. {
  124. switch (Type.GetTypeCode (t)) {
  125. case TypeCode.Object:
  126. if (t == typeof (Guid) ||
  127. t == typeof (DBNull) ||
  128. t == typeof (DateTimeOffset) ||
  129. t == typeof (TimeSpan) ||
  130. t == typeof (XmlQualifiedName))
  131. return true;
  132. return false;
  133. default:
  134. return true;
  135. }
  136. }
  137. public override string ConvertValueToString (object parameter, Type parameterType)
  138. {
  139. if (parameterType == null)
  140. throw new ArgumentNullException ("parameterType");
  141. if (!CanConvert (parameterType))
  142. throw new NotSupportedException (String.Format ("Conversion from the argument parameterType '{0}' is not supported", parameterType));
  143. if (parameter == null)
  144. return null;
  145. if (parameter is DBNull)
  146. return "{}";
  147. parameterType = ToActualType (parameterType);
  148. if (parameter is IConvertible)
  149. parameter = ((IConvertible) parameter).ToType (parameterType, CultureInfo.InvariantCulture);
  150. switch (Type.GetTypeCode (parameterType)) {
  151. case TypeCode.String:
  152. string s = parameter is IFormattable ?
  153. ((IFormattable) parameter).ToString (null, CultureInfo.InvariantCulture) :
  154. parameter.ToString ();
  155. StringBuilder sb = new StringBuilder (s);
  156. sb.Replace ("\"", "\\\"");
  157. sb.Insert (0, '\"');
  158. sb.Append ('\"');
  159. return sb.ToString ();
  160. default:
  161. if (parameterType == typeof (XmlQualifiedName)) {
  162. var qname = (XmlQualifiedName) parameter;
  163. return String.Concat ("\"", qname.Name, ":", qname.Namespace, "\"");
  164. }
  165. return parameter.ToString ();
  166. }
  167. throw new NotImplementedException ();
  168. }
  169. Type ToActualType (Type t)
  170. {
  171. switch (Type.GetTypeCode (t)) {
  172. case TypeCode.DBNull: // though DBNull.Value input is converted to "{}". This result is used for String input.
  173. case TypeCode.Char:
  174. case TypeCode.String:
  175. return typeof (string);
  176. case TypeCode.SByte:
  177. case TypeCode.Int16:
  178. case TypeCode.Int32:
  179. case TypeCode.Int64:
  180. // return typeof (long);
  181. return typeof (decimal);
  182. case TypeCode.Byte:
  183. case TypeCode.UInt16:
  184. case TypeCode.UInt32:
  185. case TypeCode.UInt64:
  186. // return typeof (ulong);
  187. return typeof (decimal);
  188. case TypeCode.DateTime:
  189. case TypeCode.Boolean:
  190. return t;
  191. case TypeCode.Single:
  192. case TypeCode.Double:
  193. // return typeof (double);
  194. return typeof (decimal);
  195. case TypeCode.Decimal:
  196. return typeof (decimal);
  197. default:
  198. return t;
  199. }
  200. }
  201. }
  202. }