source: trunk/include/ws_protocols/json_encoder.php @ 2297

Last change on this file since 2297 was 2297, checked in by plg, 16 years ago

Modification: new header on PHP files, PhpWebGallery renamed Piwigo.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 4.9 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008      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// +-----------------------------------------------------------------------+
24// | PhpWebGallery - a PHP based picture gallery                           |
25// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
26// +-----------------------------------------------------------------------+
27// | branch        : BSF (Best So Far)
28// | file          : $URL: svn+ssh://rvelices@svn.gna.org/svn/phpwebgallery/trunk/action.php $
29// | last update   : $Date: 2008-04-04 22:57:23 +0000 (Fri, 04 Apr 2008) $
30// | last modifier : $Author: plg $
31// | revision      : $Rev: 2297 $
32// +-----------------------------------------------------------------------+
33// | This program is free software; you can redistribute it and/or modify  |
34// | it under the terms of the GNU General Public License as published by  |
35// | the Free Software Foundation                                          |
36// |                                                                       |
37// | This program is distributed in the hope that it will be useful, but   |
38// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
39// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
40// | General Public License for more details.                              |
41// |                                                                       |
42// | You should have received a copy of the GNU General Public License     |
43// | along with this program; if not, write to the Free Software           |
44// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
45// | USA.                                                                  |
46// +-----------------------------------------------------------------------+
47
48
49#_____________________  PHP 5.2
50if (! function_exists('json_encode')) {
51  function json_encode($data) {
52    switch (gettype($data)) {
53      case 'boolean':
54        return ($data ? 'true' : 'false');
55      case 'null':
56      case 'NULL':
57        return 'null';
58      case 'integer':
59      case 'double':
60        return $data;
61      case 'string':
62        return '"'. str_replace(array("\\",'"',"/","\n","\r","\t"), array("\\\\",'\"',"\\/","\\n","\\r","\\t"), $data) .'"';
63      case 'object':
64      case 'array':
65        if ($data === array()) return '[]'; # empty array
66        if (range(0, count($data) - 1) !== array_keys($data) ) { # string keys, unordered, non-incremental keys, .. - whatever, make object
67          $out = "\n".'{';
68          foreach($data as $key => $value) {
69            $out .= json_encode((string) $key) . ':' . json_encode($value) . ',';
70          }
71          $out = substr($out, 0, -1) . "\n". '}';
72        }else{
73          # regular array
74          $out = "\n".'[' . join("\n".',', array_map('json_encode', $data)) ."\n".']';
75        }
76        return $out;
77    }
78  }
79}
80
81class PwgJsonEncoder extends PwgResponseEncoder
82{
83  function encodeResponse($response)
84  {
85    $respClass = strtolower( get_class($response) );
86    if ($respClass=='pwgerror')
87    {
88      return json_encode(
89        array(
90          'stat' => 'fail',
91          'err' => $response->code(),
92          'message' => $response->message(),
93          )
94      );
95    }
96    parent::flattenResponse($response);
97    return json_encode(
98        array(
99          'stat' => 'ok',
100          'result' => $response,
101      )
102    );
103  }
104
105  function getContentType()
106  {
107    return 'text/plain';
108  }
109}
110
111?>
Note: See TracBrowser for help on using the repository browser.