Updated: August 2008
Used by the JSON.stringify method to enable the transformation of an object's data of before the JSON serialization.
objectname.toJSON()
Required. An object of type String, Boolean, Date, or Number for which JSON serialization is wanted.
The toJSON method is a member of the String, Boolean, Date, and Number JScript objects. Its behavior depends on the object type, as shown in the following table:
Object
Behavior
Boolean
Returns this.valueOf().
Date
Returns an ISO-formatted date string for the UTC time zone (denoted by the suffix Z).
Number
String
You can override the toJSON method for the previous types or define a toJSON method for other object types to achieve transformation of data for the specific object type before JSON serialization.
The following example illustrates the use of the toJSON method. toJSON is called when JSON.stringify is called. The conditional statement returns true because toJSON returns the same string literal value during the serialization of both the objects.
var a = new String(42); var b = new String("42"); if((JSON.stringify(a)=='"42"')&&(JSON.stringify(b)=='"42"')) { // Code placed here will run. }
Version 5.8
Starting with JScript 5.8, by default, the JScript scripting engine supports the language feature set as it existed in version 5.7. This is to maintain compatibility with the earlier versions of the engine. To use the complete language feature set of version 5.8, the Windows Script interface host has to invoke IActiveScriptProperty::SetProperty.
Internet Explorer 8 opts into the JScript 5.8 language features when the document mode for Internet Explorer 8 is "Internet Explorer 8 Standards" mode. For other document modes, Internet Explorer uses the version 5.7 feature set.
JScript 5.8 includes native JavaScript Object Notation (JSON) support and the accessor methods for Document Object Model (DOM) prototypes.
Applies To: Boolean Object| Date Object| Number Object| String Object
History
Reason
August 2008
Added topic.
Information enhancement.