Advertisement
voodooKobra

Escape package dependency hell;json_encode() & json_decode()

Dec 27th, 2013
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.18 KB | None | 0 0
  1. <?php
  2. // Temporary fixes while my development machine is borked; felt like sharing...
  3. // Stolen from various places
  4.     if(!function_exists('json_encode')) {
  5.         function json_encode($data) {
  6.         switch ($type = gettype($data)) {
  7.             case 'NULL':
  8.                 return 'null';
  9.             case 'boolean':
  10.                 return ($data ? 'true' : 'false');
  11.             case 'integer':
  12.             case 'double':
  13.             case 'float':
  14.                 return $data;
  15.             case 'string':
  16.                 return '"' . addslashes($data) . '"';
  17.             case 'object':
  18.                 $data = get_object_vars($data);
  19.             case 'array':
  20.                 $output_index_count = 0;
  21.                 $output_indexed = array();
  22.                 $output_associative = array();
  23.                 foreach ($data as $key => $value) {
  24.                     $output_indexed[] = json_encode($value);
  25.                     $output_associative[] = json_encode($key) . ':' . json_encode($value);
  26.                     if ($output_index_count !== NULL && $output_index_count++ !== $key) {
  27.                         $output_index_count = NULL;
  28.                     }
  29.                 }
  30.                 if ($output_index_count !== NULL) {
  31.                     return '[' . implode(',', $output_indexed) . ']';
  32.                 } else {
  33.                     return '{' . implode(',', $output_associative) . '}';
  34.                 }
  35.             default:
  36.                 return ''; // Not supported
  37.         }
  38.     }
  39.         function json_decode($json, $assoc = false) {
  40.  
  41.   /* by default we don't tolerate ' as string delimiters
  42.      if you need this, then simply change the comments on
  43.      the following lines: */
  44.  
  45.   // $matchString = '/(".*?(?<!\\\\)"|\'.*?(?<!\\\\)\')/';
  46.   $matchString = '/".*?(?<!\\\\)"/';
  47.  
  48.   // safety / validity test
  49.     $json = '';
  50.   $t = preg_replace( $matchString, '', $json );
  51.   $t = preg_replace( '/[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/', '', $t );
  52.   if ($t != '') { return null; }
  53.  
  54.   // build to/from hashes for all strings in the structure
  55.   $s2m = array();
  56.   $m2s = array();
  57.   preg_match_all( $matchString, $json, $m );
  58.   foreach ($m[0] as $s) {
  59.     $hash       = '"' . md5( $s ) . '"';
  60.     $s2m[$s]    = $hash;
  61.     $m2s[$hash] = str_replace( '$', '\$', $s );  // prevent $ magic
  62.   }
  63.  
  64.   // hide the strings
  65.   $json = strtr( $json, $s2m );
  66.  
  67.   // convert JS notation to PHP notation
  68.   $a = ($assoc) ? '' : '(object) ';
  69.   $json = strtr( $json,
  70.     array(
  71.       ':' => '=>',
  72.       '[' => 'array(',
  73.       '{' => "{$a}array(",
  74.       ']' => ')',
  75.       '}' => ')'
  76.     )
  77.   );
  78.  
  79.   // remove leading zeros to prevent incorrect type casting
  80.   $json = preg_replace( '~([\s\(,>])(-?)0~', '$1$2', $json );
  81.  
  82.   // return the strings
  83.   $json = strtr( $json, $m2s );
  84.  
  85.   /* "eval" string and return results.
  86.      As there is no try statement in PHP4, the trick here
  87.      is to suppress any parser errors while a function is
  88.      built and then run the function if it got made. */
  89.   $f = @create_function( '', "return {$json};" );
  90.   $r = ($f) ? $f() : null;
  91.  
  92.   // free mem (shouldn't really be needed, but it's polite)
  93.   unset( $s2m ); unset( $m2s ); unset( $f );
  94.  
  95.   return $r;
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement