Библиотека для cis, online, cms1
This commit is contained in:
commit
3c2e614d87
269 changed files with 39854 additions and 0 deletions
97
core/json.php
Normal file
97
core/json.php
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* http://phptunes.blogspot.com/2007/01/phpjson.html
|
||||
*/
|
||||
class json
|
||||
{
|
||||
static $DEFAULT_ENCODING = 'windows-1251//IGNORE';
|
||||
static $INTERNAL_ENCODING = 'utf-8';
|
||||
|
||||
static function encode($var)
|
||||
{
|
||||
return php2js($var); //json_encode(self::prepare($var, array('self', 'unicode_encode')));
|
||||
}
|
||||
|
||||
// Ïðåîáðàçîâàíèå json â ìàññèâ
|
||||
static function decode($var, $encoding = 'utf-8', $is_array = false)
|
||||
{
|
||||
if ($encoding != 'utf-8') $var = iconv($encoding, self::$INTERNAL_ENCODING, $var);
|
||||
return self::prepare(json_decode($var, $is_array), array('self', 'unicode_decode'));
|
||||
}
|
||||
|
||||
static function ident($var)
|
||||
{
|
||||
return $var;
|
||||
}
|
||||
|
||||
/* windows-1251 -> utf-8 */
|
||||
static function unicode_encode($var)
|
||||
{
|
||||
return @iconv(self::$DEFAULT_ENCODING, self::$INTERNAL_ENCODING, $var);
|
||||
}
|
||||
|
||||
/* utf-8 -> windows-1251 */
|
||||
static function unicode_decode($var)
|
||||
{
|
||||
return @iconv(self::$INTERNAL_ENCODING, self::$DEFAULT_ENCODING, $var);
|
||||
}
|
||||
|
||||
static function prepare($var, $encode)
|
||||
{
|
||||
if (is_array($var)) {
|
||||
$new = array();
|
||||
foreach ($var as $k => $v) {
|
||||
$new[self::prepare($k, $encode)] = self::prepare($v, $encode);
|
||||
}
|
||||
$var = $new;
|
||||
} elseif (is_object($var)) {
|
||||
// Áûëî ïðåîáðàçîâàíèå òèïà äëÿ îòîáðàæåíèÿ !!
|
||||
$vars = get_object_vars($var);
|
||||
foreach ($vars as $m => $v) {
|
||||
$var->$m = self::prepare($var->$m, $encode);
|
||||
}
|
||||
} elseif (is_string($var)) {
|
||||
$var = call_user_func($encode, $var);
|
||||
}
|
||||
return $var;
|
||||
}
|
||||
}
|
||||
|
||||
function php2js($a = false) {
|
||||
if (is_null($a)) return 'null';
|
||||
if ($a === false) return 'false';
|
||||
if ($a === true) return 'true';
|
||||
if (is_scalar($a)) {
|
||||
if (is_float($a)) {
|
||||
// Always use "." for floats.
|
||||
$a = str_replace(",", ".", strval($a));
|
||||
} else if (is_int($a)) {
|
||||
return $a;
|
||||
}
|
||||
// All scalars are converted to strings to avoid indeterminism.
|
||||
// PHP's "1" and 1 are equal for all PHP operators, but
|
||||
// JS's "1" and 1 are not. So if we pass "1" or 1 from the PHP backend,
|
||||
// we should get the same result in the JS frontend (string).
|
||||
// Character replacements for JSON.
|
||||
static $jsonReplaces = array(
|
||||
array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'),
|
||||
array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
|
||||
return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"';
|
||||
}
|
||||
$isList = true;
|
||||
for ($i = 0, reset($a);$i < count($a);$i++, next($a)) {
|
||||
if (key($a) !== $i) {
|
||||
$isList = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
$result = array();
|
||||
if ($isList) {
|
||||
foreach($a as $v) $result[] = php2js($v);
|
||||
return '[' . join(',', $result) . ']';
|
||||
} else {
|
||||
foreach($a as $k => $v) $result[] = php2js($k) . ':' . php2js($v);
|
||||
return '{' . join(',', $result) . '}';
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue