13 * to programmers of the C-family of languages, including C, C++, C#, Java, |
13 * to programmers of the C-family of languages, including C, C++, C#, Java, |
14 * JavaScript, Perl, TCL, and many others. These properties make JSON an |
14 * JavaScript, Perl, TCL, and many others. These properties make JSON an |
15 * ideal data-interchange language. |
15 * ideal data-interchange language. |
16 * |
16 * |
17 * This package provides a simple encoder and decoder for JSON notation. It |
17 * This package provides a simple encoder and decoder for JSON notation. It |
18 * is intended for use with client-side Javascript applications that make |
18 * is intended for use with client-side JavaScript applications that make |
19 * use of HTTPRequest to perform server communication functions - data can |
19 * use of HTTPRequest to perform server communication functions - data can |
20 * be encoded into JSON notation for use in a client-side javascript, or |
20 * be encoded into JSON notation for use in a client-side javaScript, or |
21 * decoded from incoming Javascript requests. JSON format is native to |
21 * decoded from incoming JavaScript requests. JSON format is native to |
22 * Javascript, and can be directly eval()'ed with no further parsing |
22 * JavaScript, and can be directly eval()'ed with no further parsing |
23 * overhead |
23 * overhead |
24 * |
24 * |
25 * All strings should be in ASCII or UTF-8 format! |
25 * All strings should be in ASCII or UTF-8 format! |
26 * |
26 * |
27 * LICENSE: Redistribution and use in source and binary forms, with or |
27 * LICENSE: Redistribution and use in source and binary forms, with or |
250 /** |
250 /** |
251 * encodes an arbitrary variable into JSON format (and sends JSON Header) |
251 * encodes an arbitrary variable into JSON format (and sends JSON Header) |
252 * |
252 * |
253 * @param mixed $var any number, boolean, string, array, or object to be encoded. |
253 * @param mixed $var any number, boolean, string, array, or object to be encoded. |
254 * see argument 1 to Services_JSON() above for array-parsing behavior. |
254 * see argument 1 to Services_JSON() above for array-parsing behavior. |
255 * if var is a strng, note that encode() always expects it |
255 * if var is a string, note that encode() always expects it |
256 * to be in ASCII or UTF-8 format! |
256 * to be in ASCII or UTF-8 format! |
257 * |
257 * |
258 * @return mixed JSON string representation of input var or an error if a problem occurs |
258 * @return mixed JSON string representation of input var or an error if a problem occurs |
259 * @access public |
259 * @access public |
260 */ |
260 */ |
266 /** |
266 /** |
267 * encodes an arbitrary variable into JSON format without JSON Header - warning - may allow XSS!!!!) |
267 * encodes an arbitrary variable into JSON format without JSON Header - warning - may allow XSS!!!!) |
268 * |
268 * |
269 * @param mixed $var any number, boolean, string, array, or object to be encoded. |
269 * @param mixed $var any number, boolean, string, array, or object to be encoded. |
270 * see argument 1 to Services_JSON() above for array-parsing behavior. |
270 * see argument 1 to Services_JSON() above for array-parsing behavior. |
271 * if var is a strng, note that encode() always expects it |
271 * if var is a string, note that encode() always expects it |
272 * to be in ASCII or UTF-8 format! |
272 * to be in ASCII or UTF-8 format! |
273 * |
273 * |
274 * @return mixed JSON string representation of input var or an error if a problem occurs |
274 * @return mixed JSON string representation of input var or an error if a problem occurs |
275 * @access public |
275 * @access public |
276 */ |
276 */ |
287 /** |
287 /** |
288 * PRIVATE CODE that does the work of encodes an arbitrary variable into JSON format |
288 * PRIVATE CODE that does the work of encodes an arbitrary variable into JSON format |
289 * |
289 * |
290 * @param mixed $var any number, boolean, string, array, or object to be encoded. |
290 * @param mixed $var any number, boolean, string, array, or object to be encoded. |
291 * see argument 1 to Services_JSON() above for array-parsing behavior. |
291 * see argument 1 to Services_JSON() above for array-parsing behavior. |
292 * if var is a strng, note that encode() always expects it |
292 * if var is a string, note that encode() always expects it |
293 * to be in ASCII or UTF-8 format! |
293 * to be in ASCII or UTF-8 format! |
294 * |
294 * |
295 * @return mixed JSON string representation of input var or an error if a problem occurs |
295 * @return mixed JSON string representation of input var or an error if a problem occurs |
296 * @access public |
296 * @access public |
297 */ |
297 */ |
803 |
803 |
804 } elseif (($chrs{$c} == $top['delim']) && |
804 } elseif (($chrs{$c} == $top['delim']) && |
805 ($top['what'] == SERVICES_JSON_IN_STR) && |
805 ($top['what'] == SERVICES_JSON_IN_STR) && |
806 (($this->strlen8($this->substr8($chrs, 0, $c)) - $this->strlen8(rtrim($this->substr8($chrs, 0, $c), '\\'))) % 2 != 1)) { |
806 (($this->strlen8($this->substr8($chrs, 0, $c)) - $this->strlen8(rtrim($this->substr8($chrs, 0, $c), '\\'))) % 2 != 1)) { |
807 // found a quote, we're in a string, and it's not escaped |
807 // found a quote, we're in a string, and it's not escaped |
808 // we know that it's not escaped becase there is _not_ an |
808 // we know that it's not escaped because there is _not_ an |
809 // odd number of backslashes at the end of the string so far |
809 // odd number of backslashes at the end of the string so far |
810 array_pop($stk); |
810 array_pop($stk); |
811 //print("Found end of string at {$c}: ".$this->substr8($chrs, $top['where'], (1 + 1 + $c - $top['where']))."\n"); |
811 //print("Found end of string at {$c}: ".$this->substr8($chrs, $top['where'], (1 + 1 + $c - $top['where']))."\n"); |
812 |
812 |
813 } elseif (($chrs{$c} == '[') && |
813 } elseif (($chrs{$c} == '[') && |