/** * {@inheritDoc} */ public function render($model, $values = null) { $apiResponse = $model->getApiResponse(); if ($apiResponse->isError()) { $errors = $apiResponse->getErrors(); if ($e = $model->getException()) { $errors[$apiResponse->getStatus()] = $e->getMessage(); } $payload = ['errors' => $errors]; } else { $payload = $apiResponse->getContent(); } if (null === $payload) { return null; } $jsonpCallback = $model->getOption('callback'); if (null !== $jsonpCallback) { // Wrap the JSON in a JSONP callback. $this->setJsonpCallback($jsonpCallback); } $output = parent::render($payload); if (null !== $model->getOption('pretty_print')) { // Pretty print the JSON. $output = Json::prettyPrint($output); } return $output; }
public function argsAction() { $job = $this->api()->read('jobs', $this->params('id'))->getContent(); $args = Json::prettyPrint(Json::encode($job->args()), ['indent' => ' ']); $response = $this->getResponse(); $response->getHeaders()->addHeaderLine('Content-Type', 'text/plain; charset=utf-8'); $response->setContent($args); return $response; }
/** * Writes log message to file * @param $name * @param $content * @param string $contentType * @return string */ public function uploadString($name, $content, $contentType = 'text/plain') { $fileName = $this->path . '/' . $this->getFilePathAndUniquePrefix() . $name; if ($contentType === 'application/json') { $content = Json::prettyPrint($content); } (new Filesystem())->dumpFile($fileName, $content); return $fileName; }
/** * Encode any mixed-type value into JSON * * @param mixed $value Value to be encoded to JSON * @param boolean $prettify [optional] If JSON should be prettify-formatted * @param boolean $html [optional] If spaces/indents should be properly viewable for HTML * @return string */ public static function Encode($value, $prettify = false, $html = false) { $json = ZendJson::encode($value); if ($prettify) { $json = ZendJson::prettyPrint($json); if ($html) { $json = str_replace("\n", '<br>', str_replace(' ', ' ', $json)); } } return $json; }
public function index01Action() { $input = array('invokables' => array('Data\\Controller\\Index' => 'Data\\Controller\\IndexController', 'Data\\Controller\\Filter' => 'Data\\Controller\\FilterController', 'Data\\Controller\\Serializer' => 'Data\\Controller\\SerializerController', 'Data\\Controller\\Escaper' => 'Data\\Controller\\EscaperController', 'Data\\Controller\\Purifier' => 'Data\\Controller\\PurifierController', 'Data\\Controller\\Dom' => 'Data\\Controller\\DomController', 'Data\\Controller\\Json' => 'Data\\Controller\\JsonController')); $output = \Zend\Json\Json::encode($input); $strJson = \Zend\Json\Json::encode($input); echo \Zend\Json\Json::prettyPrint($output, array("indent" => "\t")) . '<br/>'; //$o = \Zend\Json\Json::decode($strJson); /** chuyển về 1 đối tượng object */ $o = \Zend\Json\Json::decode($strJson, \Zend\Json\Json::TYPE_ARRAY); /** chuyển về 1 array */ echo '<pre>'; print_r($o); echo '</pre>'; return $this->response; }
public static function generate(array $config, $profilePath = null) { $buildConfig = $config['build']; $packages = array(); foreach ($buildConfig['packages'] as $name => $path) { $packages[] = array('name' => $name, 'location' => $path); } $profile = $buildConfig; unset($profile['profilePath']); $profile['packages'] = $packages; $profile = Json::prettyPrint(Json::encode($profile)); $profile = str_replace('<profile>', $profile, file_get_contents(__DIR__ . '/Profile.js.template')); if (!isset($profilePath)) { $profilePath = $buildConfig['profilePath']; } if (file_put_contents($profilePath, $profile)) { return array($profile, $profilePath); } else { return array(null, $profilePath); } }
public function testGetResources() { $response = ClientStatic::get(self::HOST . '/resources', [], ['Accept' => 'application/hal+json']); echo Json::prettyPrint($response->getBody()); }
/** * Render a Zend_Config into a JSON config string. * * @since 1.10 * @return string */ public function render() { $data = $this->_config->toArray(); $sectionName = $this->_config->getSectionName(); $extends = $this->_config->getExtends(); if (is_string($sectionName)) { $data = array($sectionName => $data); } foreach ($extends as $section => $parentSection) { $data[$section][JsonConfig::EXTENDS_NAME] = $parentSection; } // Ensure that each "extends" section actually exists foreach ($data as $section => $sectionData) { if (is_array($sectionData) && isset($sectionData[JsonConfig::EXTENDS_NAME])) { $sectionExtends = $sectionData[JsonConfig::EXTENDS_NAME]; if (!isset($data[$sectionExtends])) { // Remove "extends" declaration if section does not exist unset($data[$section][JsonConfig::EXTENDS_NAME]); } } } $out = JsonUtil::encode($data); if ($this->prettyPrint()) { $out = JsonUtil::prettyPrint($out); } return $out; }
/** * Invoked by event manager * * @param MvcEvent $e * @return void */ public function __invoke(MvcEvent $e) { $response = $e->getResponse(); $headers = $response->getHeaders(); if (!$headers->has('Content-Type')) { return; } $contentType = $headers->get('Content-Type'); if (false !== strpos('application/json', $contentType->getFieldValue())) { return; } $request = $e->getRequest(); $headers = $request->getHeaders(); if (!$headers->has('X-Pretty-Json')) { return; } $body = $response->getContent(); $body = Json::prettyPrint($body, array('indent' => ' ')); $body = $body . "\n"; $response->setContent($body); }
/** * Compile bootstrap for a theme */ public function compileAction() { // Theme name $name = _post('name') ?: Pi::theme()->current(); // Compiled boostrap.min.css, string $bsString = _post('less'); // Config JSON string $cfgString = _post('custom'); // Write bootstrap scripts to online custom theme folder $path = sprintf('%s/custom/theme/%s/asset/vendor/bootstrap/css', Pi::path('asset'), $name); $configJson = Json::prettyPrint(json_encode($cfgString), array('indent' => ' ')); Pi::service('file')->mkdir($path); file_put_contents($path . '/bootstrap.min.css', $bsString); file_put_contents(dirname($path) . '/config.json', $configJson); // Republish the theme Pi::service('asset')->publishTheme($name); return array('status' => 1, 'message' => __('Bootstrap compiled successfully.')); }
public function serialize() { $json = parent::serialize(); return \Zend\Json\Json::prettyPrint($json, array('indent' => ' ')); }
<?php use Zend\Json\Json; use Zend\Json\Decode; require_once '/home/matthew/tmp/composer/vendor/autoload.php'; $json = file_get_contents('users.search.raw.json'); $php = Json::decode($json); $json = Json::encode($php); $json = Json::prettyPrint($json, array('indent' => ' ')); echo $json;
public function testPrettyPrintDoublequoteFollowingEscapedBackslashShouldNotBeTreatedAsEscaped() { $this->assertEquals("[\n\t1,\n\t\"\\\\\",\n\t3\n]", Json\Json::prettyPrint(Json\Json::encode(array(1, '\\', 3)))); $this->assertEquals("{\n\t\"a\":\"\\\\\"\n}", Json\Json::prettyPrint(Json\Json::encode(array('a' => '\\')))); }
<?php use JzForm\Render\Json\Form as JsonForm; require_once 'autoloader.php'; $form = (require 'forms/simple.php'); $filter = (require 'filters/simple.php'); $render = new JsonForm(); $json = $render->render($form, $filter); $string = \Zend\Json\Json::encode($json); echo \Zend\Json\Json::prettyPrint($string);
/** * {@inheritdoc} */ public function formatAttributes(array $attributes = []) { $json = Json::encode($attributes, false, ['enableJsonExprFinder' => true]); $content = Json::prettyPrint($json, ['indent' => ' ']); return $content; }
$defaults = ['DebugMode' => false, 'TestClassRegex' => '(.+)Test', 'ReporterClass' => 'HtmlReporter']; // note that this can be overridden by "config.json" ob_start(); // Initialize configuration $fileConfig = realpath(__DIR__ . "/config.json"); $config = $defaults; if ($fileConfig === false || !file_exists($fileConfig)) { echo concat("<b>Warning:</b> Config file <b>", __DIR__ . "/config.json", "</b> not found. Will use the defaults.<br>"); } else { $config = json_decode(file_get_contents($fileConfig), true); if (empty($config)) { echo "<b>Warning:</b> Config file is not a valid JSON configuration. Will use the defaults.<br>"; } } echo "<br><br>Proceeding using the following configuration:<br>"; printbr(nl2br(str_replace(" ", " ", \Zend\Json\Json::prettyPrint(json_encode($config, true)))), true); $debug_output = ob_get_clean(); if ($config['DebugMode']) { echo $debug_output; } // Load and Run all test files $recursiveIterator = new RecursiveDirectoryIterator(__DIR__, RecursiveDirectoryIterator::SKIP_DOTS); $it = new RecursiveIteratorIterator($recursiveIterator); $regexIterator = new RegexIterator($it, '/Test\\.php$/'); foreach ($regexIterator as $key => $value) { // By default, we only get classes which names end with "Test", e.g. MyUtilityTest, etc. $classname = preg_replace(sprintf('/(.+)(\\/|\\\\)(%s)\\.(.+)$/i', $config['TestClassRegex']), '$3', (string) $value); if ($classname == $value) { continue; } require_once (string) $value;
/** * @group ZF-10185 */ public function testJsonPrettyPrintWorksWithArrayNotationInStringLiteral() { $o = new \stdClass(); $o->test = 1; $o->faz = 'fubar'; // The escaped double-quote in item 'stringwithjsonchars' ensures that // escaped double-quotes don't throw off prettyPrint's string literal detection $test = array('simple' => 'simple test string', 'stringwithjsonchars' => '\\"[1,2]', 'complex' => array('foo' => 'bar', 'far' => 'boo', 'faz' => array('obj' => $o))); $pretty = Json\Json::prettyPrint(Json\Json::encode($test), array("indent" => " ")); $expected = <<<EOB { "simple":"simple test string", "stringwithjsonchars":"\\\\\\u0022[1,2]", "complex":{ "foo":"bar", "far":"boo", "faz":{ "obj":{ "test":1, "faz":"fubar" } } } } EOB; $this->assertSame($expected, $pretty); }
protected function implodeParams(array $params) { $tmp = []; foreach ($params as $key => $value) { switch (true) { case is_array($value): $param = "{$key}: " . Json::prettyPrint(Json::encode($value, false, ['enableJsonExprFinder' => true])); break; case $value instanceof Expr: $param = "{$key}: " . $value; break; default: $param = "{$key}: '{$value}'"; } $tmp[] = $param; } return $this->indent(implode(",\n\n", $tmp), 12); }