note 109118 modified in function.json-encode by danbrown

From: Date: Tue, 26 Jun 2012 14:00:07 +0000
Subject: note 109118 modified in function.json-encode by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-188842@lists.php.net to get a copy of this message
If you need the JsonSerializable Interface in PHP&#60;5.4... this could help:&#13;&#10;&#13;&#10;&#60;?php&#13;&#10;$fallback_jsonencode = !interface_exists(&#34;JsonSerializable&#34;, false);&#13;&#10;if($fallback_jsonencode){&#13;&#10; interface JsonSerializable{&#13;&#10;&#9;public function jsonSerialize();&#13;&#10; }&#13;&#10; function fallback_json_encode($obj){&#13;&#10;&#9;if(is_numeric($obj)){&#13;&#10;&#9; return $obj;&#13;&#10;&#9;} else if(is_string($obj)){&#13;&#10;&#9; return &#34;\\&#34;&#34;.$obj.&#34;\\&#34;&#34;; &#13;&#10;&#9;} else if(is_bool($obj)){&#13;&#10;&#9; return $obj?&#34;true&#34;:&#34;false&#34;; &#13;&#10;&#9;} else if(is_array($obj)){&#13;&#10;&#9; $parts = array();&#13;&#10;&#9; $keys = array(); &#13;&#10;&#9; //$assocarr = false;&#13;&#10;&#9; foreach($obj AS $k =&#62; $v){&#13;&#10;&#9;&#9;$parts[$k] = fallback_json_encode($v);&#13;&#10;&#9;&#9;//if(!is_numeric($k)) $assocarr = true; &#13;&#10;&#9; }&#13;&#10;&#9; /*if($assocarr){&#13;&#10;&#9; $out = &#34;&#34;; $sep = &#34;\\&#34;&#34;;&#13;&#10;&#9; foreach($parts AS $k =&#62; $v){&#13;&#10;&#9; $out .= $sep.$k.&#34;\\&#34;:&#34;.$v;&#13;&#10;&#9; $sep = &#34;,\\&#34;&#34;;&#13;&#10;&#9; }&#13;&#10;&#9; return &#34;{&#34;.$out.&#34;}&#34;; &#13;&#10;&#9; } else {*/&#13;&#10;&#9; return &#34;[&#34;.implode(&#34;,&#34;,$parts).&#34;]&#34;;&#13;&#10;&#9; //}&#13;&#10;&#9;} else if(is_object($obj)) {&#13;&#10;&#9; if($obj instanceof JsonSerializable) $obj = $obj-&#62;jsonSerialize();&#13;&#10;&#9; $parts = array();&#13;&#10;&#9; foreach($obj AS $k =&#62; $v){&#13;&#10;&#9;&#9;$parts[] = &#34;\\&#34;&#34;.$k.&#34;\\&#34;:&#34;.fallback_json_encode($v); &#13;&#10;&#9; }&#13;&#10;&#13;&#10;&#9; return &#34;{&#34;.implode(&#34;,&#34;,$parts).&#34;}&#34;;&#13;&#10;&#9;} else return &#34;null&#34;; &#13;&#10; } &#13;&#10;}&#13;&#10;?&#62;&#13;&#10;&#13;&#10;Note: This Code runs slightly faster than the PHP 5.4 Implementation on my machine, but it DOES NOT SUPPORT ASSOCIATIVE ARRAYS like that. Just uncomment the commented section for that and it should work. --was-- If you need the JsonSerializable Interface in PHP<5.4... this could help: $fallback_jsonencode = !interface_exists("JsonSerializable", false); if($fallback_jsonencode){ interface JsonSerializable{ public function jsonSerialize(); } function fallback_json_encode($obj){ if(is_numeric($obj)){ return $obj; } else if(is_string($obj)){ return "\"".$obj."\""; } else if(is_bool($obj)){ return $obj?"true":"false"; } else if(is_array($obj)){ $parts = array(); $keys = array(); //$assocarr = false; foreach($obj AS $k => $v){ $parts[$k] = fallback_json_encode($v); //if(!is_numeric($k)) $assocarr = true; } /*if($assocarr){ $out = ""; $sep = "\""; foreach($parts AS $k => $v){ $out .= $sep.$k."\":".$v; $sep = ",\""; } return "{".$out."}"; } else {*/ return "[".implode(",",$parts)."]"; //} } else if(is_object($obj)) { if($obj instanceof JsonSerializable) $obj = $obj->jsonSerialize(); $parts = array(); foreach($obj AS $k => $v){ $parts[] = "\"".$k."\":".fallback_json_encode($v); } return "{".implode(",",$parts)."}"; } else return "null"; } } Note: This Code runs slightly faster than the PHP 5.4 Implementation on my machine, but it DOES NOT SUPPORT ASSOCIATIVE ARRAYS like that. Just uncomment the commented section for that and it should work. http://php.net/manual/en/function.json-encode.php

« previous php.notes (#188842) next »