e98c5a57a52bdaf7e43938e4fc01bb2b0bb1a0b7
[acontent.git] / docs / updater / include / json.inc.php
1 <?php
2 /************************************************************************/
3 /* AContent                                                             */
4 /************************************************************************/
5 /* Copyright (c) 2010                                                   */
6 /* Inclusive Design Institute                                           */
7 /*                                                                      */
8 /* This program is free software. You can redistribute it and/or        */
9 /* modify it under the terms of the GNU General Public License          */
10 /* as published by the Free Software Foundation.                        */
11 /************************************************************************/
12
13 // my simple JSON encode/decode function, similar to PHP5.2 
14 function json_encode_result($phpdata) {
15         if (!$phpdata) return "[]";
16         
17         if(is_array($phpdata)) 
18                 return php2js_array($phpdata);
19         else
20                 return php2js_object($phpdata);
21 }
22
23
24 //function json_decode($jsonata) {
25 //      return $phpdata;
26 //}
27
28
29 // convert a PHP object to javascript object
30 function php2js_object($phpobj) {
31         $str = ""; 
32         
33         if (!is_array($phpobj)) return "[]";
34         
35         foreach($phpobj as $col => $val) {
36           if($str == "")
37             $str = $col .":'" . escapeString($val) . "'";
38           else
39             $str = $str . "," . $col .":'" . escapeString($val) . "'";
40         }
41         
42         return "{" . $str . "}";
43 }
44
45 // convert a PHP object to javascript object
46 function php2js_array($phparr) {
47         $str = "";
48
49   if (!is_array($phparr)) return "[]";
50
51         foreach ($phparr as $e) {
52           if($str == "") 
53                         $str = php2js_object($e) ;
54           else
55             $str = $str . "," . php2js_object($e);
56         }
57         
58         return "[" . $str . "]";
59 }
60
61 function escapeString($string) {
62     $escape = array(
63     "\r\n" => '\n',
64     "\r"   => '\n',
65     "\n"   => '\n',
66     "/"    => '\/'
67     );
68
69     return str_replace(array_keys($escape), array_values($escape), addslashes($string));
70 }       
71
72 ?>