JsonQueryStringConverter.cs 7.9 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. // Copyright 2014 Xamarin Inc. (http://www.xamarin.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Globalization;
  31. using System.IO;
  32. using System.Runtime.Serialization.Json;
  33. using System.ServiceModel;
  34. using System.ServiceModel.Description;
  35. using System.Text;
  36. using System.Xml;
  37. namespace System.ServiceModel.Dispatcher
  38. {
  39. public class JsonQueryStringConverter : QueryStringConverter
  40. {
  41. DataContractJsonSerializer serializer = new DataContractJsonSerializer (typeof (object));
  42. internal string CustomWrapperName { get; set; }
  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. // FIXME: it should return false for things like List<OfPrivateType>.
  64. return type.IsPublic || type.IsNestedPublic;
  65. default:
  66. return true;
  67. }
  68. }
  69. public override object ConvertStringToValue (string parameter, Type parameterType)
  70. {
  71. if (parameterType == null)
  72. throw new ArgumentNullException ("parameterType");
  73. if (!CanConvert (parameterType))
  74. throw new NotSupportedException (String.Format ("Conversion from the argument parameterType '{0}' is not supported", parameterType));
  75. // In general .NET JSON parser is sloppy. It accepts
  76. // such a string that is actually invalid in terms of
  77. // the target type in JSON context.
  78. switch (Type.GetTypeCode (parameterType)) {
  79. case TypeCode.String:
  80. // LAMESPEC LAMESPEC LAMESPEC: we cannot give "foo" as the string value input (even if they are escaped as %22!)
  81. if (parameter == null)
  82. return null;
  83. if (parameter.Length > 1 && parameter [0] == '"' && parameter [parameter.Length - 1] == '"')
  84. return parameter.Substring (1, parameter.Length - 2);
  85. else if (parameter [0] != '"')
  86. return parameter;
  87. break;
  88. case TypeCode.Char:
  89. return parameter != null ? Char.Parse (parameter): default (char);
  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. var f = (parameter as IFormattable);
  168. return (f == null) ? parameter.ToString () : f.ToString (null, CultureInfo.InvariantCulture);
  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. }