Yahoo! UI Library

json  2.7.0

Yahoo! UI Library > json > JSON
Search:
 
Filters

static Class JSON

Provides methods to parse JSON strings and convert objects to JSON strings.

Properties

_BRACKETS - private static {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 static {RegExp}

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

_INVALID - private static {RegExp}

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

_SPECIAL_CHARS - private static {RegExp}

Regex used to replace special characters in strings for JSON stringification.

_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 escape sequences. IMPORTANT NOTE: This regex will be used to modify the input if a match is found.

_VALUES - private static {RegExp}

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

Methods

_char

String _char ( c )
Escapes a special character to a safe Unicode representation
Parameters:
c <String> single character to escape
Returns: String
safe Unicode escape

_isValid

static boolean _isValid ( str )
Four step determination whether a string is valid JSON. In three steps, escape sequences, safe values, and properly placed open square brackets are replaced with placeholders or removed. Then in the final step, the result of all these replacements is checked for invalid characters.
Parameters:
str <String> JSON string to be tested
Returns: boolean
is the string safe for eval?

_prepare

private String _prepare ( s )
Replace certain Unicode characters that may be handled incorrectly by some browser implementations.
Parameters:
s <String> parse input
Returns: String
sanitized JSON string ready to be validated/parsed

_revive

private MIXED _revive ( data , reviver )
Traverses nested objects, applying a filter or reviver function to each value. 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/mutated data structure

_string

private String _string ( s )
Enclose escaped strings in quotes
Parameters:
s <String> string to wrap
Returns: String
'"'+s+'"' after s has had special characters escaped

_stringify

String _stringify ( h , key , depth , w , pstack )
Worker function used by public stringify.
Parameters:
h <Object> object holding the key
key <String> String key in object h to serialize
depth <Number> depth to serialize
w <Array|Function> array of whitelisted keys OR replacer function
pstack <Array> used to protect against recursion
Returns: String
serialized version of o

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

isValid

static boolean isValid ( str )
Four step determination whether a string is valid JSON. In three steps, escape sequences, safe values, and properly placed open square brackets are replaced with placeholders or removed. Then in the final step, the result of all these replacements is checked for invalid characters.
Parameters:
str <String> JSON string to be tested
Returns: boolean
is the string safe for eval?

parse

static MIXED parse ( s , reviver )
Parse a JSON string, returning the native JavaScript representation. Only minor modifications from http://www.json.org/json2.js.
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 function(value,key) to alter values before serialization
d <number> (optional) depth limit to recurse objects/arrays (practical minimum 1)
Returns: string
JSON string representation of the input

stringToDate

Date stringToDate ( str )
Reconstitute Date instances from the default JSON UTC serialization. Reference this from a reviver function to rebuild Dates during the parse operation.
Parameters:
str <String> String serialization of a Date
Returns: Date


Copyright © 2009 Yahoo! Inc. All rights reserved.