Yahoo! UI Library

json  3.0.0pr2

Yahoo! UI Library > json > JSON
Search:
 
Filters

static Class JSON

The JSON Utility provides methods to serialize JavaScript objects into JSON strings and parse JavaScript objects from strings containing JSON data. Three modules are available for inclusion:
  1. 1. json-parse for parsing JSON strings into native JavaScript data
  2. 2. json-stringify for stringification of JavaScript objects into JSON strings
  3. 3. json for both parsing and stringification
Both json-parse and json-stringify create functions in a static JSON class under your YUI instance (e.g. Y.JSON.parse(..)).

Properties

_BRACKETS - private {RegExp}

Third step in the validation. Regex used to remove all open square brackets following a colon, comma, or at the beginning of the string.

_CHARS - private static {Object}

Character substitution map for common escapes and special characters.

_ESCAPES - private {RegExp}

First step in the validation. Regex used to replace all escape sequences (i.e. "\\", etc) with '@' characters (a non-JSON character).

_INVALID - private {RegExp}

Final step in the validation. Regex used to test the string left after all previous replacements for invalid characters.

_SPECIAL_CHARS - private {RegExp}

Regex used to capture characters that need escaping before enclosing their containing string in quotes.

_UNICODE_EXCEPTIONS - private {RegExp}

Replace certain Unicode characters that JavaScript may handle incorrectly during eval--either by deleting them or treating them as line endings--with escae sequences. IMPORTANT NOTE: This regex will be used to modify the input if a match is found.

_VALUES - private {RegExp}

Second step in the validation. Regex used to replace all simple values with ']' characters.

Methods

_revive

private MIXED _revive ( data , reviver )
Traverses nested objects, applying a reviver function to each (key,value) from the scope if the key:value's containing object. The value returned from the function will replace the original value in the key:value pair. If the value returned is undefined, the key will be omitted from the returned object.
Parameters:
data <MIXED> Any JavaScript data
reviver <Function> filter or mutation function
Returns: MIXED
The results of the filtered data

dateToString

static String dateToString ( d )
Serializes a Date instance as a UTC date string. Used internally by stringify. Override this method if you need Dates serialized in a different format.
Parameters:
d <Date> The Date to serialize
Returns: String
stringified Date in UTC format YYYY-MM-DDTHH:mm:SSZ

parse

static MIXED parse ( s , reviver )
Parse a JSON string, returning the native JavaScript representation.
Parameters:
s <string> JSON string data
reviver <function> (optional) function(k,v) passed each key value pair of object literals, allowing pruning or altering values
Returns: MIXED
the native JavaScript representation of the JSON string

stringify

static string stringify ( o , w , d )
Converts an arbitrary value to a JSON string representation. Cyclical object or array references are replaced with null. If a whitelist is provided, only matching object keys will be included. If a depth limit is provided, objects and arrays at that depth will be stringified as empty.
Parameters:
o <MIXED> any arbitrary object to convert to JSON string
w <Array|Function> (optional) whitelist of acceptable object keys to include, or a replacer function to modify the raw value before serialization
d <number> (optional) depth limit to recurse objects/arrays (practical minimum 1)
Returns: string
JSON string representation of the input


Copyright © 2008 Yahoo! Inc. All rights reserved.