JsonQueryStringConverter.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. public override bool CanConvert (Type type)
  42. {
  43. // almost copy from QueryStringConverter, except that DBNull and XmlQualifiedName are supported
  44. switch (Type.GetTypeCode (type)) {
  45. //case TypeCode.DBNull:
  46. case TypeCode.Empty:
  47. return false;
  48. case TypeCode.Object:
  49. if (type == typeof (TimeSpan))
  50. return true;
  51. if (type == typeof (DateTimeOffset))
  52. return true;
  53. if (type == typeof (Guid))
  54. return true;
  55. if (type == typeof (XmlQualifiedName))
  56. return true;
  57. if (type == typeof (object))
  58. return true;
  59. // if (type.GetCustomAttributes (typeof (TypeConverterAttribute), true).Length > 0)
  60. // return true;
  61. return false;
  62. default:
  63. return true;
  64. }
  65. }
  66. public override object ConvertStringToValue (string parameter, Type parameterType)
  67. {
  68. if (parameterType == null)
  69. throw new ArgumentNullException ("parameterType");
  70. if (!CanConvert (parameterType))
  71. throw new NotSupportedException (String.Format ("Conversion from the argument parameterType '{0}' is not supported", parameterType));
  72. // In general .NET JSON parser is sloppy. It accepts
  73. // such a string that is actually invalid in terms of
  74. // the target type in JSON context.
  75. switch (Type.GetTypeCode (parameterType)) {
  76. //case TypeCode.String:
  77. // return parameter;
  78. case TypeCode.Char:
  79. return parameter != null ? Char.Parse (parameter): default (char);
  80. case TypeCode.SByte:
  81. return parameter != null ? SByte.Parse (parameter, CultureInfo.InvariantCulture): default (sbyte);
  82. case TypeCode.Byte:
  83. return parameter != null ? Byte.Parse (parameter, CultureInfo.InvariantCulture): default (byte);
  84. case TypeCode.Int16:
  85. return parameter != null ? Int16.Parse (parameter, CultureInfo.InvariantCulture): default (short);
  86. case TypeCode.Int32:
  87. return parameter != null ? Int32.Parse (parameter, CultureInfo.InvariantCulture): default (int);
  88. case TypeCode.Int64:
  89. return parameter != null ? Int64.Parse (parameter, CultureInfo.InvariantCulture): default (long);
  90. case TypeCode.UInt16:
  91. return parameter != null ? UInt16.Parse (parameter, CultureInfo.InvariantCulture): default (ushort);
  92. case TypeCode.UInt32:
  93. return parameter != null ? UInt32.Parse (parameter, CultureInfo.InvariantCulture): default (uint);
  94. case TypeCode.UInt64:
  95. return parameter != null ? UInt64.Parse (parameter, CultureInfo.InvariantCulture): default (ulong);
  96. case TypeCode.DateTime:
  97. return parameter != null ? DateTime.Parse (parameter, CultureInfo.InvariantCulture): default (DateTime);
  98. case TypeCode.Boolean:
  99. return parameter != null ? Boolean.Parse (parameter): default (bool);
  100. case TypeCode.Single:
  101. return parameter != null ? Single.Parse (parameter, CultureInfo.InvariantCulture): default (float);
  102. case TypeCode.Double:
  103. return parameter != null ? Double.Parse (parameter, CultureInfo.InvariantCulture): default (double);
  104. case TypeCode.Decimal:
  105. return parameter != null ? Decimal.Parse (parameter, CultureInfo.InvariantCulture): default (decimal);
  106. }
  107. if (parameter == null)
  108. return null;
  109. DataContractJsonSerializer serializer =
  110. new DataContractJsonSerializer (parameterType);
  111. // hmm, it costs so silly though.
  112. return serializer.ReadObject (new MemoryStream (Encoding.UTF8.GetBytes (parameter)));
  113. }
  114. bool IsKnownType (Type t)
  115. {
  116. switch (Type.GetTypeCode (t)) {
  117. case TypeCode.Object:
  118. if (t == typeof (Guid) ||
  119. t == typeof (DBNull) ||
  120. t == typeof (DateTimeOffset) ||
  121. t == typeof (TimeSpan) ||
  122. t == typeof (XmlQualifiedName))
  123. return true;
  124. return false;
  125. default:
  126. return true;
  127. }
  128. }
  129. public override string ConvertValueToString (object parameter, Type parameterType)
  130. {
  131. if (parameterType == null)
  132. throw new ArgumentNullException ("parameterType");
  133. if (!CanConvert (parameterType))
  134. throw new NotSupportedException (String.Format ("Conversion from the argument parameterType '{0}' is not supported", parameterType));
  135. if (parameter == null)
  136. return null;
  137. if (parameter is DBNull)
  138. return "{}";
  139. parameterType = ToActualType (parameterType);
  140. if (parameter is IConvertible)
  141. parameter = ((IConvertible) parameter).ToType (parameterType, CultureInfo.InvariantCulture);
  142. switch (Type.GetTypeCode (parameterType)) {
  143. case TypeCode.String:
  144. string s = parameter is IFormattable ?
  145. ((IFormattable) parameter).ToString (null, CultureInfo.InvariantCulture) :
  146. parameter.ToString ();
  147. StringBuilder sb = new StringBuilder (s);
  148. sb.Replace ("\"", "\\\"");
  149. sb.Insert (0, '\"');
  150. sb.Append ('\"');
  151. return sb.ToString ();
  152. default:
  153. if (parameterType == typeof (XmlQualifiedName)) {
  154. var qname = (XmlQualifiedName) parameter;
  155. return String.Concat ("\"", qname.Name, ":", qname.Namespace, "\"");
  156. }
  157. return parameter.ToString ();
  158. }
  159. throw new NotImplementedException ();
  160. }
  161. Type ToActualType (Type t)
  162. {
  163. switch (Type.GetTypeCode (t)) {
  164. case TypeCode.DBNull: // though DBNull.Value input is converted to "{}". This result is used for String input.
  165. case TypeCode.Char:
  166. case TypeCode.String:
  167. return typeof (string);
  168. case TypeCode.SByte:
  169. case TypeCode.Int16:
  170. case TypeCode.Int32:
  171. case TypeCode.Int64:
  172. // return typeof (long);
  173. return typeof (decimal);
  174. case TypeCode.Byte:
  175. case TypeCode.UInt16:
  176. case TypeCode.UInt32:
  177. case TypeCode.UInt64:
  178. // return typeof (ulong);
  179. return typeof (decimal);
  180. case TypeCode.DateTime:
  181. case TypeCode.Boolean:
  182. return t;
  183. case TypeCode.Single:
  184. case TypeCode.Double:
  185. // return typeof (double);
  186. return typeof (decimal);
  187. case TypeCode.Decimal:
  188. return typeof (decimal);
  189. default:
  190. return t;
  191. }
  192. }
  193. }
  194. }