コード例 #1
0
ファイル: JsonFormat.php プロジェクト: jeremydenoun/Restler
 public function encode($data, $humanReadable = false)
 {
     if (!is_null(self::$prettyPrint)) {
         $humanReadable = self::$prettyPrint;
     }
     if (is_null(self::$unEscapedSlashes)) {
         self::$unEscapedSlashes = $humanReadable;
     }
     if (is_null(self::$unEscapedUnicode)) {
         self::$unEscapedUnicode = $this->charset == 'utf-8';
     }
     $options = 0;
     if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 4 || PHP_MAJOR_VERSION > 5) {
         if ($humanReadable) {
             $options |= JSON_PRETTY_PRINT;
         }
         if (self::$unEscapedSlashes) {
             $options |= JSON_UNESCAPED_SLASHES;
         }
         if (self::$bigIntAsString) {
             $options |= JSON_BIGINT_AS_STRING;
         }
         if (self::$unEscapedUnicode) {
             $options |= JSON_UNESCAPED_UNICODE;
         }
         $result = json_encode(Object::toArray($data, true), $options);
         $this->handleJsonError();
         return $result;
     }
     $result = json_encode(Object::toArray($data, true));
     $this->handleJsonError();
     if ($humanReadable) {
         $result = $this->formatJson($result);
     }
     if (self::$unEscapedUnicode) {
         $result = preg_replace_callback('/\\\\u(\\w\\w\\w\\w)/', function ($matches) {
             if (function_exists('mb_convert_encoding')) {
                 return mb_convert_encoding(pack('H*', $matches[1]), 'UTF-8', 'UTF-16BE');
             } else {
                 return iconv('UTF-16BE', 'UTF-8', pack('H*', $matches[1]));
             }
         }, $result);
     }
     if (self::$unEscapedSlashes) {
         $result = str_replace('\\/', '/', $result);
     }
     return $result;
 }
コード例 #2
0
 public function encode($data, $humanReadable = false)
 {
     if (!is_null(self::$prettyPrint)) {
         $humanReadable = self::$prettyPrint;
     }
     if (is_null(self::$unEscapedSlashes)) {
         self::$unEscapedSlashes = $humanReadable;
     }
     if (is_null(self::$unEscapedUnicode)) {
         self::$unEscapedUnicode = $this->charset == 'utf-8';
     }
     $options = 0;
     if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 4 || PHP_MAJOR_VERSION > 5) {
         if ($humanReadable) {
             $options |= JSON_PRETTY_PRINT;
         }
         if (self::$unEscapedSlashes) {
             $options |= JSON_UNESCAPED_SLASHES;
         }
         if (self::$bigIntAsString) {
             $options |= JSON_BIGINT_AS_STRING;
         }
         if (self::$unEscapedUnicode) {
             $options |= JSON_UNESCAPED_UNICODE;
         }
         return json_encode(Util::objectToArray($data, true), $options);
     }
     $result = json_encode(Util::objectToArray($data, true));
     if ($humanReadable) {
         $result = $this->formatJson($result);
     }
     if (self::$unEscapedUnicode) {
         $result = preg_replace_callback('/\\\\u(\\w\\w\\w\\w)/', function ($matches) {
             return utf8_encode(chr(hexdec($matches[1])));
         }, $result);
     }
     if (self::$unEscapedSlashes) {
         $result = str_replace('\\/', '/', $result);
     }
     return $result;
 }