public static function Encode($var) { if (function_exists('json_encode')) { return json_encode($var); } return minijson_encode($var, false); }
/** * Encodes an array (indexed or associative) as JSON. * * in: array x * in: string indent or bool false to skip beautification * out: string encoded */ function minijson_encode($x, $ri = "") { if (!isset($x) || is_null($x) || is_float($x) && (is_nan($x) || is_infinite($x))) { return "null"; } if ($x === true) { return "true"; } if ($x === false) { return "false"; } if (is_int($x)) { $y = (int) $x; $z = (string) $y; if ($x == $z) { return $z; } $x = (string) $x; } if (is_float($x)) { if (floor($x) == $x) { return sprintf("%.0f", $x); } $rs = sprintf("%.14E", $x); $v = explode("E", $rs); $rs = rtrim($v[0], "0"); if (substr($rs, -1) == ".") { $rs .= "0"; } if ($v[1] != "-0" && $v[1] != "+0") { $rs .= "E" . $v[1]; } return $rs; } if (is_string($x)) { $rs = "\""; $x .= ""; /* * A bit unbelievable: not only does mb_check_encoding * not exist from the start, but also does it not check * reliably – so converting forth and back is the way * they recommend… also, JSON is not binary-safe either… */ $isunicode = false; $mb_encoding = false; if (function_exists('mb_internal_encoding') && function_exists('mb_convert_encoding')) { $mb_encoding = mb_internal_encoding(); mb_internal_encoding("UTF-8"); $z = mb_convert_encoding($x, "UTF-16LE", "UTF-8"); $y = mb_convert_encoding($z, "UTF-8", "UTF-16LE"); $isunicode = $y == $x; } if ($isunicode) { $z = str_split($z, 2); } else { $z = str_split($x); } foreach ($z as $v) { $y = ord($v[0]); if ($isunicode) { $y |= ord($v[1]) << 8; } if ($y == 0) { break; } else { if ($y == 8) { $rs .= "\\b"; } else { if ($y == 9) { $rs .= "\\t"; } else { if ($y == 10) { $rs .= "\\n"; } else { if ($y == 12) { $rs .= "\\f"; } else { if ($y == 13) { $rs .= "\\r"; } else { if ($y == 34) { $rs .= "\\\""; } else { if ($y == 92) { $rs .= "\\\\"; } else { if ($y < 0x20 || $y > 0xfffd || $y >= 0xd800 && $y <= 0xdfff || $y > 0x7e && (!$isunicode || $y < 0xa0)) { $rs .= sprintf("\\u%04X", $y); } else { if ($isunicode && $y > 0x7e) { $rs .= mb_convert_encoding($v, "UTF-8", "UTF-16LE"); } else { $rs .= $v[0]; } } } } } } } } } } } if ($mb_encoding !== false) { mb_internal_encoding($mb_encoding); } return $rs . "\""; } if (is_array($x)) { $k = array_keys($x); $isnum = true; foreach ($k as $v) { if (is_int($v)) { $y = (int) $v; $z = (string) $y; if ($v != $z) { $isnum = false; break; } } else { $isnum = false; break; } } if ($isnum) { /* all array keys are integers */ $s = $k; sort($s, SORT_NUMERIC); /* test keys for order and delta */ $y = 0; foreach ($s as $v) { if ($v != $y) { $isnum = false; break; } $y++; } } $si = $ri === false ? false : $ri . " "; $first = true; if ($isnum) { /* all array keys are integers 0‥n */ $rs = "["; if ($ri !== false) { $rs .= "\n"; } foreach ($s as $v) { if ($first) { $first = false; } else { if ($ri === false) { $rs .= ","; } else { $rs .= ",\n"; } } if ($si !== false) { $rs .= $si; } $rs .= minijson_encode($x[$v], $si); } if ($ri !== false) { $rs .= "\n" . $ri; } $rs .= "]"; return $rs; } $rs = "{"; if ($ri !== false) { $rs .= "\n"; } foreach ($k as $v) { if (!isset($x[$v])) { continue; } if ($first) { $first = false; } else { if ($ri === false) { $rs .= ","; } else { $rs .= ",\n"; } } if ($si !== false) { $rs .= $si; } $rs .= minijson_encode((string) $v, false); if ($ri === false) { $rs .= ":"; } else { $rs .= ": "; } $rs .= minijson_encode($x[$v], $si); } if ($ri !== false) { $rs .= "\n" . $ri; } $rs .= "}"; return $rs; } /* treat everything else as array or string */ if (!is_scalar($x)) { return minijson_encode((array) $x, $ri); } return minijson_encode((string) $x, $ri); }