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

Last change on this file since 1912 was 1912, checked in by rub, 17 years ago

Update svn properties (svn:eol-style and svn:keywords)

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