JavaScriptConvert.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #region License
  2. // Copyright (c) 2007 James Newton-King
  3. //
  4. // Permission is hereby granted, free of charge, to any person
  5. // obtaining a copy of this software and associated documentation
  6. // files (the "Software"), to deal in the Software without
  7. // restriction, including without limitation the rights to use,
  8. // copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the
  10. // Software is furnished to do so, subject to the following
  11. // conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be
  14. // included in all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  18. // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  20. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. // OTHER DEALINGS IN THE SOFTWARE.
  24. #endregion
  25. using System;
  26. using System.Collections.Generic;
  27. using System.Text;
  28. using System.Web.UI.WebControls;
  29. using System.Collections;
  30. using System.IO;
  31. using System.Globalization;
  32. using System.Runtime.Serialization;
  33. using System.Reflection;
  34. using System.Data.SqlTypes;
  35. using Newtonsoft.Json.Utilities;
  36. using System.Xml;
  37. namespace Newtonsoft.Json
  38. {
  39. /// <summary>
  40. /// Provides methods for converting between common language runtime types and JavaScript types.
  41. /// </summary>
  42. static class JavaScriptConvert
  43. {
  44. /// <summary>
  45. /// Represents JavaScript's boolean value true as a string. This field is read-only.
  46. /// </summary>
  47. public static readonly string True;
  48. /// <summary>
  49. /// Represents JavaScript's boolean value false as a string. This field is read-only.
  50. /// </summary>
  51. public static readonly string False;
  52. /// <summary>
  53. /// Represents JavaScript's null as a string. This field is read-only.
  54. /// </summary>
  55. public static readonly string Null;
  56. /// <summary>
  57. /// Represents JavaScript's undefined as a string. This field is read-only.
  58. /// </summary>
  59. public static readonly string Undefined;
  60. internal static readonly long InitialJavaScriptDateTicks;
  61. internal static readonly DateTime MinimumJavaScriptDate;
  62. static JavaScriptConvert()
  63. {
  64. True = "true";
  65. False = "false";
  66. Null = "null";
  67. Undefined = "undefined";
  68. InitialJavaScriptDateTicks = new DateTime (1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).Ticks;
  69. MinimumJavaScriptDate = new DateTime (100, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  70. }
  71. /// <summary>
  72. /// Converts the <see cref="DateTime"/> to it's JavaScript string representation.
  73. /// </summary>
  74. /// <param name="value">The value to convert.</param>
  75. /// <returns>A Json string representation of the <see cref="DateTime"/>.</returns>
  76. public static string ToString(DateTime value)
  77. {
  78. long javaScriptTicks = ConvertDateTimeToJavaScriptTicks(value);
  79. return @"""\/Date(" + javaScriptTicks + @")\/""";
  80. }
  81. internal static long ConvertDateTimeToJavaScriptTicks(DateTime dateTime)
  82. {
  83. dateTime = dateTime.ToUniversalTime ();
  84. if (dateTime < MinimumJavaScriptDate)
  85. dateTime = MinimumJavaScriptDate;
  86. long javaScriptTicks = (dateTime.Ticks - InitialJavaScriptDateTicks) / (long)10000;
  87. return javaScriptTicks;
  88. }
  89. internal static DateTime ConvertJavaScriptTicksToDateTime(long javaScriptTicks)
  90. {
  91. return new DateTime((javaScriptTicks * 10000) + InitialJavaScriptDateTicks, DateTimeKind.Utc);
  92. }
  93. /// <summary>
  94. /// Converts the <see cref="Boolean"/> to it's JavaScript string representation.
  95. /// </summary>
  96. /// <param name="value">The value to convert.</param>
  97. /// <returns>A Json string representation of the <see cref="Boolean"/>.</returns>
  98. public static string ToString(bool value)
  99. {
  100. return (value) ? True : False;
  101. }
  102. /// <summary>
  103. /// Converts the <see cref="Char"/> to it's JavaScript string representation.
  104. /// </summary>
  105. /// <param name="value">The value to convert.</param>
  106. /// <returns>A Json string representation of the <see cref="Char"/>.</returns>
  107. public static void WriteChar (char value, TextWriter writer) {
  108. if (value == '\0')
  109. writer.Write(Null);
  110. else
  111. JavaScriptUtils.WriteEscapedJavaScriptChar (value, '"', true, writer);
  112. }
  113. /// <summary>
  114. /// Converts the <see cref="Enum"/> to it's JavaScript string representation.
  115. /// </summary>
  116. /// <param name="value">The value to convert.</param>
  117. /// <returns>A Json string representation of the <see cref="Enum"/>.</returns>
  118. public static string ToString(Enum value)
  119. {
  120. return value.ToString ();
  121. }
  122. /// <summary>
  123. /// Converts the <see cref="Int32"/> to it's JavaScript string representation.
  124. /// </summary>
  125. /// <param name="value">The value to convert.</param>
  126. /// <returns>A Json string representation of the <see cref="Int32"/>.</returns>
  127. public static string ToString(int value)
  128. {
  129. return value.ToString(CultureInfo.InvariantCulture);
  130. }
  131. /// <summary>
  132. /// Converts the <see cref="Int16"/> to it's JavaScript string representation.
  133. /// </summary>
  134. /// <param name="value">The value to convert.</param>
  135. /// <returns>A Json string representation of the <see cref="Int16"/>.</returns>
  136. public static string ToString(short value)
  137. {
  138. return value.ToString(CultureInfo.InvariantCulture);
  139. }
  140. /// <summary>
  141. /// Converts the <see cref="UInt16"/> to it's JavaScript string representation.
  142. /// </summary>
  143. /// <param name="value">The value to convert.</param>
  144. /// <returns>A Json string representation of the <see cref="UInt16"/>.</returns>
  145. public static string ToString(ushort value)
  146. {
  147. return value.ToString(CultureInfo.InvariantCulture);
  148. }
  149. /// <summary>
  150. /// Converts the <see cref="UInt32"/> to it's JavaScript string representation.
  151. /// </summary>
  152. /// <param name="value">The value to convert.</param>
  153. /// <returns>A Json string representation of the <see cref="UInt32"/>.</returns>
  154. public static string ToString(uint value)
  155. {
  156. return value.ToString(CultureInfo.InvariantCulture);
  157. }
  158. /// <summary>
  159. /// Converts the <see cref="Int64"/> to it's JavaScript string representation.
  160. /// </summary>
  161. /// <param name="value">The value to convert.</param>
  162. /// <returns>A Json string representation of the <see cref="Int64"/>.</returns>
  163. public static string ToString(long value)
  164. {
  165. return value.ToString(CultureInfo.InvariantCulture);
  166. }
  167. /// <summary>
  168. /// Converts the <see cref="UInt64"/> to it's JavaScript string representation.
  169. /// </summary>
  170. /// <param name="value">The value to convert.</param>
  171. /// <returns>A Json string representation of the <see cref="UInt64"/>.</returns>
  172. public static string ToString(ulong value)
  173. {
  174. return value.ToString(CultureInfo.InvariantCulture);
  175. }
  176. /// <summary>
  177. /// Converts the <see cref="Single"/> to it's JavaScript string representation.
  178. /// </summary>
  179. /// <param name="value">The value to convert.</param>
  180. /// <returns>A Json string representation of the <see cref="Single"/>.</returns>
  181. public static string ToString(float value)
  182. {
  183. return value.ToString ("R", CultureInfo.InvariantCulture);
  184. }
  185. /// <summary>
  186. /// Converts the <see cref="Double"/> to it's JavaScript string representation.
  187. /// </summary>
  188. /// <param name="value">The value to convert.</param>
  189. /// <returns>A Json string representation of the <see cref="Double"/>.</returns>
  190. public static string ToString(double value)
  191. {
  192. return value.ToString ("R", CultureInfo.InvariantCulture);
  193. }
  194. /// <summary>
  195. /// Converts the <see cref="Byte"/> to it's JavaScript string representation.
  196. /// </summary>
  197. /// <param name="value">The value to convert.</param>
  198. /// <returns>A Json string representation of the <see cref="Byte"/>.</returns>
  199. public static string ToString(byte value)
  200. {
  201. return value.ToString(CultureInfo.InvariantCulture);
  202. }
  203. /// <summary>
  204. /// Converts the <see cref="SByte"/> to it's JavaScript string representation.
  205. /// </summary>
  206. /// <param name="value">The value to convert.</param>
  207. /// <returns>A Json string representation of the <see cref="SByte"/>.</returns>
  208. public static string ToString(sbyte value)
  209. {
  210. return value.ToString(CultureInfo.InvariantCulture);
  211. }
  212. /// <summary>
  213. /// Converts the <see cref="Decimal"/> to it's JavaScript string representation.
  214. /// </summary>
  215. /// <param name="value">The value to convert.</param>
  216. /// <returns>A Json string representation of the <see cref="SByte"/>.</returns>
  217. public static string ToString(decimal value)
  218. {
  219. return value.ToString(CultureInfo.InvariantCulture);
  220. }
  221. /// <summary>
  222. /// Converts the <see cref="Guid"/> to it's JavaScript string representation.
  223. /// </summary>
  224. /// <param name="value">The value to convert.</param>
  225. /// <returns>A Json string representation of the <see cref="Guid"/>.</returns>
  226. //public static string ToString(Guid value)
  227. //{
  228. // return '"' + value.ToString("D", CultureInfo.InvariantCulture) + '"';
  229. //}
  230. /// <summary>
  231. /// Converts the <see cref="String"/> to it's JavaScript string representation.
  232. /// </summary>
  233. /// <param name="value">The value to convert.</param>
  234. /// <returns>A Json string representation of the <see cref="String"/>.</returns>
  235. public static void WriteString (string value, TextWriter writer)
  236. {
  237. WriteString (value, '"', writer);
  238. }
  239. /// <summary>
  240. /// Converts the <see cref="String"/> to it's JavaScript string representation.
  241. /// </summary>
  242. /// <param name="value">The value to convert.</param>
  243. /// <param name="delimter">The string delimiter character.</param>
  244. /// <returns>A Json string representation of the <see cref="String"/>.</returns>
  245. public static void WriteString (string value, char delimter, TextWriter writer)
  246. {
  247. JavaScriptUtils.WriteEscapedJavaScriptString(value, delimter, true, writer);
  248. }
  249. }
  250. }