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. // 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. case TypeCode.Char:
  88. return parameter != null ? Char.Parse (parameter): default (char);
  89. case TypeCode.SByte:
  90. return parameter != null ? SByte.Parse (parameter, CultureInfo.InvariantCulture): default (sbyte);
  91. case TypeCode.Byte:
  92. return parameter != null ? Byte.Parse (parameter, CultureInfo.InvariantCulture): default (byte);
  93. case TypeCode.Int16:
  94. return parameter != null ? Int16.Parse (parameter, CultureInfo.InvariantCulture): default (short);
  95. case TypeCode.Int32:
  96. return parameter != null ? Int32.Parse (parameter, CultureInfo.InvariantCulture): default (int);
  97. case TypeCode.Int64:
  98. return parameter != null ? Int64.Parse (parameter, CultureInfo.InvariantCulture): default (long);
  99. case TypeCode.UInt16:
  100. return parameter != null ? UInt16.Parse (parameter, CultureInfo.InvariantCulture): default (ushort);
  101. case TypeCode.UInt32:
  102. return parameter != null ? UInt32.Parse (parameter, CultureInfo.InvariantCulture): default (uint);
  103. case TypeCode.UInt64:
  104. return parameter != null ? UInt64.Parse (parameter, CultureInfo.InvariantCulture): default (ulong);
  105. case TypeCode.DateTime:
  106. return parameter != null ? DateTime.Parse (parameter, CultureInfo.InvariantCulture): default (DateTime);
  107. case TypeCode.Boolean:
  108. return parameter != null ? Boolean.Parse (parameter): default (bool);
  109. case TypeCode.Single:
  110. return parameter != null ? Single.Parse (parameter, CultureInfo.InvariantCulture): default (float);
  111. case TypeCode.Double:
  112. return parameter != null ? Double.Parse (parameter, CultureInfo.InvariantCulture): default (double);
  113. case TypeCode.Decimal:
  114. return parameter != null ? Decimal.Parse (parameter, CultureInfo.InvariantCulture): default (decimal);
  115. }
  116. if (parameter == null)
  117. return null;
  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. bool IsKnownType (Type t)
  124. {
  125. switch (Type.GetTypeCode (t)) {
  126. case TypeCode.Object:
  127. if (t == typeof (Guid) ||
  128. t == typeof (DBNull) ||
  129. t == typeof (DateTimeOffset) ||
  130. t == typeof (TimeSpan) ||
  131. t == typeof (XmlQualifiedName))
  132. return true;
  133. return false;
  134. default:
  135. return true;
  136. }
  137. }
  138. public override string ConvertValueToString (object parameter, Type parameterType)
  139. {
  140. if (parameterType == null)
  141. throw new ArgumentNullException ("parameterType");
  142. if (!CanConvert (parameterType))
  143. throw new NotSupportedException (String.Format ("Conversion from the argument parameterType '{0}' is not supported", parameterType));
  144. if (parameter == null)
  145. return null;
  146. if (parameter is DBNull)
  147. return "{}";
  148. parameterType = ToActualType (parameterType);
  149. if (parameter is IConvertible)
  150. parameter = ((IConvertible) parameter).ToType (parameterType, CultureInfo.InvariantCulture);
  151. switch (Type.GetTypeCode (parameterType)) {
  152. case TypeCode.String:
  153. string s = parameter is IFormattable ?
  154. ((IFormattable) parameter).ToString (null, CultureInfo.InvariantCulture) :
  155. parameter.ToString ();
  156. StringBuilder sb = new StringBuilder (s);
  157. sb.Replace ("\"", "\\\"");
  158. sb.Insert (0, "\"");
  159. sb.Append ('\"');
  160. return sb.ToString ();
  161. default:
  162. if (parameterType == typeof (XmlQualifiedName)) {
  163. var qname = (XmlQualifiedName) parameter;
  164. return String.Concat ("\"", qname.Name, ":", qname.Namespace, "\"");
  165. }
  166. return parameter.ToString ();
  167. }
  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. }