options = $options; return $this; } /** * Get options * * @return JsonOptions */ public function getOptions() { if ($this->options === null) { $this->options = new JsonOptions(); } return $this->options; } /** * Serialize PHP value to JSON * * @param mixed $value * @return string * @throws Exception\InvalidArgumentException * @throws Exception\RuntimeException */ public function serialize($value) { $options = $this->getOptions(); $cycleCheck = $options->getCycleCheck(); $opts = array( 'enableJsonExprFinder' => $options->getEnableJsonExprFinder(), 'objectDecodeType' => $options->getObjectDecodeType(), ); try { return ZendJson::encode($value, $cycleCheck, $opts); } catch (\InvalidArgumentException $e) { throw new Exception\InvalidArgumentException('Serialization failed: ' . $e->getMessage(), 0, $e); } catch (\Exception $e) { throw new Exception\RuntimeException('Serialization failed: ' . $e->getMessage(), 0, $e); } } /** * Deserialize JSON to PHP value * * @param string $json * @return mixed * @throws Exception\InvalidArgumentException * @throws Exception\RuntimeException */ public function unserialize($json) { try { $ret = ZendJson::decode($json, $this->getOptions()->getObjectDecodeType()); } catch (\InvalidArgumentException $e) { throw new Exception\InvalidArgumentException('Unserialization failed: ' . $e->getMessage(), 0, $e); } catch (\Exception $e) { throw new Exception\RuntimeException('Unserialization failed: ' . $e->getMessage(), 0, $e); } return $ret; } }