Overview

Namespaces

  • Webmozart
    • Json

Classes

  • JsonDecoder
  • JsonEncoder
  • JsonError
  • JsonValidator

Exceptions

  • DecodingFailedException
  • EncodingFailedException
  • FileNotFoundException
  • InvalidSchemaException
  • ValidationFailedException
  • Overview
  • Namespace
  • Class

Class JsonEncoder

Encodes data as JSON.

Namespace: Webmozart\Json
Author: Bernhard Schussek bschussek@gmail.com
Since: 1.0
Located at JsonEncoder.php
Methods summary
public
# __construct( )

Creates a new encoder.

Creates a new encoder.

public string
# encode( mixed $data, string|object $schema = null )

Encodes data as JSON.

Encodes data as JSON.

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.

Parameters

$data
The data to encode.
$schema
The schema file or object.

Returns

string
The JSON string.

Throws

Webmozart\Json\EncodingFailedException
If the data could not be encoded.
Webmozart\Json\ValidationFailedException
If the data fails schema validation.
Webmozart\Json\InvalidSchemaException
If the schema is invalid.
public
# encodeFile( mixed $data, string $file, string|object $schema = null )

Encodes data into a JSON file.

Encodes data into a JSON file.

Parameters

$data
The data to encode.
$file
The path where the JSON file will be stored.
$schema
The schema file or object.

Throws

Webmozart\Json\EncodingFailedException
If the data could not be encoded.
Webmozart\Json\ValidationFailedException
If the data fails schema validation.
Webmozart\Json\InvalidSchemaException
If the schema is invalid.

See

Webmozart\Json\JsonEncoder::encode()
public integer
# getArrayEncoding( )

Returns the encoding of non-associative arrays.

Returns the encoding of non-associative arrays.

Returns

integer
One of the constants Webmozart\Json\JsonEncoder::JSON_OBJECT and Webmozart\Json\JsonEncoder::JSON_ARRAY.
public
# setArrayEncoding( integer $encoding )

Sets the encoding of non-associative arrays.

Sets the encoding of non-associative arrays.

By default, non-associative arrays are decoded as JSON arrays.

Parameters

$encoding
One of the constants Webmozart\Json\JsonEncoder::JSON_OBJECT and Webmozart\Json\JsonEncoder::JSON_ARRAY.

Throws

InvalidArgumentException
If the passed encoding is invalid.
public integer
# getNumericEncoding( )

Returns the encoding of numeric strings.

Returns the encoding of numeric strings.

Returns

integer
One of the constants Webmozart\Json\JsonEncoder::JSON_STRING and Webmozart\Json\JsonEncoder::JSON_NUMBER.
public
# setNumericEncoding( integer $encoding )

Sets the encoding of numeric strings.

Sets the encoding of numeric strings.

By default, non-associative arrays are decoded as JSON strings.

Parameters

$encoding
One of the constants Webmozart\Json\JsonEncoder::JSON_STRING and Webmozart\Json\JsonEncoder::JSON_NUMBER.

Throws

InvalidArgumentException
If the passed encoding is invalid.
public boolean
# isAmpersandEscaped( )

Returns whether ampersands (&) are escaped.

Returns whether ampersands (&) are escaped.

If true, ampersands will be escaped as "\u0026".

By default, ampersands are not escaped.

Returns

boolean
Whether ampersands are escaped.
public
# setEscapeAmpersand( boolean $enabled )

Sets whether ampersands (&) should be escaped.

Sets whether ampersands (&) should be escaped.

If true, ampersands will be escaped as "\u0026".

By default, ampersands are not escaped.

Parameters

$enabled
Whether ampersands should be escaped.
public boolean
# isDoubleQuoteEscaped( )

Returns whether double quotes (") are escaped.

Returns whether double quotes (") are escaped.

If true, double quotes will be escaped as "\u0022".

By default, double quotes are not escaped.

Returns

boolean
Whether double quotes are escaped.
public
# setEscapeDoubleQuote( boolean $enabled )

Sets whether double quotes (") should be escaped.

Sets whether double quotes (") should be escaped.

If true, double quotes will be escaped as "\u0022".

By default, double quotes are not escaped.

Parameters

$enabled
Whether double quotes should be escaped.
public boolean
# isSingleQuoteEscaped( )

Returns whether single quotes (') are escaped.

Returns whether single quotes (') are escaped.

If true, single quotes will be escaped as "\u0027".

By default, single quotes are not escaped.

Returns

boolean
Whether single quotes are escaped.
public
# setEscapeSingleQuote( boolean $enabled )

Sets whether single quotes (") should be escaped.

Sets whether single quotes (") should be escaped.

If true, single quotes will be escaped as "\u0027".

By default, single quotes are not escaped.

Parameters

$enabled
Whether single quotes should be escaped.
public boolean
# isSlashEscaped( )

Returns whether forward slashes (/) are escaped.

Returns whether forward slashes (/) are escaped.

If true, forward slashes will be escaped as "\/".

By default, forward slashes are not escaped.

Returns

boolean
Whether forward slashes are escaped.
public
# setEscapeSlash( boolean $enabled )

Sets whether forward slashes (") should be escaped.

Sets whether forward slashes (") should be escaped.

If true, forward slashes will be escaped as "\/".

By default, forward slashes are not escaped.

Parameters

$enabled
Whether forward slashes should be escaped.
public boolean
# isGtLtEscaped( )

Returns whether greater than/less than symbols (>, <) are escaped.

Returns whether greater than/less than symbols (>, <) are escaped.

If true, greater than will be escaped as "\u003E" and less than as "\u003C".

By default, greater than/less than symbols are not escaped.

Returns

boolean
Whether greater than/less than symbols are escaped.
public
# setEscapeGtLt( boolean $enabled )

Sets whether greater than/less than symbols (>, <) should be escaped.

Sets whether greater than/less than symbols (>, <) should be escaped.

If true, greater than will be escaped as "\u003E" and less than as "\u003C".

By default, greater than/less than symbols are not escaped.

Parameters

$enabled
Whether greater than/less than should be escaped.
public boolean
# isUnicodeEscaped( )

Returns whether unicode characters are escaped.

Returns whether unicode characters are escaped.

If true, unicode characters will be escaped as hexadecimals strings. For example, "ü" will be escaped as "\u00fc".

By default, unicode characters are escaped.

Returns

boolean
Whether unicode characters are escaped.
public
# setEscapeUnicode( boolean $enabled )

Sets whether unicode characters should be escaped.

Sets whether unicode characters should be escaped.

If true, unicode characters will be escaped as hexadecimals strings. For example, "ü" will be escaped as "\u00fc".

By default, unicode characters are escaped.

Parameters

$enabled
Whether unicode characters should be escaped.
public boolean
# isPrettyPrinting( )

Returns whether JSON strings are formatted for better readability.

Returns whether JSON strings are formatted for better readability.

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.

Returns

boolean
Whether JSON strings are formatted.
public
# setPrettyPrinting( boolean $prettyPrinting )

Sets whether JSON strings should be formatted for better readability.

Sets whether JSON strings should be formatted for better readability.

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.

Parameters

$prettyPrinting
Whether JSON strings should be formatted.
public boolean
# isTerminatedWithLineFeed( )

Returns whether JSON strings are terminated with a line feed.

Returns whether JSON strings are terminated with a line feed.

By default, JSON strings are not terminated with a line feed.

Returns

boolean
Whether JSON strings are terminated with a line feed.
public
# setTerminateWithLineFeed( boolean $enabled )

Sets whether JSON strings should be terminated with a line feed.

Sets whether JSON strings should be terminated with a line feed.

By default, JSON strings are not terminated with a line feed.

Parameters

$enabled

Whether JSON strings should be terminated with a line feed.

public integer
# getMaxDepth( )

Returns the maximum recursion depth.

Returns the maximum recursion depth.

A depth of zero means that objects are not allowed. A depth of one means only one level of objects or arrays is allowed.

Returns

integer
The maximum recursion depth.
public
# setMaxDepth( integer $maxDepth )

Sets the maximum recursion depth.

Sets the maximum recursion depth.

If the depth is exceeded during encoding, an Webmozart\Json\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.

Parameters

$maxDepth
The maximum recursion depth.

Throws

InvalidArgumentException

If the depth is not an integer greater than or equal to zero.

Constants summary
integer JSON_ARRAY

Encode a value as JSON array.

Encode a value as JSON array.

# 1
integer JSON_OBJECT

Encode a value as JSON object.

Encode a value as JSON object.

# 2
integer JSON_STRING

Encode a value as JSON string.

Encode a value as JSON string.

# 3
integer JSON_NUMBER

Encode a value as JSON integer or float.

Encode a value as JSON integer or float.

# 4
Webmozart JSON API API documentation generated by ApiGen