/** * Encodes data into a JSON string. * Can optionally beautify the result immediately. * * @param $data mixed The data to encode. * @param bool $beautify Whether to beautify the result right away * @return string The JSON string. * @throws JSON\Exception\CtrlCharException * @throws JSON\Exception\ErrorDepthException * @throws JSON\Exception\SyntaxException */ public static function encode($data, $beautify = false) { $encoded = json_encode($data); if ($beautify) { $encoded = JSON::beautify($encoded); } switch (json_last_error()) { case JSON_ERROR_DEPTH: throw new ErrorDepthException('JSON_ERROR_DEPTH', JSON_ERROR_DEPTH); break; case JSON_ERROR_CTRL_CHAR: throw new CtrlCharException('JSON_ERROR_CTRL_CHAR', JSON_ERROR_CTRL_CHAR); break; case JSON_ERROR_SYNTAX: throw new SyntaxException('JSON_ERROR_SYNTAX', JSON_ERROR_SYNTAX); break; default: return $encoded; break; } }
public function testBeautify() { // does beautify destroy json strings? $test = array('yellow' => 'flowers', "hallo" => 1244, 'float' => 0.467, array(3, 4, 6, 'siebzehn')); $this->assertEquals($test, JSON::decode(JSON::beautify(JSON::encode($test)))); }