PHP Class Webmozart\Json\JsonEncoder

Since: 1.0
Author: Bernhard Schussek ([email protected])
Show file Open project: webmozart/json Class Usage Examples

Public Methods

Method Description
__construct ( JsonValidator $validator = null ) Creates a new encoder.
encode ( mixed $data, string | object $schema = null ) : string Encodes data as JSON.
encodeFile ( mixed $data, string $path, string | object $schema = null ) Encodes data into a JSON file.
getArrayEncoding ( ) : integer Returns the encoding of non-associative arrays.
getMaxDepth ( ) : integer Returns the maximum recursion depth.
getNumericEncoding ( ) : integer Returns the encoding of numeric strings.
isAmpersandEscaped ( ) : boolean Returns whether ampersands (&) are escaped.
isDoubleQuoteEscaped ( ) : boolean Returns whether double quotes (") are escaped.
isGtLtEscaped ( ) : boolean Returns whether greater than/less than symbols (>, <) are escaped.
isPrettyPrinting ( ) : boolean Returns whether JSON strings are formatted for better readability.
isSingleQuoteEscaped ( ) : boolean Returns whether single quotes (') are escaped.
isSlashEscaped ( ) : boolean Returns whether forward slashes (/) are escaped.
isTerminatedWithLineFeed ( ) : boolean Returns whether JSON strings are terminated with a line feed.
isUnicodeEscaped ( ) : boolean Returns whether unicode characters are escaped.
setArrayEncoding ( integer $encoding ) Sets the encoding of non-associative arrays.
setEscapeAmpersand ( boolean $enabled ) Sets whether ampersands (&) should be escaped.
setEscapeDoubleQuote ( boolean $enabled ) Sets whether double quotes (") should be escaped.
setEscapeGtLt ( boolean $enabled ) Sets whether greater than/less than symbols (>, <) should be escaped.
setEscapeSingleQuote ( boolean $enabled ) Sets whether single quotes (") should be escaped.
setEscapeSlash ( boolean $enabled ) Sets whether forward slashes (") should be escaped.
setEscapeUnicode ( boolean $enabled ) Sets whether unicode characters should be escaped.
setMaxDepth ( integer $maxDepth ) Sets the maximum recursion depth.
setNumericEncoding ( integer $encoding ) Sets the encoding of numeric strings.
setPrettyPrinting ( boolean $prettyPrinting ) Sets whether JSON strings should be formatted for better readability.
setTerminateWithLineFeed ( boolean $enabled ) Sets whether JSON strings should be terminated with a line feed.

Method Details

__construct() public method

Creates a new encoder.
public __construct ( JsonValidator $validator = null )
$validator JsonValidator

encode() public method

If a schema is passed, the value is validated against that schema before encoding. The schema may be passed as file path or as object returned from JsonDecoder::decodeFile($schemaFile). You can adjust the decoding with the various setters in this class.
public encode ( mixed $data, string | object $schema = null ) : string
$data mixed The data to encode
$schema string | object The schema file or object
return string The JSON string

encodeFile() public method

Encodes data into a JSON file.
See also: encode
public encodeFile ( mixed $data, string $path, string | object $schema = null )
$data mixed The data to encode
$path string The path where the JSON file will be stored
$schema string | object The schema file or object

getArrayEncoding() public method

Returns the encoding of non-associative arrays.
public getArrayEncoding ( ) : integer
return integer One of the constants {@link JSON_OBJECT} and {@link JSON_ARRAY}

getMaxDepth() public method

A depth of zero means that objects are not allowed. A depth of one means only one level of objects or arrays is allowed.
public getMaxDepth ( ) : integer
return integer The maximum recursion depth

getNumericEncoding() public method

Returns the encoding of numeric strings.
public getNumericEncoding ( ) : integer
return integer One of the constants {@link JSON_STRING} and {@link JSON_NUMBER}

isAmpersandEscaped() public method

If true, ampersands will be escaped as "\u0026". By default, ampersands are not escaped.
public isAmpersandEscaped ( ) : boolean
return boolean Whether ampersands are escaped

isDoubleQuoteEscaped() public method

If true, double quotes will be escaped as "\u0022". By default, double quotes are not escaped.
public isDoubleQuoteEscaped ( ) : boolean
return boolean Whether double quotes are escaped

isGtLtEscaped() public method

If true, greater than will be escaped as "\u003E" and less than as "\u003C". By default, greater than/less than symbols are not escaped.
public isGtLtEscaped ( ) : boolean
return boolean Whether greater than/less than symbols are escaped

isPrettyPrinting() public method

If true, line breaks will be added after object properties and array entries. Each new nesting level will be indented by four spaces. By default, pretty printing is not enabled.
public isPrettyPrinting ( ) : boolean
return boolean Whether JSON strings are formatted

isSingleQuoteEscaped() public method

If true, single quotes will be escaped as "\u0027". By default, single quotes are not escaped.
public isSingleQuoteEscaped ( ) : boolean
return boolean Whether single quotes are escaped

isSlashEscaped() public method

If true, forward slashes will be escaped as "\/". By default, forward slashes are not escaped.
public isSlashEscaped ( ) : boolean
return boolean Whether forward slashes are escaped

isTerminatedWithLineFeed() public method

By default, JSON strings are not terminated with a line feed.
public isTerminatedWithLineFeed ( ) : boolean
return boolean Whether JSON strings are terminated with a line feed

isUnicodeEscaped() public method

If true, unicode characters will be escaped as hexadecimals strings. For example, "ü" will be escaped as "\u00fc". By default, unicode characters are escaped.
public isUnicodeEscaped ( ) : boolean
return boolean Whether unicode characters are escaped

setArrayEncoding() public method

By default, non-associative arrays are decoded as JSON arrays.
public setArrayEncoding ( integer $encoding )
$encoding integer One of the constants {@link JSON_OBJECT} and {@link JSON_ARRAY}

setEscapeAmpersand() public method

If true, ampersands will be escaped as "\u0026". By default, ampersands are not escaped.
public setEscapeAmpersand ( boolean $enabled )
$enabled boolean Whether ampersands should be escaped

setEscapeDoubleQuote() public method

If true, double quotes will be escaped as "\u0022". By default, double quotes are not escaped.
public setEscapeDoubleQuote ( boolean $enabled )
$enabled boolean Whether double quotes should be escaped

setEscapeGtLt() public method

If true, greater than will be escaped as "\u003E" and less than as "\u003C". By default, greater than/less than symbols are not escaped.
public setEscapeGtLt ( boolean $enabled )
$enabled boolean Whether greater than/less than should be escaped

setEscapeSingleQuote() public method

If true, single quotes will be escaped as "\u0027". By default, single quotes are not escaped.
public setEscapeSingleQuote ( boolean $enabled )
$enabled boolean Whether single quotes should be escaped

setEscapeSlash() public method

If true, forward slashes will be escaped as "\/". By default, forward slashes are not escaped.
public setEscapeSlash ( boolean $enabled )
$enabled boolean Whether forward slashes should be escaped

setEscapeUnicode() public method

If true, unicode characters will be escaped as hexadecimals strings. For example, "ü" will be escaped as "\u00fc". By default, unicode characters are escaped.
public setEscapeUnicode ( boolean $enabled )
$enabled boolean Whether unicode characters should be escaped

setMaxDepth() public method

If the depth is exceeded during encoding, an {@link EncodingFailedException} will be thrown. A depth of zero means that objects are not allowed. A depth of one means only one level of objects or arrays is allowed.
public setMaxDepth ( integer $maxDepth )
$maxDepth integer The maximum recursion depth

setNumericEncoding() public method

By default, non-associative arrays are decoded as JSON strings.
public setNumericEncoding ( integer $encoding )
$encoding integer One of the constants {@link JSON_STRING} and {@link JSON_NUMBER}

setPrettyPrinting() public method

If true, line breaks will be added after object properties and array entries. Each new nesting level will be indented by four spaces. By default, pretty printing is not enabled.
public setPrettyPrinting ( boolean $prettyPrinting )
$prettyPrinting boolean Whether JSON strings should be formatted

setTerminateWithLineFeed() public method

By default, JSON strings are not terminated with a line feed.
public setTerminateWithLineFeed ( boolean $enabled )
$enabled boolean Whether JSON strings should be terminated with a line feed