PageRenderTime 123ms CodeModel.GetById 40ms app.highlight 8ms RepoModel.GetById 52ms app.codeStats 0ms

/system/helper/json.php

https://bitbucket.org/jjasko/opencart_serbian
PHP | 135 lines | 95 code | 33 blank | 7 comment | 12 complexity | fa1e4fee2619c32eea0d802504174782 MD5 | raw file
  1<?php
  2if (!function_exists('json_encode')) {
  3	function json_encode($data) {
  4		switch (gettype($data)) {
  5			case 'boolean':
  6				return $data ? 'true' : 'false';
  7			case 'integer':
  8			case 'double':
  9				return $data;
 10			case 'resource':
 11			case 'string':
 12				# Escape non-printable or Non-ASCII characters. 
 13				# I also put the \\ character first, as suggested in comments on the 'addclashes' page. 
 14				$json = ''; 
 15				
 16				$string = '"' . addcslashes($data, "\\\"\n\r\t/" . chr(8) . chr(12)) . '"'; 
 17				
 18				# Convert UTF-8 to Hexadecimal Codepoints. 
 19				for ($i = 0; $i < strlen($string); $i++) { 
 20					$char = $string[$i]; 
 21					$c1 = ord($char); 
 22					
 23					# Single byte; 
 24					if ($c1 < 128) { 
 25						$json .= ($c1 > 31) ? $char : sprintf("\\u%04x", $c1); 
 26						
 27						continue; 
 28					} 
 29					
 30					# Double byte 
 31					$c2 = ord($string[++$i]); 
 32					
 33					if (($c1 & 32) === 0) { 
 34						$json .= sprintf("\\u%04x", ($c1 - 192) * 64 + $c2 - 128); 
 35						
 36						continue;
 37					} 
 38					
 39					# Triple 
 40					$c3 = ord($string[++$i]); 
 41					
 42					if (($c1 & 16) === 0) { 
 43						$json .= sprintf("\\u%04x", (($c1 - 224) <<12) + (($c2 - 128) << 6) + ($c3 - 128)); 
 44						
 45						continue; 
 46					} 
 47						
 48					# Quadruple 
 49					$c4 = ord($string[++$i]); 
 50					
 51					if (($c1 & 8 ) === 0) { 
 52						$u = (($c1 & 15) << 2) + (($c2>>4) & 3) - 1; 
 53					
 54						$w1 = (54<<10) + ($u<<6) + (($c2 & 15) << 2) + (($c3>>4) & 3); 
 55						$w2 = (55<<10) + (($c3 & 15)<<6) + ($c4-128); 
 56						$json .= sprintf("\\u%04x\\u%04x", $w1, $w2); 
 57					} 
 58				} 				
 59			
 60				return $json;
 61			case 'array':
 62				if (empty($data) || array_keys($data) === range(0, sizeof($data) - 1)) {
 63					$output = array();
 64					
 65					foreach ($data as $value) {
 66						$output[] = json_encode($value);
 67					}
 68					
 69					return '[' . implode(',', $output) . ']';
 70				}
 71			case 'object':
 72				$output = array();
 73				
 74				foreach ($data as $key => $value) {
 75					$output[] = json_encode(strval($key)) . ':' . json_encode($value);
 76				}
 77				
 78				return '{' . implode(',', $output) . '}';
 79			default:
 80				return 'null';
 81		}
 82	}
 83}
 84
 85if (!function_exists('json_decode')) {	
 86	function json_decode($json, $assoc = false) {
 87		$match = '/".*?(?<!\\\\)"/';
 88
 89		$string = preg_replace($match, '', $json);
 90		$string = preg_replace('/[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/', '', $string);
 91
 92		if ($string != '') {
 93			return null;
 94		}
 95
 96		$s2m = array();
 97		$m2s = array();
 98
 99		preg_match_all($match, $json, $m);
100		
101		foreach ($m[0] as $s) {
102			$hash = '"' . md5($s) . '"';
103			$s2m[$s] = $hash;
104			$m2s[$hash] = str_replace('$', '\$', $s);
105		}
106
107		$json = strtr($json, $s2m);
108
109		$a = ($assoc) ? '' : '(object) ';
110		
111		$data = array(
112			':' => '=>', 
113			'[' => 'array(', 
114			'{' => "{$a}array(", 
115			']' => ')', 
116			'}' => ')'
117		);
118		
119		$json = strtr($json, $data);
120
121		$json = preg_replace('~([\s\(,>])(-?)0~', '$1$2', $json);
122
123		$json = strtr($json, $m2s);
124
125		$function = @create_function('', "return {$json};");
126		$return = ($function) ? $function() : null;
127
128		unset($s2m); 
129		unset($m2s); 
130		unset($function);
131
132		return $return;
133	}
134}
135?>