json

Source:

Methods

(static) parse(jsonString, onFailopt) → {any}

Description:
  • Parses a JSON string. This function will throw an error if the input string is not valid JSON, similar to how JSON.parse() behaves. It also provides an optional callback that runs BEFORE the error is re-thrown.

Source:
Parameters:
Name Type Attributes Default Description
jsonString string

The string to parse as JSON.

onFail function <optional>
null

Optional callback function called with the error and the original string if parsing fails, before the error is re-thrown.

Throws:

If the string is not valid JSON.

Type
SyntaxError
Returns:

The parsed JavaScript object or value.

Type
any

(static) safeParse(jsonString, defaultValueopt, onErroropt) → {any}

Description:
  • Safely parses a JSON string, returning a default value on error. This function provides an optional callback for custom error handling.

Source:
Parameters:
Name Type Attributes Default Description
jsonString string

The string to parse.

defaultValue any <optional>
{}

The value to return if parsing fails.

onError function <optional>
null

Optional callback function called with the error and the original string if parsing fails. If not provided, a console warning will be logged by default. Pass () => {} to suppress all logging.

Returns:

The parsed JSON object, or the defaultValue if parsing fails.

Type
any