/** * @module org.rhaco.Flow * @param mixed $obj */ public function flow_exception_output($obj, \Exception $exception) { \org\rhaco\Log::disable_display(); \org\rhaco\net\http\Header::send('Content-Type', $this->mode == 'jsonp' ? 'text/javascript' : 'application/json'); $error = array('error' => array()); if ($exception instanceof \org\rhaco\Exceptions) { foreach (\org\rhaco\Exceptions::gets() as $g => $e) { $error['error'][] = array('message' => $e->getMessage(), 'group' => $g, 'type' => basename(str_replace("\\", '/', get_class($e)))); } } else { $error['error'][] = array('message' => $exception->getMessage(), 'group' => 'exceptions', 'type' => basename(str_replace("\\", '/', get_class($exception)))); } $json = \org\rhaco\lang\Json::encode($error); print $this->mode == 'jsonp' ? $this->varname . '(' . $json . ')' : $json; }
/** * Json文字列にして返す * @param mixed $value * @return string */ public function json($value) { return \org\rhaco\lang\Json::encode($value); }
/** * @automap */ public function method_info_json() { $result = array(); try { $result = Dt\Man::method_info($this->in_vars('class'), $this->in_vars('method')); } catch (\Exception $e) { } \org\rhaco\lang\Json::output($result); }
<?php $variable = array(1, 2, 3); eq("[1,2,3]", \org\rhaco\lang\Json::encode($variable)); $variable = "ABC"; eq("\"ABC\"", \org\rhaco\lang\Json::encode($variable)); $variable = 10; eq(10, \org\rhaco\lang\Json::encode($variable)); $variable = 10.123; eq(10.123, \org\rhaco\lang\Json::encode($variable)); $variable = true; eq("true", \org\rhaco\lang\Json::encode($variable)); $variable = array('foo', 'bar', array(1, 2, 'baz'), array(3, array(4))); eq('["foo","bar",[1,2,"baz"],[3,[4]]]', \org\rhaco\lang\Json::encode($variable)); $variable = array("foo" => "bar", 'baz' => 1, 3 => 4); eq('{"foo":"bar","baz":1,"3":4}', \org\rhaco\lang\Json::encode($variable)); $variable = array("type" => "hoge", "name" => "fuga"); eq('{"type":"hoge","name":"fuga"}', \org\rhaco\lang\Json::encode($variable)); # array $variable = array("name" => "hoge", "type" => "fuga"); eq('{"name":"hoge","type":"fuga"}', \org\rhaco\lang\Json::encode($variable)); $variable = array("aa", "name" => "hoge", "type" => "fuga"); eq('{"0":"aa","name":"hoge","type":"fuga"}', \org\rhaco\lang\Json::encode($variable)); $variable = array("aa", "hoge", "fuga"); eq('["aa","hoge","fuga"]', \org\rhaco\lang\Json::encode($variable)); $variable = array("aa", "hoge", "fuga"); eq('["aa","hoge","fuga"]', \org\rhaco\lang\Json::encode($variable)); $variable = array(array("aa" => 1), array("aa" => 2), array("aa" => 3)); eq('[{"aa":1},{"aa":2},{"aa":3}]', \org\rhaco\lang\Json::encode($variable));
eq($variable, \org\rhaco\lang\Json::decode('{"A":"a","B":"b","C":{"E":"e","F":{"H":"h","I":"i"},"G":"g"}}')); $variable = array("A" => "a", "B" => array(1, 2, 3), "C" => "c"); eq($variable, \org\rhaco\lang\Json::decode('{"A":"a","B":[1,2,3],"C":"c"}')); $variable = array("A" => "a", "B" => array(1, array("C" => "c", "D" => "d"), 3), "C" => "c"); eq($variable, \org\rhaco\lang\Json::decode('{"A":"a","B":[1,{"C":"c","D":"d"},3],"C":"c"}')); $variable = array(array("a" => 1, "b" => array("a", "b", 1)), array(null, false, true)); eq($variable, \org\rhaco\lang\Json::decode('[ {"a" : 1, "b" : ["a", "b", 1] }, [ null, false, true ] ]')); eq(null, \org\rhaco\lang\Json::decode("[1,2,3,]")); eq(null, \org\rhaco\lang\Json::decode("[1,2,3,,,]")); if (extension_loaded("json")) { eq(null, json_decode("[1,[1,2,],3]")); } eq(array(1, null, 3), \org\rhaco\lang\Json::decode("[1,[1,2,],3]")); eq(null, \org\rhaco\lang\Json::decode('{"A":"a","B":"b","C":"c",}')); eq(array("hoge" => "123,456"), \org\rhaco\lang\Json::decode('{"hoge":"123,456"}')); // quote eq(array("hoge" => '123,"456'), \org\rhaco\lang\Json::decode('{"hoge":"123,\\"456"}')); eq(array("hoge" => "123,'456"), \org\rhaco\lang\Json::decode('{"hoge":"123,\'456"}')); eq(array("hoge" => '123,\\"456'), \org\rhaco\lang\Json::decode('{"hoge":"123,\\\\\\"456"}')); eq(array("hoge" => "123,\\'456"), \org\rhaco\lang\Json::decode('{"hoge":"123,\\\\\'456"}')); // escape eq(array("hoge" => "\\"), \org\rhaco\lang\Json::decode('{"hoge":"\\\\"}')); eq(array("hoge" => "a\\"), \org\rhaco\lang\Json::decode('{"hoge":"a\\\\"}')); eq(array("hoge" => "t\\t"), \org\rhaco\lang\Json::decode('{"hoge":"t\\\\t"}')); eq(array("hoge" => "\tA"), \org\rhaco\lang\Json::decode('{"hoge":"\\tA"}')); // value_error try { \org\rhaco\lang\Json::decode("{'hoge':'123,456'}"); fail(); } catch (\InvalidArgumentException $e) { }