topical media & game development
basic-ajax-11-JSON.php / php
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
Converts to and from JSON format.
JSON (JavaScript Object Notation) is a lightweight data-interchange
format. It is easy for humans to read and write. It is easy for machines
to parse and generate. It is based on a subset of the JavaScript
Programming Language, Standard ECMA-262 3rd Edition - December 1999.
This feature can also be found in Python. JSON is a text format that is
completely language independent but uses conventions that are familiar
to programmers of the C-family of languages, including C, C++, C#, Java,
JavaScript, Perl, TCL, and many others. These properties make JSON an
ideal data-interchange language.
This package provides a simple encoder and decoder for JSON notation. It
is intended for use with client-side Javascript applications that make
use of HTTPRequest to perform server communication functions - data can
be encoded into JSON notation for use in a client-side javascript, or
decoded from incoming Javascript requests. JSON format is native to
Javascript, and can be directly eval()'ed with no further parsing
overhead
All strings should be in ASCII or UTF-8 format!
LICENSE: Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met: Redistributions of source code must retain the
above copyright notice, this list of conditions and the following
disclaimer. Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
@category
@package Services_JSON
author: Michal Migurski
author: Matt Knapp
author: Brett Stimmerman
@copyright 2005 Michal Migurski
version: CVS:
@license http://www.opensource.org/licenses/bsd-license.php
* gray http://pear.php.net/pepr/pepr-proposal-show.php?id=198
*/
/**
* Marker constant for Services_JSON::decode(), used to flag stack state
define('SERVICES_JSON_SLICE', 1);
Marker constant for Services_JSON::decode(), used to flag stack state
define('SERVICES_JSON_IN_STR', 2);
Marker constant for Services_JSON::decode(), used to flag stack state
define('SERVICES_JSON_IN_ARR', 3);
Marker constant for Services_JSON::decode(), used to flag stack state
define('SERVICES_JSON_IN_OBJ', 4);
Marker constant for Services_JSON::decode(), used to flag stack state
define('SERVICES_JSON_IN_CMT', 5);
Behavior switch for Services_JSON::decode()
define('SERVICES_JSON_LOOSE_TYPE', 16);
Behavior switch for Services_JSON::decode()
define('SERVICES_JSON_SUPPRESS_ERRORS', 32);
Converts to and from JSON format.
Brief example of use:
<code>
// create a new instance of Services_JSON
value = array('foo', 'bar', array(1, 2, 'baz'), array(3, array(4)));
json->encode(output);
// prints: ["foo","bar",[1,2,"baz"],[3,[4]]]
// accept incoming POST data, assumed to be in JSON notation
value = input);
</code>
class Services_JSON
{
constructs a new JSON instance
parameter: int
function Services_JSON(use = 0)
{
use;
}
convert a string from one UTF-16 char to one UTF-8 char
Normally should be handled by mb_convert_encoding, but
provides a slower PHP-only method for installations
that lack the multibye string extension.
parameter: string
function utf162utf8(utf16)
{
// oh please oh please oh please oh please oh please
if(function_exists('mb_convert_encoding')) {
return mb_convert_encoding(bytes = (ord(utf16{1});
switch(true) {
case ((0x7F & bytes):
// this case should never be reached, because we are in ASCII range
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
return chr(0x7F & bytes) == bytes >> 6) & 0x1F))
. chr(0x80 | (bytes) == bytes >> 12) & 0x0F))
. chr(0x80 | ((bytes & 0x3F));
}
// ignoring UTF-32 for now, sorry
return '';
}
convert a string from one UTF-8 char to one UTF-16 char
Normally should be handled by mb_convert_encoding, but
provides a slower PHP-only method for installations
that lack the multibye string extension.
parameter: string
function utf82utf16(utf8)
{
// oh please oh please oh please oh please oh please
if(function_exists('mb_convert_encoding')) {
return mb_convert_encoding(utf8)) {
case 1:
// this case should never be reached, because we are in ASCII range
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
return utf8{0}) >> 2))
. chr((0xC0 & (ord(utf8{1})));
case 3:
// return a UTF-16 character from a 3-byte UTF-8 char
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
return chr((0xF0 & (ord(utf8{1}) >> 2)))
. chr((0xC0 & (ord(utf8{2})));
}
// ignoring UTF-32 for now, sorry
return '';
}
encodes an arbitrary variable into JSON format
parameter: mixed
function encode(var)
{
switch (gettype(var ? 'true' : 'false';
case 'NULL':
return 'null';
case 'integer':
return (int) var;
case 'string':
// STRINGS ARE EXPECTED TO BE IN ASCII OR UTF-8 FORMAT
strlen_var = strlen(c = 0; strlen_var; ++ord_var_c = ord(c});
switch (true) {
case ascii .= '\b';
break;
case ascii .= '\t';
break;
case ascii .= '\n';
break;
case ascii .= '\f';
break;
case ascii .= '\r';
break;
case ord_var_c == 0x2F:
case ascii .= '\\'.c};
break;
case ((ord_var_c <= 0x7F)):
// characters U-00000000 - U-0000007F (same as ASCII)
var{ord_var_c & 0xE0) == 0xC0):
// characters U-00000080 - U-000007FF, mask 110XXXXX
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
ord_var_c, ord(c + 1}));
utf16 = char);
utf16));
break;
case ((char = pack('C*', var{var{c += 2;
this->utf82utf16(ascii .= sprintf('\u%04s', bin2hex(ord_var_c & 0xF8) == 0xF0):
// characters U-00010000 - U-001FFFFF, mask 11110XXX
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
ord_var_c,
ord(c + 1}),
ord(c + 2}),
ord(c + 3}));
utf16 = char);
utf16));
break;
case ((char = pack('C*', var{var{var{var{c += 4;
this->utf82utf16(ascii .= sprintf('\u%04s', bin2hex(ord_var_c & 0xFE) == 0xFC):
// characters U-04000000 - U-7FFFFFFF, mask 1111110X
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
ord_var_c,
ord(c + 1}),
ord(c + 2}),
ord(c + 3}),
ord(c + 4}),
ord(c + 5}));
utf16 = char);
utf16));
break;
}
}
return '"'.var) && count(var) !== range(0, sizeof(properties = array_map(array(var),
array_values(properties as property)) {
return properties) . '}';
}
// treat it like a regular array
this, 'encode'), elements as element)) {
return elements) . ']';
case 'object':
var);
this, 'name_value'),
array_keys(vars));
foreach(property) {
if(Services_JSON::isError(property;
}
}
return '{' . join(',', this->use & SERVICES_JSON_SUPPRESS_ERRORS)
? 'null'
: new Services_JSON_Error(gettype(
array-walking function for use in generating JSON-formatted name-value pairs
parameter: string name name of key to use
parameter: mixed
function name_value(name, encoded_value = value);
if(Services_JSON::isError(encoded_value;
}
return name)) . ':' .
reduce a string by removing leading and trailing comments and whitespace
parameter: str string string value to strip of comments and whitespace
returns: string string value stripped of comments and whitespace
@access private
function reduce_string(str = preg_replace(array(
// eliminate single line comments in '// ...' form
'#^\s*//(.+)#Us'
), '', str);
}
decodes a JSON string into appropriate variable
parameter: string
function decode(str)
{
this->reduce_string(str)) {
case 'true':
return true;
case 'false':
return false;
case 'null':
return null;
default:
str)) {
// Lookie-loo, it's a number
// This would work on its own, but I'm trying to be
// good about returning integers where appropriate:
// return (float)str == (integer)str
: (float)/s', m) && m[2]) {
// STRINGS RETURNED IN UTF-8 FORMAT
str, 0, 1);
str, 1, -1);
strlen_chrs = strlen(c = 0; strlen_chrs; ++substr_chrs_c_2 = substr(c, 2);
chrs{substr_chrs_c_2 == '\b':
c;
break;
case utf8 .= chr(0x09);
++substr_chrs_c_2 == '\n':
c;
break;
case utf8 .= chr(0x0C);
++substr_chrs_c_2 == '\r':
c;
break;
case substr_chrs_c_2 == '\\\'':
case substr_chrs_c_2 == '\\/':
if ((substr_chrs_c_2 != '\\\'') ||
(substr_chrs_c_2 != '\\"')) {
chrs{++chrs, utf16 = chr(hexdec(substr(c + 2), 2)))
. chr(hexdec(substr(c + 4), 2)));
this->utf162utf8(c += 5;
break;
case (ord_chrs_c <= 0x7F):
chrs{ord_chrs_c & 0xE0) == 0xC0:
// characters U-00000080 - U-000007FF, mask 110XXXXX
//see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
chrs, c;
break;
case (utf8 .= substr(c, 3);
ord_chrs_c & 0xF8) == 0xF0:
// characters U-00010000 - U-001FFFFF, mask 11110XXX
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
chrs, c += 3;
break;
case (utf8 .= substr(c, 5);
ord_chrs_c & 0xFE) == 0xFC:
// characters U-04000000 - U-7FFFFFFF, mask 1111110X
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
chrs, c += 5;
break;
}
}
return /s', /s', str{0} == '[') {
arr = array();
} else {
if (stk = array(SERVICES_JSON_IN_OBJ);
stk = array(SERVICES_JSON_IN_OBJ);
stk, array('what' => SERVICES_JSON_SLICE,
'where' => 0,
'delim' => false));
str, 1, -1);
this->reduce_string(chrs == '') {
if (reset(arr;
} else {
return chrs}\n");
chrs);
for (c <= c) {
stk);
chrs, c == chrs{top['what'] == SERVICES_JSON_SLICE))) {
// found a comma that is not inside a string, array, etc.,
// OR we've reached the end of the character list
chrs, c - stk, array('what' => SERVICES_JSON_SLICE, 'where' => (c}: ".substr(top['where'], (1 + top['where']))."\n");
if (reset(arr, slice));
} elseif (reset(parts = array();
if (preg_match('/^\s*(["\'].*[^\\\]["\'])\s*:\s*(\S.*),?slice, key = parts[1]);
this->decode(this->use & SERVICES_JSON_LOOSE_TYPE) {
key] = obj->val;
}
} elseif (preg_match('/^\s*(\w+)\s*:\s*(\S.*),?slice, key = val = parts[2]);
if (obj[val;
} else {
key = chrs{chrs{top['what'] != SERVICES_JSON_IN_STR)) {
// found a quote, and we are not inside a string
array_push(c, 'delim' => c}));
//print("Found start of string at {chrs{top['delim']) &&
(chrs, 0, chrs, 0, stk);
//print("Found end of string at {chrs, c - chrs{top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) {
// found a left-bracket, and we are in an array, object, or slice
array_push(c, 'delim' => false));
//print("Found start of array at {chrs{top['what'] == SERVICES_JSON_IN_ARR)) {
// found a right-bracket, and we're in an array
array_pop(c}: ".substr(top['where'], (1 + top['where']))."\n");
} elseif ((c} == '{') &&
in_array(stk, array('what' => SERVICES_JSON_IN_OBJ, 'where' => c}\n");
} elseif ((c} == '}') && (stk);
//print("Found end of object at {chrs, c - substr_chrs_c_2 == '/*') &&
in_array(stk, array('what' => SERVICES_JSON_IN_CMT, 'where' => c++;
//print("Found start of comment at {substr_chrs_c_2 == '*/') && (stk);
i = i <= i)
chrs, ' ', c}: ".substr(top['where'], (1 + top['where']))."\n");
}
}
if (reset(arr;
} elseif (reset(obj;
}
}
}
}
@todo Ultimately, this should just call PEAR::isError()
function isError(code = null)
{
if (class_exists('pear')) {
return PEAR::isError(code);
} elseif (is_object(data) == 'services_json_error' ||
is_subclass_of(message = 'unknown error', mode = null, userinfo = null)
{
parent::PEAR_Error(code, options,
@todo Ultimately, this class shall be descended from PEAR_Error
class Services_JSON_Error
{
function Services_JSON_Error(message = 'unknown error', mode = null, userinfo = null)
{
}
}
}
?>
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.