Yahoo! UI Library

json  2.5.0

Yahoo! UI Library > json > YAHOO.lang.JSON

Show Private Show Protected

static Class YAHOO.lang.JSON

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

Properties

_CHARS - private static {Object}

Character substitution map for common escapes and special characters.

_PARSE_DATE - private static {RegExp}

Regex used to reconstitute serialized Dates.

_SPECIAL_CHARS - private static {RegExp}

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

RE_INVALID - private static {RegExp}

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

RE_REPLACE_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.

RE_REPLACE_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).

RE_REPLACE_VALUES - private static {RegExp}

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

Methods

_applyFilter

private static MIXED _applyFilter ( data , filter )
Traverses nested objects, applying a filter or mutation 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
filter <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

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 , filter )
Parse a JSON string, returning the native JavaScript representation. Only minor modifications from http://www.json.org/json.js.
Parameters:
s <string> JSON string data
filter <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> (optional) whitelist of acceptable object keys to include
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 parse filter function to rebuild Dates during the parse operation.
Parameters:
str <String> String serialization of a Date
Returns: Date


Copyright © 2007 Yahoo! Inc. All rights reserved.