PHP Class Webmozart\Json\JsonEncoder

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

Méthodes publiques

Méthode 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 méthode

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

encode() public méthode

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
Résultat string The JSON string

encodeFile() public méthode

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 méthode

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

getMaxDepth() public méthode

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
Résultat integer The maximum recursion depth

getNumericEncoding() public méthode

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

isAmpersandEscaped() public méthode

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

isDoubleQuoteEscaped() public méthode

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

isGtLtEscaped() public méthode

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
Résultat boolean Whether greater than/less than symbols are escaped

isPrettyPrinting() public méthode

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
Résultat boolean Whether JSON strings are formatted

isSingleQuoteEscaped() public méthode

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

isSlashEscaped() public méthode

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

isTerminatedWithLineFeed() public méthode

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

isUnicodeEscaped() public méthode

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
Résultat boolean Whether unicode characters are escaped

setArrayEncoding() public méthode

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 méthode

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 méthode

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 méthode

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 méthode

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 méthode

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 méthode

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 méthode

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 méthode

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 méthode

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 méthode

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