WebAPI
[ class tree: WebAPI ] [ index: WebAPI ] [ all elements ]

Source for file WebAPI_JSON.php

Documentation is available at WebAPI_JSON.php

  1. <?php
  2. /**
  3.  * Project: WebAPI_JSON:: JSON API
  4.  * File:    WebAPI/WebAPI_JSON.php
  5.  *
  6.  * WebAPI_JSON class는 JSON encode시에 UTF-8 관련 conflict를
  7.  * 고민하지 않고 사용할 수 있게 해 준다.
  8.  *
  9.  * @category    HTTP
  10.  * @package     WebAPI
  11.  * @subpackage  WebAPI_JSON
  12.  * @author      JoungKyun.Kim <http://oops.org>
  13.  * @copyright   (c) 2018, OOPS.org
  14.  * @license     BSD License
  15.  * @link        http://pear.oops.org/package/WebAPI
  16.  * @filesource
  17.  * @since       1.0.3
  18.  */
  19.  
  20. /**
  21.  * JSON API for WebAPI Package
  22.  *
  23.  * UTF-8 관련 conflict에 상관 없이 json encoding
  24.  *
  25.  * @package     WebAPI
  26.  */
  27. Class WebAPI_JSON {
  28.     // {{{ +-- static private (mixed) normalize ($v)
  29.     /**
  30.      * JSON encode시의 UTF-8 conflict 문제를 해결하기 위해
  31.      * ASCII 외의 문자는 모두 urlencode 처리한다.
  32.      */
  33.     static private function normalize ($var{
  34.         if is_object ($var&& is_array ($var) )
  35.             return urlencode ($var);
  36.  
  37.         foreach $var as $key => $val {
  38.             if is_object ($val|| is_array ($val) )
  39.                 $buf[urlencode ($key)self::normalize ($val);
  40.             else
  41.                 $buf[urlencode ($key)urlencode ($val);
  42.         }
  43.  
  44.         return $buf;
  45.     }
  46.     // }}}
  47.  
  48.     // {{{ +-- static private (mixed) unnormalize ($v)
  49.     /**
  50.      * url encode 되어 있는 데이터를 url decode 처리
  51.      */
  52.     static private function unnormalize ($var$assoc true{
  53.         if is_object ($var&& is_array ($var) )
  54.             return urldecode ($var);
  55.  
  56.         foreach $var as $key => $val {
  57.             if is_object ($val|| is_array ($val) )
  58.                 $buf[urldecode ($key)self::unnormalize ($val$assoc);
  59.             else
  60.                 $buf[urldecode ($key)urldecode ($val);
  61.         }
  62.  
  63.         if $assoc )
  64.             return $buf;
  65.  
  66.         return (object) $buf;
  67.     }
  68.     // }}}
  69.  
  70.     // {{{ +-- static public (string) encode ($data, $nopretty = false)
  71.     /**
  72.      * utf8 conflict 및 binary data 를 해결한 json encode wrapper
  73.      *
  74.      * @access public
  75.      * @return string 
  76.      * @param mixed encode할 변수
  77.      * @param bool (optional) pretty 출력 여부. 기본값 false이며 이는 pretty 출력
  78.      *              을 의미한다. pretty 인자는 php 5.4 부터 지원한다. [기본값 false]
  79.      * @param bool (optional) binary safe를 위한 normalize를 할지 여부 [기본값 true]
  80.      */
  81.     static public function encode ($data$nopretty false$normal true{
  82.         if is_object ($data|| is_array ($data) ) {
  83.             if count ((array) $data) )
  84.                 return json_encode (array ());
  85.         else {
  86.             if ($data trim ($data)) )
  87.                 return '';
  88.         }
  89.  
  90.         if $normal {
  91.             // for binary data
  92.             $data self::normalize ($data);
  93.         }
  94.  
  95.         // since php 5.4.0
  96.         if defined ('JSON_UNESCAPED_UNICODE') ) {
  97.             $opt JSON_NUMERIC_CHECK JSON_UNESCAPED_UNICODE;
  98.             if $nopretty )
  99.                 $opt |= JSON_PRETTY_PRINT;
  100.             return json_encode ($data$opt);
  101.         }
  102.  
  103.         // under php 5.3.x
  104.         if $normal )
  105.             return json_encode ($dataJSON_NUMERIC_CHECK);
  106.  
  107.         $data self::normalize ($data);
  108.         return urldecode (json_encode ($dataJSON_NUMERIC_CHECK));
  109.     }
  110.     // }}}
  111.  
  112.     // {{{ +-- static public (string) decode ($data)
  113.     /**
  114.      * utf8 conflict 및 binary data 를 해결한 json encode wrapper
  115.      *
  116.      * @access public
  117.      * @return stdClass 
  118.      * @param string $data json data
  119.      * @param int    (optional) User specified recursion depth (default: 512)
  120.      * @param int    (optional) Bitmask of JSON decode options. Currently only
  121.      *                JSON_BIGINT_AS_STRING is supported (default is
  122.      *                to cast large integers as floats)
  123.      * @since 1.0.5
  124.      */
  125.     static public function decode ($data$assoc false$depth 512$options 0{
  126.         $data json_decode ($data$assoc$depth$options);
  127.         return self::unnormalize ($data$assoc);
  128.     }
  129.     // }}}
  130. }
  131.  
  132. /*
  133.  * Local variables:
  134.  * tab-width: 4
  135.  * c-basic-offset: 4
  136.  * End:
  137.  * vim: set filetype=php noet sw=4 ts=4 fdm=marker:
  138.  * vim600: noet sw=4 ts=4 fdm=marker
  139.  * vim<600: noet sw=4 ts=4
  140.  */
  141. ?>

Documentation generated on Tue, 14 May 2019 02:00:50 +0900 by phpDocumentor 1.4.4