json.php
378 lines
| 1 | <?php |
| 2 | /* |
| 3 | *************************************************************************** |
| 4 | * Copyright (C) 2007 by Cesar D. Rodas * |
| 5 | * crodas@phpy.org * |
| 6 | * * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining * |
| 8 | * a copy of this software and associated documentation files (the * |
| 9 | * "Software"), to deal in the Software without restriction, including * |
| 10 | * without limitation the rights to use, copy, modify, merge, publish, * |
| 11 | * distribute, sublicense, and/or sell copies of the Software, and to * |
| 12 | * permit persons to whom the Software is furnished to do so, subject to * |
| 13 | * the following conditions: * |
| 14 | * * |
| 15 | * The above copyright notice and this permission notice shall be * |
| 16 | * included in all copies or substantial portions of the Software. * |
| 17 | * * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * |
| 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * |
| 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.* |
| 21 | * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR * |
| 22 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * |
| 23 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * |
| 24 | * OTHER DEALINGS IN THE SOFTWARE. * |
| 25 | *************************************************************************** |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * Serialize and Unserialize PHP Objects and array |
| 30 | * into JSON notation. |
| 31 | * |
| 32 | * @category Javascript |
| 33 | * @package JSON |
| 34 | * @author Cesar D. Rodas <crodas@phpy.org> |
| 35 | * @copyright 2007 Cesar D. Rodas |
| 36 | * @license http://www.opensource.org/licenses/bsd-license.php BSD License |
| 37 | * @version 1.0 |
| 38 | * @link http://cesars.users.phpclasses.org/json |
| 39 | */ |
| 40 | |
| 41 | define('IN_NOWHERE',0); |
| 42 | define('IN_STRING',1); |
| 43 | define('IN_OBJECT',2); |
| 44 | define('IN_ATOMIC',3); |
| 45 | define('IN_ASSIGN',4); |
| 46 | define('IN_ENDSTMT',5); |
| 47 | define('IN_ARRAY',6); |
| 48 | |
| 49 | /** |
| 50 | * JSON |
| 51 | * |
| 52 | * This class serilize an PHP OBJECT or an ARRAY into JSON |
| 53 | * notation. Also convert a JSON text into a PHP OBJECT or |
| 54 | * array. |
| 55 | * |
| 56 | * @category Javascript |
| 57 | * @package JSON |
| 58 | * @author Cesar D. Rodas <crodas@phpy.org> |
| 59 | * @copyright 2007 Cesar D. Rodas |
| 60 | * @license http://www.opensource.org/licenses/bsd-license.php BSD License |
| 61 | * @version 1.0 |
| 62 | * @link http://cesars.users.phpclasses.org/json |
| 63 | */ |
| 64 | class JSON |
| 65 | { |
| 66 | /** |
| 67 | * Was parsed with an error? |
| 68 | * |
| 69 | * var bool |
| 70 | * @access private |
| 71 | */ |
| 72 | var $error; |
| 73 | |
| 74 | function Json() { |
| 75 | $this->error = false; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Serialize |
| 80 | * |
| 81 | * Serialize a PHP OBJECT or an ARRAY into |
| 82 | * JSON notation. |
| 83 | * |
| 84 | * param mixed $obj Object or array to serialize |
| 85 | * return string JSON. |
| 86 | */ |
| 87 | function serialize($obj) { |
| 88 | if ( is_object($obj) ) { |
| 89 | $e = get_object_vars($obj); |
| 90 | /* bug reported by Ben Rowe */ |
| 91 | /* Adding default empty array if the */ |
| 92 | /* object doesn't have any property */ |
| 93 | $properties = array(); |
| 94 | foreach ($e as $k => $v) { |
| 95 | $properties[] = $this->_serialize( $k,$v ); |
| 96 | } |
| 97 | return "{".implode(",",$properties)."}"; |
| 98 | } else if ( is_array($obj) ) { |
| 99 | return $this->_serialize('',$obj); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * UnSerialize |
| 105 | * |
| 106 | * Transform an JSON text into a PHP object |
| 107 | * and return it. |
| 108 | * @access public |
| 109 | * @param string $text JSON text |
| 110 | * @return mixed PHP Object, array or false. |
| 111 | */ |
| 112 | function unserialize( $text ) { |
| 113 | $this->error = false; |
| 114 | |
| 115 | return !$this->error ? $this->_unserialize($text) : false; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * UnSerialize |
| 120 | * |
| 121 | * Transform an JSON text into a PHP object |
| 122 | * and return it. |
| 123 | * @access private |
| 124 | * @param string $text JSON text |
| 125 | * @return mixed PHP Object, array or false. |
| 126 | */ |
| 127 | function _unserialize($text) { |
| 128 | $ret = new stdClass; |
| 129 | |
| 130 | while ( $f = $this->getNextToken($text,$i,$type) ) { |
| 131 | switch ( $type ) { |
| 132 | case IN_ARRAY: |
| 133 | $tmp = $this->_unserializeArray($text); |
| 134 | $ret = $tmp[0]; |
| 135 | break; |
| 136 | case IN_OBJECT: |
| 137 | $g=0; |
| 138 | do { |
| 139 | $varName = $this->getNextToken($f,$g,$xType); |
| 140 | if ( $xType != IN_STRING ) { |
| 141 | return false; /* error parsing */ |
| 142 | } |
| 143 | $this->getNextToken($f,$g,$xType); |
| 144 | if ( $xType != IN_ASSIGN) return false; |
| 145 | $value = $this->getNextToken($f,$g,$xType); |
| 146 | |
| 147 | if ( $xType == IN_OBJECT) { |
| 148 | $ret->$varName = $this->unserialize( "{".$value."}" ); |
| 149 | $g--; |
| 150 | } else if ($xType == IN_ARRAY) { |
| 151 | $ret->$varName = $this->_unserializeArray( $value); |
| 152 | $g--; |
| 153 | } else |
| 154 | $ret->$varName = $value; |
| 155 | |
| 156 | $this->getNextToken($f,$g,$xType); |
| 157 | } while ( $xType == IN_ENDSTMT); |
| 158 | break; |
| 159 | default: |
| 160 | $this->error = true; |
| 161 | break 2; |
| 162 | } |
| 163 | } |
| 164 | return $ret; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * JSON Array Parser |
| 169 | * |
| 170 | * This method transform an json-array into a PHP |
| 171 | * array |
| 172 | * @access private |
| 173 | * @param string $text String to parse |
| 174 | * @return Array PHP Array |
| 175 | */ |
| 176 | function _unserializeArray($text) { |
| 177 | $r = array(); |
| 178 | do { |
| 179 | $f = $this->getNextToken($text,$i,$type); |
| 180 | switch ( $type ) { |
| 181 | case IN_STRING: |
| 182 | case IN_ATOMIC: |
| 183 | $r[] = $f; |
| 184 | break; |
| 185 | case IN_OBJECT: |
| 186 | $r[] = $this->unserialize("{".$f."}"); |
| 187 | $i--; |
| 188 | break; |
| 189 | case IN_ARRAY: |
| 190 | $r[] = $this->_unserializeArray($f); |
| 191 | $i--; |
| 192 | break; |
| 193 | |
| 194 | } |
| 195 | $this->getNextToken($text,$i,$type); |
| 196 | } while ( $type == IN_ENDSTMT); |
| 197 | |
| 198 | return $r; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Tokenizer |
| 203 | * |
| 204 | * Return to the Parser the next valid token and the type |
| 205 | * of the token. If the tokenizer fails it returns false. |
| 206 | * |
| 207 | * @access private |
| 208 | * @param string $e Text to extract token |
| 209 | * @param integer $i Start position to search next token |
| 210 | * @param integer $state Variable to get the token type |
| 211 | * @return string|bool Token in string or false on error. |
| 212 | */ |
| 213 | function getNextToken($e, &$i, &$state) { |
| 214 | $state = IN_NOWHERE; |
| 215 | $end = -1; |
| 216 | $start = -1; |
| 217 | while ( $i < strlen($e) && $end == -1 ) { |
| 218 | switch( $e[$i] ) { |
| 219 | /* objects */ |
| 220 | case "{": |
| 221 | case "[": |
| 222 | $_tag = $e[$i]; |
| 223 | $_endtag = $_tag == "{" ? "}" : "]"; |
| 224 | if ( $state == IN_NOWHERE ) { |
| 225 | $start = $i+1; |
| 226 | switch ($state) { |
| 227 | case IN_NOWHERE: |
| 228 | $aux = 1; /* for loop objects */ |
| 229 | $state = $_tag == "{" ? IN_OBJECT : IN_ARRAY; |
| 230 | break; |
| 231 | default: |
| 232 | break 2; /* exit from switch and while */ |
| 233 | } |
| 234 | while ( ++$i && $i < strlen($e) && $aux != 0 ) { |
| 235 | switch( $e[$i] ) { |
| 236 | case $_tag: |
| 237 | $aux++; |
| 238 | break; |
| 239 | case $_endtag: |
| 240 | $aux--; |
| 241 | break; |
| 242 | } |
| 243 | } |
| 244 | $end = $i-1; |
| 245 | } |
| 246 | break; |
| 247 | |
| 248 | case '"': |
| 249 | case "'": |
| 250 | $state = IN_STRING; |
| 251 | $buf = ""; |
| 252 | while ( ++$i && $i < strlen($e) && $e[$i] != '"' ) { |
| 253 | if ( $e[$i] == "\\") |
| 254 | $i++; |
| 255 | $buf .= $e[$i]; |
| 256 | } |
| 257 | $i++; |
| 258 | return $buf; |
| 259 | break; |
| 260 | case ":": |
| 261 | $state = IN_ASSIGN; |
| 262 | $end = 1; |
| 263 | break; |
| 264 | case "n": |
| 265 | if ( substr($e,$i,4) == "null" ) { |
| 266 | $i=$i+4; |
| 267 | $state = IN_ATOMIC; |
| 268 | return NULL; |
| 269 | } |
| 270 | else break 2; /* exit from switch and while */ |
| 271 | case "t": |
| 272 | if ( substr($e,$i,4) == "true") { |
| 273 | $state = IN_ATOMIC; |
| 274 | $i=$i+4; |
| 275 | return true; |
| 276 | } |
| 277 | else break 2; /* exit from switch and while */ |
| 278 | break; |
| 279 | case "f": |
| 280 | if ( substr($e,$i,5) == "false") { |
| 281 | $state = IN_ATOMIC; |
| 282 | $i=$i+5; |
| 283 | return false; |
| 284 | } |
| 285 | else break 2; /* exit from switch and while */ |
| 286 | break; |
| 287 | case ",": |
| 288 | $state = IN_ENDSTMT; |
| 289 | $end = 1; |
| 290 | break; |
| 291 | case " ": |
| 292 | case "\t": |
| 293 | case "\r": |
| 294 | case "\n": |
| 295 | break; |
| 296 | case "+": |
| 297 | case "-": |
| 298 | case 0: |
| 299 | case 1: |
| 300 | case 2: |
| 301 | case 3: |
| 302 | case 4: |
| 303 | case 5: |
| 304 | case 6: |
| 305 | case 7: |
| 306 | case 8: |
| 307 | case 9: |
| 308 | case '.': |
| 309 | $state = IN_ATOMIC; |
| 310 | $start = (int)$i; |
| 311 | if ( $e[$i] == "-" || $e[$i] == "+") |
| 312 | $i++; |
| 313 | for ( ; $i < strlen($e) && (is_numeric($e[$i]) || $e[$i] == "." || strtolower($e[$i]) == "e") ;$i++){ |
| 314 | $n = $i+1 < strlen($e) ? $e[$i+1] : ""; |
| 315 | $a = strtolower($e[$i]); |
| 316 | if ( $a == "e" && ($n == "+" || $n == "-")) |
| 317 | $i++; |
| 318 | else if ( $a == "e") |
| 319 | $this->error=true; |
| 320 | } |
| 321 | |
| 322 | $end = $i; |
| 323 | break 2; /* break while too */ |
| 324 | default: |
| 325 | $this->error = true; |
| 326 | |
| 327 | } |
| 328 | $i++; |
| 329 | } |
| 330 | |
| 331 | return $start == -1 || $end == -1 ? false : substr($e, $start, $end - $start); |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Internal Serializer |
| 336 | * |
| 337 | * @param string $key Variable name |
| 338 | * @param mixed $value Value of the variable |
| 339 | * @access private |
| 340 | * @return string Serialized variable |
| 341 | */ |
| 342 | function _serialize ( $key = '', &$value ) { |
| 343 | $r = ''; |
| 344 | if ( $key != '')$r .= "\"${key}\" : "; |
| 345 | if ( is_numeric($value) ) { |
| 346 | $r .= ''.$value.''; |
| 347 | } else if ( is_string($value) ) { |
| 348 | $r .= '"'.$this->toString($value).'"'; |
| 349 | } else if ( is_object($value) ) { |
| 350 | $r .= $this->serialize($value); |
| 351 | } else if ( is_null($value) ) { |
| 352 | $r .= "null"; |
| 353 | } else if ( is_bool($value) ) { |
| 354 | $r .= $value ? "true":"false"; |
| 355 | } else if ( is_array($value) ) { |
| 356 | foreach($value as $k => $v) |
| 357 | $f[] = $this->_serialize('',$v); |
| 358 | $r .= "[".implode(",",$f)."]"; |
| 359 | unset($f); |
| 360 | } |
| 361 | return $r; |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Convert String variables |
| 366 | * |
| 367 | * @param string $e Variable with an string value |
| 368 | * @access private |
| 369 | * @return string Serialized variable |
| 370 | */ |
| 371 | function toString($e) { |
| 372 | $rep = array("\\","\r","\n","\t","'",'"'); |
| 373 | $val = array("\\\\",'\r','\n','\t','\'','\"'); |
| 374 | $e = str_replace($rep, $val, $e); |
| 375 | return $e; |
| 376 | } |
| 377 | } |
| 378 | ?> |