#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 { /// /// Provides methods for converting between common language runtime types and JavaScript types. /// static class JavaScriptConvert { /// /// Represents JavaScript's boolean value true as a string. This field is read-only. /// public static readonly string True; /// /// Represents JavaScript's boolean value false as a string. This field is read-only. /// public static readonly string False; /// /// Represents JavaScript's null as a string. This field is read-only. /// public static readonly string Null; /// /// Represents JavaScript's undefined as a string. This field is read-only. /// 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, 0, 0, 0, DateTimeKind.Utc).Ticks; MinimumJavaScriptDate = new DateTime (100, 1, 1, 0, 0, 0, DateTimeKind.Utc); } /// /// Converts the to it's JavaScript string representation. /// /// The value to convert. /// A Json string representation of the . public static string ToString(DateTime value) { long javaScriptTicks = ConvertDateTimeToJavaScriptTicks(value); return @"""\/Date(" + javaScriptTicks + @")\/"""; } internal static long ConvertDateTimeToJavaScriptTicks(DateTime dateTime) { dateTime = dateTime.ToUniversalTime (); if (dateTime < MinimumJavaScriptDate) dateTime = MinimumJavaScriptDate; long javaScriptTicks = (dateTime.Ticks - InitialJavaScriptDateTicks) / (long)10000; return javaScriptTicks; } internal static DateTime ConvertJavaScriptTicksToDateTime(long javaScriptTicks) { return new DateTime((javaScriptTicks * 10000) + InitialJavaScriptDateTicks, DateTimeKind.Utc); } /// /// Converts the to it's JavaScript string representation. /// /// The value to convert. /// A Json string representation of the . public static string ToString(bool value) { return (value) ? True : False; } /// /// Converts the to it's JavaScript string representation. /// /// The value to convert. /// A Json string representation of the . public static void WriteChar (char value, TextWriter writer) { if (value == '\0') writer.Write(Null); else JavaScriptUtils.WriteEscapedJavaScriptChar (value, '"', true, writer); } /// /// Converts the to it's JavaScript string representation. /// /// The value to convert. /// A Json string representation of the . public static string ToString(Enum value) { return value.ToString(); } /// /// Converts the to it's JavaScript string representation. /// /// The value to convert. /// A Json string representation of the . public static string ToString(int value) { return value.ToString(null, CultureInfo.InvariantCulture); } /// /// Converts the to it's JavaScript string representation. /// /// The value to convert. /// A Json string representation of the . public static string ToString(short value) { return value.ToString(null, CultureInfo.InvariantCulture); } /// /// Converts the to it's JavaScript string representation. /// /// The value to convert. /// A Json string representation of the . public static string ToString(ushort value) { return value.ToString(null, CultureInfo.InvariantCulture); } /// /// Converts the to it's JavaScript string representation. /// /// The value to convert. /// A Json string representation of the . public static string ToString(uint value) { return value.ToString(null, CultureInfo.InvariantCulture); } /// /// Converts the to it's JavaScript string representation. /// /// The value to convert. /// A Json string representation of the . public static string ToString(long value) { return value.ToString(null, CultureInfo.InvariantCulture); } /// /// Converts the to it's JavaScript string representation. /// /// The value to convert. /// A Json string representation of the . public static string ToString(ulong value) { return value.ToString(null, CultureInfo.InvariantCulture); } /// /// Converts the to it's JavaScript string representation. /// /// The value to convert. /// A Json string representation of the . public static string ToString(float value) { return value.ToString ("R", CultureInfo.InvariantCulture); } /// /// Converts the to it's JavaScript string representation. /// /// The value to convert. /// A Json string representation of the . public static string ToString(double value) { return value.ToString ("R", CultureInfo.InvariantCulture); } /// /// Converts the to it's JavaScript string representation. /// /// The value to convert. /// A Json string representation of the . public static string ToString(byte value) { return value.ToString(null, CultureInfo.InvariantCulture); } /// /// Converts the to it's JavaScript string representation. /// /// The value to convert. /// A Json string representation of the . public static string ToString(sbyte value) { return value.ToString(null, CultureInfo.InvariantCulture); } /// /// Converts the to it's JavaScript string representation. /// /// The value to convert. /// A Json string representation of the . public static string ToString(decimal value) { return value.ToString(null, CultureInfo.InvariantCulture); } /// /// Converts the to it's JavaScript string representation. /// /// The value to convert. /// A Json string representation of the . //public static string ToString(Guid value) //{ // return '"' + value.ToString("D", CultureInfo.InvariantCulture) + '"'; //} /// /// Converts the to it's JavaScript string representation. /// /// The value to convert. /// A Json string representation of the . public static void WriteString (string value, TextWriter writer) { WriteString (value, '"', writer); } /// /// Converts the to it's JavaScript string representation. /// /// The value to convert. /// The string delimiter character. /// A Json string representation of the . public static void WriteString (string value, char delimter, TextWriter writer) { JavaScriptUtils.WriteEscapedJavaScriptString(value, delimter, true, writer); } } }