Return to Snippet

Revision: 13114
at July 27, 2009 14:38 by kirie


Updated Code
if(!function_exists('json_encode')){
	function json_encode($v){
		if($v==NUll)return 'null';
		else if(is_array($v)){
			if(!count($v)||array_keys($v)===range(0,count($v)-1))return '['.join(',',array_map(__FUNCTION__,$v)).']';
			foreach($v as $k=>$val)$v[$k]=call_user_func(__FUNCTION__,$k).':'.call_user_func(__FUNCTION__,$val);
			return '{'.join(',',$v).'}';
		}
		return '"'.addslashes(preg_replace('/(\n|\r|\t)/i','',strval($v))).'"';
	}
}

Revision: 13113
at July 27, 2009 14:38 by kirie


Updated Code
if(!function_exists('json_encode')){
	function json_encode($v){
		if($v==NUll)return 'null';
		else if(is_array($v)){
			if(!count($v)||array_keys($v)===range(0,count($v)-1))return '['.join(',',array_map(__FUNCTION__,$v)).']';
			foreach($v as $k=>$val)$v[$k]=call_user_func(__FUNCTION__,$k).':'.call_user_func(__FUNCTION__,$val);
			return '{'.join(',',$v).'}';
		}
		else return '"'.addslashes(preg_replace('/(\n|\r|\t)/i','',strval($v))).'"';
	}
}

Revision: 13112
at April 11, 2009 17:49 by kirie


Updated Code
if(!function_exists('json_encode')){
	function json_encode($v){
		if($v==NUll)return 'null';
		else if(is_array($v)){
			if(!count($v)||array_keys($v)===range(0,count($v)-1))return '['.join(',',array_map(__FUNCTION__,$v)).']';
			foreach($v as $k=>$val)$v[$k]=call_user_func(__FUNCTION__,$k).':'.call_user_func(__FUNCTION__,$val);
			return '{'.join(',',$v).'}';
		}
		else return '"'.addslashes(preg_replace('/(\n|\r|\t)/i','',strval($v))).'"';
		return '{'.$txt.'}';
	}
}

Revision: 13111
at April 11, 2009 17:49 by kirie


Updated Code
if(!function_exists('json_encode')){
	function json_encode($v){
		if($v==NUll)return 'null';
		else if(is_array($v)){
			if(!count($v)||array_keys($v)===range(0,count($v)-1))return '['.join(',',array_map(__FUNCTION__,$v)).']';
			foreach($v as $k=>$val)$v[$k]=call_user_func(__FUNCTION__,$k).':'.call_user_func(__FUNCTION__,$val);
				return '{'.join(',',$v).'}';
			}
		else return '"'.addslashes(preg_replace('/(\n|\r|\t)/i','',strval($v))).'"';
		return '{'.$txt.'}';
	}
}

Revision: 13110
at April 9, 2009 09:12 by kirie


Initial Code
if(!function_exists('json_encode')){
	function json_encode($v){
		if($v==NUll)return 'null';
		else if(is_array($v)){
			if(!count($v)||array_keys($v)===range(0,count($v)-1))return '['.join(',',array_map(__FUNCTION__,$v)).']';
			foreach($v as $k=>$val)$v[$k]=var_export(strval($k),true).':'.call_user_func(__FUNCTION__,$val);
			return '{'.join(',',$v).'}';
		}
		else return var_export($v,true);
		return '{'.$txt.'}';
	}
}

Initial URL
http://kirie.no/

Initial Description
A fallback for php's json_encode functionality. What do you think?

Initial Title
fast and light json_encode

Initial Tags
php, json

Initial Language
PHP