When encoding PHP objects as JSON, all public properties of that object will be encoded in a JSON object.
JSON does not allow object references, so care should be taken not to encode
objects with recursive references. If you have issues with recursion,
Laminas\Json\Encoder::encode() each allow an
optional second parameter to check for recursion; if an object is serialized
twice, an exception will be thrown.
Laminas\Json\Json will decode JSON objects as
However, if you desire an associative array returned, you can request it using
the second argument to
// Decode JSON objects as PHP array $phpNative = Laminas\Json\Json::decode($encodedValue, Laminas\Json\Json::TYPE_ARRAY);
Any objects thus decoded are returned as associative arrays with keys and values corresponding to the key/value pairs in the JSON notation.
The recommendation of Laminas is that the individual developer should decide how to decode JSON objects. If an object of a specified type should be created, it can be created in the developer code and populated with the values decoded using laminas-json.
Encoding PHP Objects
If you are encoding PHP objects, the default encoding mechanism can only
access public properties of these objects. When a method
implemented on an object to encode,
Laminas\Json\Json calls this method and
expects the object to return a JSON representation of its internal state.
Laminas\Json\Json can encode PHP objects recursively but does not do so by
default. This can be enabled by passing
true as the second argument to
// Encode PHP object recursively $jsonObject = Laminas\Json\Json::encode($data, true);
When doing recursive encoding of objects, as JSON does not support cycles, a
Laminas\Json\Exception\RecursionException will be thrown. If you wish, you can
silence these exceptions by passing the
$jsonObject = Laminas\Json\Json::encode( $data, true, ['silenceCyclicalExceptions' => true] );
Laminas\Json has two different modes depending if ext/json is enabled in your PHP
installation or not. If
ext/json is installed, laminas-json will use the
json_decode() functions for encoding and decoding JSON. If
ext/json is not installed, a Laminas implementation in PHP code is used
for en/decoding. This is considerably slower than using the PHP extension, but
behaves exactly the same.
Sometimes you might want to use the laminas-json encoder/decoder even if you have
ext/json installed. You can achieve this by calling:
Laminas\Json\Json::$useBuiltinEncoderDecoder = true;
within JSON object variables. They only work if not returned inside double
quotes, which laminas-json implements by default. With the Expression support for
This works when either
json_encode() or the internal encoder is used.
Laminas\Json\Expr object. It
expression as the first constructor argument. By default
pass the option
enableJsonExprFinder and set it to
TRUE when calling the
encode() method. If enabled, the expression support works for all nested
expressions in large object structures.
As an example:
Found a mistake or want to contribute to the documentation? Edit this page on GitHub!