Yahoo! UI Library

json  2.6.0

Yahoo! UI Library > json > JSON

static Class JSON

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

Properties

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?

_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 © 2008 Yahoo! Inc. All rights reserved.