Show:
Module: json-parse
Parent Module: json

<p>The JSON module adds support for serializing JavaScript objects into JSON strings and parsing JavaScript objects from strings in JSON format.</p>

<p>The JSON namespace is added to your YUI instance including static methods Y.JSON.parse(..) and Y.JSON.stringify(..).</p>

<p>The functionality and method signatures follow the ECMAScript 5 specification. In browsers with native JSON support, the native implementation is used.</p>

<p>The <code>json</code> module is a rollup of <code>json-parse</code> and <code>json-stringify</code>.</p>

<p>As their names suggest, <code>json-parse</code> adds support for parsing JSON data (Y.JSON.parse) and <code>json-stringify</code> for serializing JavaScript data into JSON strings (Y.JSON.stringify). You may choose to include either of the submodules individually if you don't need the complementary functionality, or include the rollup for both.</p>

Methods

_revive

(
  • data
  • reviver
)
MIXED private

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

customTransport

(
  • id
)
static

Provided by the io module.

Defined in yui3/src/io/js/transports.js:56

Create a custom transport of type and return it's object

Parameters:

  • id String

    The id of the transport to create.

dateToString

(
  • d
)
String deprecated static

Provided by the json-stringify module.

Defined in yui3/src/json/js/stringify.js:13

Deprecated: Use a replacer function

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

defaultTransport

(
  • [id]
)
Object static

Provided by the io module.

Defined in yui3/src/io/js/transports.js:18

Parameters:

  • [id] String optional

    The transport to set as the default, if empty a new transport is created.

Returns:

Object:

The transport object with a send method

escapeException

(
  • c
)
String private

Replaces specific unicode characters with their appropriate \unnnn format. Some browsers ignore certain characters during eval.

Parameters:

  • c String

    Unicode character

Returns:

String:

the \unnnn escapement of the character

notify

(
  • event
  • transaction
  • config
)

Provided by the io module.

Defined in yui3/src/io/js/transports.js:71

Fired from the notify method of the transport which in turn fires the event on the IO object.

Parameters:

  • event String

    The name of the event

  • transaction Object

    The transaction object

  • config Object

    The configuration object for this transaction

parse

(
  • s
  • reviver
)
MIXED static

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

(
  • o
  • w
  • ind
)
String static

Provided by the json-stringify module.

Defined in yui3/src/json/js/stringify.js:37

<p>Converts an arbitrary value to a JSON string representation.</p>

<p>Objects with cyclical references will trigger an exception.</p>

<p>If a whitelist is provided, only matching object keys will be included. Alternately, a replacer function may be passed as the second parameter. This function is executed on every value in the input, and its return value will be used in place of the original value. This is useful to serialize specialized objects or class instances.</p>

<p>If a positive integer or non-empty string is passed as the third parameter, the output will be formatted with carriage returns and indentation for readability. If a String is passed (such as "\t") it will be used once for each indentation level. If a number is passed, that number of spaces will be used.</p>

Parameters:

  • o MIXED

    any arbitrary value 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

  • ind Number | String

    (optional) indentation character or depth of spaces to format the output.

Returns:

String:

JSON string representation of the input

Properties

_BRACKETS

RegExp private

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

_default

String static

Provided by the io module.

Defined in yui3/src/io/js/transports.js:11

The ID of the default IO transport, defaults to xhr

_ESCAPES

RegExp private

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

_UNICODE_EXCEPTIONS

RegExp private

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.

_UNSAFE

RegExp private

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

_VALUES

RegExp private

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

charCacheThreshold

Number static

Provided by the json-stringify module.

Defined in yui3/src/json/js/stringify.js:68

<p>Number of occurrences of a special character within a single call to stringify that should trigger promotion of that character to a dedicated preprocess step for future calls. This is only used in environments that don't support native JSON, or when useNativeJSONStringify is set to false.</p>

<p>So, if set to 50 and an object is passed to stringify that includes strings containing the special character \x07 more than 50 times, subsequent calls to stringify will process object strings through a faster serialization path for \x07 before using the generic, slower, replacement process for all special characters.</p>

<p>To prime the preprocessor cache, set this value to 1, then call <code>Y.JSON.stringify("<em>(all special characters to cache)</em>");</code>, then return this setting to a more conservative value.</p>

<p>Special characters \ " \b \t \n \f \r are already cached.</p>

Default: 100

transports

Object static

Provided by the io module.

Defined in yui3/src/io/js/transports.js:38

An object hash of custom transports available to IO