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. // FIXME: it should return false for things like List<OfPrivateType>.
  63. return type.IsPublic || type.IsNestedPublic;
  64. default:
  65. return true;
  66. }
  67. }
  68. public override object ConvertStringToValue (string parameter, Type parameterType)
  69. {
  70. if (parameterType == null)
  71. throw new ArgumentNullException ("parameterType");
  72. if (!CanConvert (parameterType))
  73. throw new NotSupportedException (String.Format ("Conversion from the argument parameterType '{0}' is not supported", parameterType));
  74. // In general .NET JSON parser is sloppy. It accepts
  75. // such a string that is actually invalid in terms of
  76. // the target type in JSON context.
  77. switch (Type.GetTypeCode (parameterType)) {
  78. case TypeCode.String:
  79. // LAMESPEC LAMESPEC LAMESPEC: we cannot give "foo" as the string value input (even if they are escaped as %22!)
  80. if (parameter == null)
  81. return null;
  82. if (parameter.Length > 1 && parameter [0] == '"' && parameter [parameter.Length - 1] == '"')
  83. return parameter.Substring (1, parameter.Length - 2);
  84. else if (parameter [0] != '"')
  85. return parameter;
  86. break;
  87. #if !NET_2_1
  88. case TypeCode.Char:
  89. return parameter != null ? Char.Parse (parameter): default (char);
  90. #endif
  91. case TypeCode.SByte:
  92. return parameter != null ? SByte.Parse (parameter, CultureInfo.InvariantCulture): default (sbyte);
  93. case TypeCode.Byte:
  94. return parameter != null ? Byte.Parse (parameter, CultureInfo.InvariantCulture): default (byte);
  95. case TypeCode.Int16:
  96. return parameter != null ? Int16.Parse (parameter, CultureInfo.InvariantCulture): default (short);
  97. case TypeCode.Int32:
  98. return parameter != null ? Int32.Parse (parameter, CultureInfo.InvariantCulture): default (int);
  99. case TypeCode.Int64:
  100. return parameter != null ? Int64.Parse (parameter, CultureInfo.InvariantCulture): default (long);
  101. case TypeCode.UInt16:
  102. return parameter != null ? UInt16.Parse (parameter, CultureInfo.InvariantCulture): default (ushort);
  103. case TypeCode.UInt32:
  104. return parameter != null ? UInt32.Parse (parameter, CultureInfo.InvariantCulture): default (uint);
  105. case TypeCode.UInt64:
  106. return parameter != null ? UInt64.Parse (parameter, CultureInfo.InvariantCulture): default (ulong);
  107. case TypeCode.DateTime:
  108. return parameter != null ? DateTime.Parse (parameter, CultureInfo.InvariantCulture): default (DateTime);
  109. case TypeCode.Boolean:
  110. return parameter != null ? Boolean.Parse (parameter): default (bool);
  111. case TypeCode.Single:
  112. return parameter != null ? Single.Parse (parameter, CultureInfo.InvariantCulture): default (float);
  113. case TypeCode.Double:
  114. return parameter != null ? Double.Parse (parameter, CultureInfo.InvariantCulture): default (double);
  115. case TypeCode.Decimal:
  116. return parameter != null ? Decimal.Parse (parameter, CultureInfo.InvariantCulture): default (decimal);
  117. }
  118. if (parameter == null)
  119. return null;
  120. DataContractJsonSerializer serializer =
  121. new DataContractJsonSerializer (parameterType);
  122. // hmm, it costs so silly though.
  123. return serializer.ReadObject (new MemoryStream (Encoding.UTF8.GetBytes (parameter)));
  124. }
  125. bool IsKnownType (Type t)
  126. {
  127. switch (Type.GetTypeCode (t)) {
  128. case TypeCode.Object:
  129. if (t == typeof (Guid) ||
  130. t == typeof (DBNull) ||
  131. t == typeof (DateTimeOffset) ||
  132. t == typeof (TimeSpan) ||
  133. t == typeof (XmlQualifiedName))
  134. return true;
  135. return false;
  136. default:
  137. return true;
  138. }
  139. }
  140. public override string ConvertValueToString (object parameter, Type parameterType)
  141. {
  142. if (parameterType == null)
  143. throw new ArgumentNullException ("parameterType");
  144. if (!CanConvert (parameterType))
  145. throw new NotSupportedException (String.Format ("Conversion from the argument parameterType '{0}' is not supported", parameterType));
  146. if (parameter == null)
  147. return null;
  148. if (parameter is DBNull)
  149. return "{}";
  150. parameterType = ToActualType (parameterType);
  151. if (parameter is IConvertible)
  152. parameter = ((IConvertible) parameter).ToType (parameterType, CultureInfo.InvariantCulture);
  153. switch (Type.GetTypeCode (parameterType)) {
  154. case TypeCode.String:
  155. string s = parameter is IFormattable ?
  156. ((IFormattable) parameter).ToString (null, CultureInfo.InvariantCulture) :
  157. parameter.ToString ();
  158. StringBuilder sb = new StringBuilder (s);
  159. sb.Replace ("\"", "\\\"");
  160. sb.Insert (0, "\"");
  161. sb.Append ('\"');
  162. return sb.ToString ();
  163. default:
  164. if (parameterType == typeof (XmlQualifiedName)) {
  165. var qname = (XmlQualifiedName) parameter;
  166. return String.Concat ("\"", qname.Name, ":", qname.Namespace, "\"");
  167. }
  168. return parameter.ToString ();
  169. }
  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. }