source: trunk/include/php_compat/json_encode.php @ 6126

Last change on this file since 6126 was 6126, checked in by plg, 14 years ago

bug 1652 fixed: json_encode function is required for admin.php?fckb_tags=1
(used in the new tags widget)

File size: 2.5 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2010 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24function json_encode($data) {
25  switch (gettype($data)) {
26    case 'boolean':
27      return ($data ? 'true' : 'false');
28    case 'null':
29    case 'NULL':
30      return 'null';
31    case 'integer':
32    case 'double':
33      return $data;
34    case 'string':
35      return '"'. str_replace(array("\\",'"',"/","\n","\r","\t"), array("\\\\",'\"',"\\/","\\n","\\r","\\t"), $data) .'"';
36    case 'object':
37    case 'array':
38      if ($data === array()) return '[]'; # empty array
39      if (range(0, count($data) - 1) !== array_keys($data) ) { # string keys, unordered, non-incremental keys, .. - whatever, make object
40        $out = "\n".'{';
41        foreach($data as $key => $value) {
42          $out .= json_encode((string) $key) . ':' . json_encode($value) . ',';
43        }
44        $out = substr($out, 0, -1) . "\n". '}';
45      }else{
46        # regular array
47        $out = "\n".'[' . join("\n".',', array_map('json_encode', $data)) ."\n".']';
48      }
49      return $out;
50  }
51}
52
53?>
Note: See TracBrowser for help on using the repository browser.