associative = $associative; $this->depth = $depth; // We don't want to throw on errors, we manually check for errors using json_last_error(). $this->encodeOptions = $encodeOptions & ~self::THROW_ON_ERROR; $this->decodeOptions = $decodeOptions & ~self::THROW_ON_ERROR; } public function serialize($data): string { $result = \json_encode($data, $this->encodeOptions, $this->depth); switch ($code = \json_last_error()) { case \JSON_ERROR_NONE: return $result; default: throw new SerializationException(\json_last_error_msg(), $code); } } public function unserialize(string $data) { $result = \json_decode($data, $this->associative, $this->depth, $this->decodeOptions); switch ($code = \json_last_error()) { case \JSON_ERROR_NONE: return $result; default: throw new SerializationException(\json_last_error_msg(), $code); } } }