JavaScriptConvert.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #region License
  2. // Copyright 2006 James Newton-King
  3. // http://www.newtonsoft.com
  4. //
  5. // This work is licensed under the Creative Commons Attribution 2.5 License
  6. // http://creativecommons.org/licenses/by/2.5/
  7. //
  8. // You are free:
  9. // * to copy, distribute, display, and perform the work
  10. // * to make derivative works
  11. // * to make commercial use of the work
  12. //
  13. // Under the following conditions:
  14. // * For any reuse or distribution, you must make clear to others the license terms of this work.
  15. // * Any of these conditions can be waived if you get permission from the copyright holder.
  16. #endregion
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Text;
  20. using System.Web.UI.WebControls;
  21. using System.Collections;
  22. using System.IO;
  23. using System.Globalization;
  24. using System.Runtime.Serialization;
  25. using System.Reflection;
  26. using System.Data.SqlTypes;
  27. using Newtonsoft.Json.Utilities;
  28. using System.Xml;
  29. namespace Newtonsoft.Json
  30. {
  31. /// <summary>
  32. /// Provides methods for converting between common language runtime types and JavaScript types.
  33. /// </summary>
  34. static class JavaScriptConvert
  35. {
  36. /// <summary>
  37. /// Represents JavaScript's boolean value true as a string. This field is read-only.
  38. /// </summary>
  39. public static readonly string True;
  40. /// <summary>
  41. /// Represents JavaScript's boolean value false as a string. This field is read-only.
  42. /// </summary>
  43. public static readonly string False;
  44. /// <summary>
  45. /// Represents JavaScript's null as a string. This field is read-only.
  46. /// </summary>
  47. public static readonly string Null;
  48. /// <summary>
  49. /// Represents JavaScript's undefined as a string. This field is read-only.
  50. /// </summary>
  51. public static readonly string Undefined;
  52. internal static readonly long InitialJavaScriptDateTicks;
  53. internal static readonly DateTime MinimumJavaScriptDate;
  54. static JavaScriptConvert()
  55. {
  56. True = "true";
  57. False = "false";
  58. Null = "null";
  59. Undefined = "undefined";
  60. InitialJavaScriptDateTicks = (new DateTime(1970, 1, 1)).Ticks;
  61. MinimumJavaScriptDate = new DateTime(100, 1, 1);
  62. }
  63. /// <summary>
  64. /// Converts the <see cref="DateTime"/> to it's JavaScript string representation.
  65. /// </summary>
  66. /// <param name="value">The value to convert.</param>
  67. /// <returns>A Json string representation of the <see cref="DateTime"/>.</returns>
  68. public static string ToString(DateTime value)
  69. {
  70. long javaScriptTicks = ConvertDateTimeToJavaScriptTicks(value);
  71. return "new Date(" + javaScriptTicks + ")";
  72. }
  73. internal static long ConvertDateTimeToJavaScriptTicks(DateTime dateTime)
  74. {
  75. if (dateTime < MinimumJavaScriptDate)
  76. dateTime = MinimumJavaScriptDate;
  77. long javaScriptTicks = (dateTime.Ticks - InitialJavaScriptDateTicks) / (long)10000;
  78. return javaScriptTicks;
  79. }
  80. internal static DateTime ConvertJavaScriptTicksToDateTime(long javaScriptTicks)
  81. {
  82. DateTime dateTime = new DateTime((javaScriptTicks * 10000) + InitialJavaScriptDateTicks);
  83. return dateTime;
  84. }
  85. /// <summary>
  86. /// Converts the <see cref="Boolean"/> to it's JavaScript string representation.
  87. /// </summary>
  88. /// <param name="value">The value to convert.</param>
  89. /// <returns>A Json string representation of the <see cref="Boolean"/>.</returns>
  90. public static string ToString(bool value)
  91. {
  92. return (value) ? True : False;
  93. }
  94. /// <summary>
  95. /// Converts the <see cref="Char"/> to it's JavaScript string representation.
  96. /// </summary>
  97. /// <param name="value">The value to convert.</param>
  98. /// <returns>A Json string representation of the <see cref="Char"/>.</returns>
  99. public static string ToString(char value)
  100. {
  101. return value == '\0' ? Null : ToString (char.ToString (value));
  102. }
  103. /// <summary>
  104. /// Converts the <see cref="Enum"/> to it's JavaScript string representation.
  105. /// </summary>
  106. /// <param name="value">The value to convert.</param>
  107. /// <returns>A Json string representation of the <see cref="Enum"/>.</returns>
  108. public static string ToString(Enum value)
  109. {
  110. return value.ToString();
  111. }
  112. /// <summary>
  113. /// Converts the <see cref="Int32"/> to it's JavaScript string representation.
  114. /// </summary>
  115. /// <param name="value">The value to convert.</param>
  116. /// <returns>A Json string representation of the <see cref="Int32"/>.</returns>
  117. public static string ToString(int value)
  118. {
  119. return value.ToString(null, CultureInfo.InvariantCulture);
  120. }
  121. /// <summary>
  122. /// Converts the <see cref="Int16"/> to it's JavaScript string representation.
  123. /// </summary>
  124. /// <param name="value">The value to convert.</param>
  125. /// <returns>A Json string representation of the <see cref="Int16"/>.</returns>
  126. public static string ToString(short value)
  127. {
  128. return value.ToString(null, CultureInfo.InvariantCulture);
  129. }
  130. /// <summary>
  131. /// Converts the <see cref="UInt16"/> to it's JavaScript string representation.
  132. /// </summary>
  133. /// <param name="value">The value to convert.</param>
  134. /// <returns>A Json string representation of the <see cref="UInt16"/>.</returns>
  135. public static string ToString(ushort value)
  136. {
  137. return value.ToString(null, CultureInfo.InvariantCulture);
  138. }
  139. /// <summary>
  140. /// Converts the <see cref="UInt32"/> to it's JavaScript string representation.
  141. /// </summary>
  142. /// <param name="value">The value to convert.</param>
  143. /// <returns>A Json string representation of the <see cref="UInt32"/>.</returns>
  144. public static string ToString(uint value)
  145. {
  146. return value.ToString(null, CultureInfo.InvariantCulture);
  147. }
  148. /// <summary>
  149. /// Converts the <see cref="Int64"/> to it's JavaScript string representation.
  150. /// </summary>
  151. /// <param name="value">The value to convert.</param>
  152. /// <returns>A Json string representation of the <see cref="Int64"/>.</returns>
  153. public static string ToString(long value)
  154. {
  155. return value.ToString(null, CultureInfo.InvariantCulture);
  156. }
  157. /// <summary>
  158. /// Converts the <see cref="UInt64"/> to it's JavaScript string representation.
  159. /// </summary>
  160. /// <param name="value">The value to convert.</param>
  161. /// <returns>A Json string representation of the <see cref="UInt64"/>.</returns>
  162. public static string ToString(ulong value)
  163. {
  164. return value.ToString(null, CultureInfo.InvariantCulture);
  165. }
  166. /// <summary>
  167. /// Converts the <see cref="Single"/> to it's JavaScript string representation.
  168. /// </summary>
  169. /// <param name="value">The value to convert.</param>
  170. /// <returns>A Json string representation of the <see cref="Single"/>.</returns>
  171. public static string ToString(float value)
  172. {
  173. return value.ToString ("R", CultureInfo.InvariantCulture);
  174. }
  175. /// <summary>
  176. /// Converts the <see cref="Double"/> to it's JavaScript string representation.
  177. /// </summary>
  178. /// <param name="value">The value to convert.</param>
  179. /// <returns>A Json string representation of the <see cref="Double"/>.</returns>
  180. public static string ToString(double value)
  181. {
  182. return value.ToString ("R", CultureInfo.InvariantCulture);
  183. }
  184. /// <summary>
  185. /// Converts the <see cref="Byte"/> to it's JavaScript string representation.
  186. /// </summary>
  187. /// <param name="value">The value to convert.</param>
  188. /// <returns>A Json string representation of the <see cref="Byte"/>.</returns>
  189. public static string ToString(byte value)
  190. {
  191. return value.ToString(null, CultureInfo.InvariantCulture);
  192. }
  193. /// <summary>
  194. /// Converts the <see cref="SByte"/> to it's JavaScript string representation.
  195. /// </summary>
  196. /// <param name="value">The value to convert.</param>
  197. /// <returns>A Json string representation of the <see cref="SByte"/>.</returns>
  198. public static string ToString(sbyte value)
  199. {
  200. return value.ToString(null, CultureInfo.InvariantCulture);
  201. }
  202. /// <summary>
  203. /// Converts the <see cref="Decimal"/> to it's JavaScript string representation.
  204. /// </summary>
  205. /// <param name="value">The value to convert.</param>
  206. /// <returns>A Json string representation of the <see cref="SByte"/>.</returns>
  207. public static string ToString(decimal value)
  208. {
  209. return value.ToString(null, CultureInfo.InvariantCulture);
  210. }
  211. /// <summary>
  212. /// Converts the <see cref="Guid"/> to it's JavaScript string representation.
  213. /// </summary>
  214. /// <param name="value">The value to convert.</param>
  215. /// <returns>A Json string representation of the <see cref="Guid"/>.</returns>
  216. //public static string ToString(Guid value)
  217. //{
  218. // return '"' + value.ToString("D", CultureInfo.InvariantCulture) + '"';
  219. //}
  220. /// <summary>
  221. /// Converts the <see cref="String"/> to it's JavaScript string representation.
  222. /// </summary>
  223. /// <param name="value">The value to convert.</param>
  224. /// <returns>A Json string representation of the <see cref="String"/>.</returns>
  225. public static string ToString(string value)
  226. {
  227. return ToString(value, '"');
  228. }
  229. /// <summary>
  230. /// Converts the <see cref="String"/> to it's JavaScript string representation.
  231. /// </summary>
  232. /// <param name="value">The value to convert.</param>
  233. /// <param name="delimter">The string delimiter character.</param>
  234. /// <returns>A Json string representation of the <see cref="String"/>.</returns>
  235. public static string ToString(string value, char delimter)
  236. {
  237. return JavaScriptUtils.EscapeJavaScriptString(value, delimter, true);
  238. }
  239. }
  240. }