/** * Serialize PHP value to JSON * * @param mixed $value * @param array $opts * @return string * @throws \Zend\Serializer\Exception on JSON encoding exception */ public function serialize($value, array $opts = array()) { $opts = $opts + $this->_options; try { return ZendJSON::encode($value, $opts['cycleCheck'], $opts); } catch (\Exception $e) { throw new SerializationException('Serialization failed', 0, $e); } }
/** * Encode data as JSON, disable layouts, and set response header * * If $keepLayouts is true, does not disable layouts. * * @param mixed $data * @param bool $keepLayouts * NOTE: if boolean, establish $keepLayouts to true|false * if array, admit params for Zend_Json::encode as enableJsonExprFinder=>true|false * this array can contains a 'keepLayout'=>true|false * that will not be passed to Zend_Json::encode method but will be used here * @return string|void */ public function direct($data = null, $keepLayouts = false) { if ($data == null) { throw new \InvalidArgumentException('JSON: missing argument. $data is required in json($data, $keepLayouts = false)'); } $options = array(); if (is_array($keepLayouts)) { $options = $keepLayouts; $keepLayouts = array_key_exists('keepLayouts', $keepLayouts) ? $keepLayouts['keepLayouts'] : false; unset($options['keepLayouts']); } $data = \Zend\JSON\JSON::encode($data, null, $options); if (!$keepLayouts) { $layout = LayoutManager::getMvcInstance(); if ($layout instanceof LayoutManager) { $layout->disableLayout(); } } $response = \Zend\Controller\Front::getInstance()->getResponse(); $response->setHeader('Content-Type', 'application/json'); return $data; }
/** * Cast to JSON * * @return string */ public function toJSON() { if ($this->isError()) { $response = array('result' => null, 'error' => $this->getError()->toArray(), 'id' => $this->getId()); } else { $response = array('result' => $this->getResult(), 'id' => $this->getId(), 'error' => null); } if (null !== ($version = $this->getVersion())) { $response['jsonrpc'] = $version; } return \Zend\JSON\JSON::encode($response); }
/** * JSON post processing * * JSON serialize view variables to response body * * @return void */ public function postJsonContext() { if (!$this->getAutoJsonSerialization()) { return; } $viewRenderer = HelperBroker\HelperBroker::getStaticHelper('viewRenderer'); $view = $viewRenderer->view; if ($view instanceof View\ViewInterface) { if (method_exists($view, 'getVars')) { $vars = \Zend\JSON\JSON::encode($view->getVars()); $this->getResponse()->setBody($vars); } else { throw new Action\Exception('View does not implement the getVars() method needed to encode the view into JSON'); } } }
/** * Cast request to JSON * * @return string */ public function toJSON() { $jsonArray = array('method' => $this->getMethod()); if (null !== ($id = $this->getId())) { $jsonArray['id'] = $id; } $params = $this->getParams(); if (!empty($params)) { $jsonArray['params'] = $params; } if ('2.0' == $this->getVersion()) { $jsonArray['jsonrpc'] = '2.0'; } return JSON\JSON::encode($jsonArray); }
public function testLoadingFromJSONShouldSetJSONRpcVersionWhenPresent() { $options = $this->getOptions(); $options['jsonrpc'] = '2.0'; $json = JSON\JSON::encode($options); $this->request->loadJSON($json); $this->assertEquals('2.0', $this->request->getVersion()); }
/** * Return JSON encoding of service * * @return string */ public function toJSON() { $service = array($this->getName() => $this->toArray()); return \Zend\JSON\JSON::encode($service); }
/** * Cast error to JSON * * @return string */ public function toJSON() { return \Zend\JSON\JSON::encode($this->toArray()); }
public function toJSON() { $data = array('expr' => new JSON\Expr($this->_expr), 'int' => $this->_int, 'string' => $this->_string); return JSON\JSON::encode($data, false, array('enableJSONExprFinder' => true)); }
/** * Get the HTML code for the captcha * * This method uses the public key to fetch a recaptcha form. * * @return string * @throws \Zend\Service\ReCaptcha\Exception */ public function getHtml() { if ($this->_publicKey === null) { throw new Exception('Missing public key'); } $host = self::API_SERVER; if ((bool) $this->_params['ssl'] === true) { $host = self::API_SECURE_SERVER; } $htmlBreak = '<br>'; $htmlInputClosing = '>'; if ((bool) $this->_params['xhtml'] === true) { $htmlBreak = '<br />'; $htmlInputClosing = '/>'; } $errorPart = ''; if (!empty($this->_params['error'])) { $errorPart = '&error=' . urlencode($this->_params['error']); } $reCaptchaOptions = ''; if (!empty($this->_options)) { $encoded = \Zend\JSON\JSON::encode($this->_options); $reCaptchaOptions = <<<SCRIPT <script type="text/javascript"> var RecaptchaOptions = {$encoded}; </script> SCRIPT; } $return = $reCaptchaOptions; $return .= <<<HTML <script type="text/javascript" src="{$host}/challenge?k={$this->_publicKey}{$errorPart}"> </script> HTML; $return .= <<<HTML <noscript> <iframe src="{$host}/noscript?k={$this->_publicKey}{$errorPart}" height="300" width="500" frameborder="0"></iframe>{$htmlBreak} <textarea name="recaptcha_challenge_field" rows="3" cols="40"> </textarea> <input type="hidden" name="recaptcha_response_field" value="manual_challenge"{$htmlInputClosing} </noscript> HTML; return $return; }
/** * Returns the items of the current page as JSON. * * @return string */ public function toJson() { $currentItems = $this->getCurrentItems(); if ($currentItems instanceof \Zend\DB\Table\Rowset\AbstractRowset) { return JSON\JSON::encode($currentItems->toArray()); } else { return JSON\JSON::encode($currentItems); } }
/** * Converts an associative array to a string of tag attributes. * * @access public * * @param array $attribs From this array, each key-value pair is * converted to an attribute name and value. * * @return string The XHTML for the attributes. */ protected function _htmlAttribs($attribs) { $xhtml = ''; foreach ((array) $attribs as $key => $val) { $key = $this->view->escape($key); if ('on' == substr($key, 0, 2) || 'constraints' == $key) { // Don't escape event attributes; _do_ substitute double quotes with singles if (!is_scalar($val)) { // non-scalar data should be cast to JSON first $val = \Zend\JSON\JSON::encode($val); } $val = preg_replace('/"([^"]*)":/', '$1:', $val); } else { if (is_array($val)) { $val = implode(' ', $val); } $val = $this->view->escape($val); } if ('id' == $key) { $val = $this->_normalizeId($val); } if (strpos($val, '"') !== false) { $xhtml .= " {$key}='{$val}'"; } else { $xhtml .= " {$key}=\"{$val}\""; } } return $xhtml; }