fast and light json_encode


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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. }

URL: http://kirie.no/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.