| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- #region License
- // Copyright 2006 James Newton-King
- // http://www.newtonsoft.com
- //
- // This work is licensed under the Creative Commons Attribution 2.5 License
- // http://creativecommons.org/licenses/by/2.5/
- //
- // You are free:
- // * to copy, distribute, display, and perform the work
- // * to make derivative works
- // * to make commercial use of the work
- //
- // Under the following conditions:
- // * For any reuse or distribution, you must make clear to others the license terms of this work.
- // * Any of these conditions can be waived if you get permission from the copyright holder.
- #endregion
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Web.UI.WebControls;
- using System.Collections;
- using System.IO;
- using System.Globalization;
- using System.Runtime.Serialization;
- using System.Reflection;
- using System.Data.SqlTypes;
- using Newtonsoft.Json.Utilities;
- using System.Xml;
- namespace Newtonsoft.Json
- {
- /// <summary>
- /// Provides methods for converting between common language runtime types and JavaScript types.
- /// </summary>
- static class JavaScriptConvert
- {
- /// <summary>
- /// Represents JavaScript's boolean value true as a string. This field is read-only.
- /// </summary>
- public static readonly string True;
- /// <summary>
- /// Represents JavaScript's boolean value false as a string. This field is read-only.
- /// </summary>
- public static readonly string False;
- /// <summary>
- /// Represents JavaScript's null as a string. This field is read-only.
- /// </summary>
- public static readonly string Null;
- /// <summary>
- /// Represents JavaScript's undefined as a string. This field is read-only.
- /// </summary>
- public static readonly string Undefined;
- internal static readonly long InitialJavaScriptDateTicks;
- internal static readonly DateTime MinimumJavaScriptDate;
- static JavaScriptConvert()
- {
- True = "true";
- False = "false";
- Null = "null";
- Undefined = "undefined";
- InitialJavaScriptDateTicks = (new DateTime(1970, 1, 1)).Ticks;
- MinimumJavaScriptDate = new DateTime(100, 1, 1);
- }
- /// <summary>
- /// Converts the <see cref="DateTime"/> to it's JavaScript string representation.
- /// </summary>
- /// <param name="value">The value to convert.</param>
- /// <returns>A Json string representation of the <see cref="DateTime"/>.</returns>
- public static string ToString(DateTime value)
- {
- long javaScriptTicks = ConvertDateTimeToJavaScriptTicks(value);
- return "new Date(" + javaScriptTicks + ")";
- }
- internal static long ConvertDateTimeToJavaScriptTicks(DateTime dateTime)
- {
- if (dateTime < MinimumJavaScriptDate)
- dateTime = MinimumJavaScriptDate;
- long javaScriptTicks = (dateTime.Ticks - InitialJavaScriptDateTicks) / (long)10000;
- return javaScriptTicks;
- }
- internal static DateTime ConvertJavaScriptTicksToDateTime(long javaScriptTicks)
- {
- DateTime dateTime = new DateTime((javaScriptTicks * 10000) + InitialJavaScriptDateTicks);
- return dateTime;
- }
- /// <summary>
- /// Converts the <see cref="Boolean"/> to it's JavaScript string representation.
- /// </summary>
- /// <param name="value">The value to convert.</param>
- /// <returns>A Json string representation of the <see cref="Boolean"/>.</returns>
- public static string ToString(bool value)
- {
- return (value) ? True : False;
- }
- /// <summary>
- /// Converts the <see cref="Char"/> to it's JavaScript string representation.
- /// </summary>
- /// <param name="value">The value to convert.</param>
- /// <returns>A Json string representation of the <see cref="Char"/>.</returns>
- public static string ToString(char value)
- {
- return value == '\0' ? Null : ToString (char.ToString (value));
- }
- /// <summary>
- /// Converts the <see cref="Enum"/> to it's JavaScript string representation.
- /// </summary>
- /// <param name="value">The value to convert.</param>
- /// <returns>A Json string representation of the <see cref="Enum"/>.</returns>
- public static string ToString(Enum value)
- {
- return value.ToString();
- }
- /// <summary>
- /// Converts the <see cref="Int32"/> to it's JavaScript string representation.
- /// </summary>
- /// <param name="value">The value to convert.</param>
- /// <returns>A Json string representation of the <see cref="Int32"/>.</returns>
- public static string ToString(int value)
- {
- return value.ToString(null, CultureInfo.InvariantCulture);
- }
- /// <summary>
- /// Converts the <see cref="Int16"/> to it's JavaScript string representation.
- /// </summary>
- /// <param name="value">The value to convert.</param>
- /// <returns>A Json string representation of the <see cref="Int16"/>.</returns>
- public static string ToString(short value)
- {
- return value.ToString(null, CultureInfo.InvariantCulture);
- }
- /// <summary>
- /// Converts the <see cref="UInt16"/> to it's JavaScript string representation.
- /// </summary>
- /// <param name="value">The value to convert.</param>
- /// <returns>A Json string representation of the <see cref="UInt16"/>.</returns>
- public static string ToString(ushort value)
- {
- return value.ToString(null, CultureInfo.InvariantCulture);
- }
- /// <summary>
- /// Converts the <see cref="UInt32"/> to it's JavaScript string representation.
- /// </summary>
- /// <param name="value">The value to convert.</param>
- /// <returns>A Json string representation of the <see cref="UInt32"/>.</returns>
- public static string ToString(uint value)
- {
- return value.ToString(null, CultureInfo.InvariantCulture);
- }
- /// <summary>
- /// Converts the <see cref="Int64"/> to it's JavaScript string representation.
- /// </summary>
- /// <param name="value">The value to convert.</param>
- /// <returns>A Json string representation of the <see cref="Int64"/>.</returns>
- public static string ToString(long value)
- {
- return value.ToString(null, CultureInfo.InvariantCulture);
- }
- /// <summary>
- /// Converts the <see cref="UInt64"/> to it's JavaScript string representation.
- /// </summary>
- /// <param name="value">The value to convert.</param>
- /// <returns>A Json string representation of the <see cref="UInt64"/>.</returns>
- public static string ToString(ulong value)
- {
- return value.ToString(null, CultureInfo.InvariantCulture);
- }
- /// <summary>
- /// Converts the <see cref="Single"/> to it's JavaScript string representation.
- /// </summary>
- /// <param name="value">The value to convert.</param>
- /// <returns>A Json string representation of the <see cref="Single"/>.</returns>
- public static string ToString(float value)
- {
- return value.ToString ("R", CultureInfo.InvariantCulture);
- }
- /// <summary>
- /// Converts the <see cref="Double"/> to it's JavaScript string representation.
- /// </summary>
- /// <param name="value">The value to convert.</param>
- /// <returns>A Json string representation of the <see cref="Double"/>.</returns>
- public static string ToString(double value)
- {
- return value.ToString ("R", CultureInfo.InvariantCulture);
- }
- /// <summary>
- /// Converts the <see cref="Byte"/> to it's JavaScript string representation.
- /// </summary>
- /// <param name="value">The value to convert.</param>
- /// <returns>A Json string representation of the <see cref="Byte"/>.</returns>
- public static string ToString(byte value)
- {
- return value.ToString(null, CultureInfo.InvariantCulture);
- }
- /// <summary>
- /// Converts the <see cref="SByte"/> to it's JavaScript string representation.
- /// </summary>
- /// <param name="value">The value to convert.</param>
- /// <returns>A Json string representation of the <see cref="SByte"/>.</returns>
- public static string ToString(sbyte value)
- {
- return value.ToString(null, CultureInfo.InvariantCulture);
- }
- /// <summary>
- /// Converts the <see cref="Decimal"/> to it's JavaScript string representation.
- /// </summary>
- /// <param name="value">The value to convert.</param>
- /// <returns>A Json string representation of the <see cref="SByte"/>.</returns>
- public static string ToString(decimal value)
- {
- return value.ToString(null, CultureInfo.InvariantCulture);
- }
- /// <summary>
- /// Converts the <see cref="Guid"/> to it's JavaScript string representation.
- /// </summary>
- /// <param name="value">The value to convert.</param>
- /// <returns>A Json string representation of the <see cref="Guid"/>.</returns>
- //public static string ToString(Guid value)
- //{
- // return '"' + value.ToString("D", CultureInfo.InvariantCulture) + '"';
- //}
- /// <summary>
- /// Converts the <see cref="String"/> to it's JavaScript string representation.
- /// </summary>
- /// <param name="value">The value to convert.</param>
- /// <returns>A Json string representation of the <see cref="String"/>.</returns>
- public static string ToString(string value)
- {
- return ToString(value, '"');
- }
- /// <summary>
- /// Converts the <see cref="String"/> to it's JavaScript string representation.
- /// </summary>
- /// <param name="value">The value to convert.</param>
- /// <param name="delimter">The string delimiter character.</param>
- /// <returns>A Json string representation of the <see cref="String"/>.</returns>
- public static string ToString(string value, char delimter)
- {
- return JavaScriptUtils.EscapeJavaScriptString(value, delimter, true);
- }
- }
- }
|