All pastes #1374018 Raw Edit

Anonymous

public text v1 · immutable
#1374018 ·published 2009-03-27 14:48 UTC
rendered paste body
<?
class json {
   private $result;

   public function __construct($dataArray = NULL) {
      if ($dataArray != NULL) {
         $this->encode($dataArray);
      }
   }

   public function encode($dataArray) {
      if (function_exists("json_encode")) {
         $this->result = json_encode($dataArray);
      } else {
         $this->result = $this->php_json_encode($dataArray);
      }
   }

   // Taken from php.net
   private function php_json_encode($arr) {
      $json_str = "";
      if(is_array($arr)) {
         $pure_array = true;
         $array_length = count($arr);
         for($i=0;$i<$array_length;$i++) {
            if(! isset($arr[$i])) {
               $pure_array = false;
               break;
            }
         }
         if($pure_array) {
            $json_str ="[";
            $temp = array();
            for($i=0;$i<$array_length;$i++) {
               $temp[] = sprintf("%s", $this->php_json_encode($arr[$i]));
            }
            $json_str .= implode(",",$temp);
            $json_str .="]";
         } else {
            $json_str ="{";
            $temp = array();
            foreach($arr as $key => $value) {
               $temp[] = sprintf("\"%s\":%s", $key, $this->php_json_encode($value));
            }
            $json_str .= implode(",",$temp);
            $json_str .="}";
         }
      } else {
         if(is_string($arr)) {
            $json_str = "\"". $this->json_encode_string($arr) . "\"";
         } else if(is_numeric($arr)) {
            $json_str = $arr;
         } else {
            $json_str = "\"". $this->json_encode_string($arr) . "\"";
         }
      }
      return $json_str;
   }
   private function json_encode_string($in_str) {
      mb_internal_encoding("UTF-8");
      $convmap = array(0x80, 0xFFFF, 0, 0xFFFF);
      $str = "";
      for($i=mb_strlen($in_str)-1; $i>=0; $i--) {
         $mb_char = mb_substr($in_str, $i, 1);
         if(mb_ereg("&#(\\d+);", mb_encode_numericentity($mb_char, $convmap, "UTF-8"), $match)) {
            $str = sprintf("\\u%04x", $match[1]) . $str;
         } else {
            $str = $mb_char . $str;
         }
      }
      return $str;
   }

   public function __toString() {
      return $this->result;
   }
}
?>