note 100810 modified in function.json-encode by danbrown

From: Date: Mon, 08 Nov 2010 17:44:18 +0000
Subject: note 100810 modified in function.json-encode by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-173862@lists.php.net to get a copy of this message
For users of php 5.1.6 or lower, a native json_encode function. This version handles objects, and makes proper distinction between [lists] and {associative arrays}, mixed arrays work as well. It can handle newlines and quotes in both keys and data. This function does _not_ convert non-ascii symbols to "\uXXXX" format as does json_encode. Besides that, it gives the exact same output as json_encode. <?php function __json_encode( $data ) { if( is_array($data) || is_object($data) ) { # See php.net / is_array $is_assoc = (is_array($data) && 0 !== count(array_diff_key($data, array_keys(array_keys($data))))); if( $is_assoc || is_object($data) ) { $items = Array(); foreach( $data as $key => $value ) { $items[] = __json_encode("$key") . ':' . __json_encode($value); } $json = '{' . implode(',', $items) . '}'; } else { $json = '[' . implode(',', array_map('__json_encode', $data) ) . ']'; } } elseif( is_int($data) || is_float($data) ) { $json = strtolower($data); # like 10e99 } elseif( null === $data ) { $json = 'null'; } else { $json = '"' . addcslashes($data, "\"\\\n\r") . '"'; # String } return $json; } ?> Example: <?php $data = new StdClass(); $data->foo = "bar"; $data->dict = Array('1' => 1, '2' => 2, '3' => 'three', 'four'); $data->list = Array('one','two','three','',null,new StdClass(),Array(),10e99); json_encode($data) === __json_encode($data); // true ?> --was-- For users of php 5.1.6 or lower, a native json_encode function. This version handles objects, and makes proper distinction between [lists] and {associative arrays}, mixed arrays work as well. It can handle newlines and quotes in both keys and data. This function does _not_ convert non-ascii symbols to "\uXXXX" format as does json_encode. Besides that, it gives the exact same output as json_encode. <?php function __json_encode( $data ) { if( is_array($data) || is_object($data) ) { # See php.net / is_array $is_assoc = (is_array($data) && 0 !== count(array_diff_key($data, array_keys(array_keys($data))))); if( $is_assoc || is_object($data) ) { $items = Array(); foreach( $data as $key => $value ) { $items[] = __json_encode("$key") . ':' . __json_encode($value); } $json = '{' . implode(',', $items) . '}'; } else { $json = '[' . implode(',', array_map('__json_encode', $data) ) . ']'; } } elseif( is_int($data) || is_float($data) ) { $json = strtolower($data); # like 10e99 } elseif( null === $data ) { $json = 'null'; } else { $json = '"' . addcslashes($data, "\"\\\n\r") . '"'; # String } return $json; } ?> Example: <?php $data = new StdClass(); $data->foo = "bar"; $data->dict = Array('1' => 1, '2' => 2, '3' => 'three', 'four'); $data->list = Array('one','two','three','',null,new StdClass(),Array(),10e99); json_encode($data) === __json_encode($data); // true http://php.net/manual/en/function.json-encode.php

« previous php.notes (#173862) next »