Posted By

kirie on 04/09/09


Tagged

php encode json


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

nkrstevski
kirie
muoto


fast and light json_encode


 / Published in: PHP
 

URL: http://kirie.no/

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

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

Report this snippet  

You need to login to post a comment.