note 87557 deleted from function.json-encode by danbrown

From: Date: Wed, 10 Dec 2008 16:20:34 +0000
Subject: note 87557 deleted from function.json-encode by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-147994@lists.php.net to get a copy of this message
Note Submitter: prgss at bk dot ru ---- You don't need json_encode - you need this: /** * Author: Nirvanko. Visit irc.freenode.org #javascript and thank him. * See RFC 4627 for details. * */ class json { public static $output; public static $pretty_print; public static $tab = " "; public static $n = "\n"; public static $space = " "; private function format($mixed, $depth) { if (is_string($mixed)) { /* sring = quotation-mark *char quotation-mark char = unescaped / escape ( %x22 / ; " quotation mark U+0022 %x5C / ; \ reverse solidus U+005C %x2F / ; / solidus U+002F %x62 / ; b backspace U+0008 %x66 / ; f form feed U+000C %x6E / ; n line feed U+000A %x72 / ; r carriage return U+000D %x74 / ; t tab U+0009 %x75 4HEXDIG ) ; uXXXX U+XXXX escape = %x5C ; \ quotation-mark = %x22 ; " unescaped = %x20-21 / %x23-5B / %x5D-10FFFF */ $escape = array("/\\\\/", "/\//", "/\x08/", "/\x09/", "/\x0a/", "/\x0c/", "/\x0d/", "/\x22/"); $replacement = array("\\\\\\", "\\/", "\\\x62", "\\\x74", "\\\x6e", "\\\x66", "\\\x72", "\\\x22"); $mixed = preg_replace($escape, $replacement, $mixed); $out = '"' . $mixed . '"'; return $out; } if (is_object($mixed)) { $out = ""; $n = json::$n; $t = json::$tab; $s = json::$space; $pp = json::$pretty_print; if ($pp) $out .= $n.str_repeat($t, $depth); $out .= "{"; foreach ($mixed as $name => $value) { if ($pp) $out .= $n.str_repeat($t, $depth).$s.$s; $out .= '"' . $name . '"'; $out .= ':'; $out .= $this->format($value, $depth + 1); $out .= ','; } $out = rtrim($out, ","); if ($pp) $out .= $n.str_repeat($t, $depth); $out .= "}"; return $out; } if (is_array($mixed)) { $out = ""; $n = json::$n; $t = json::$tab; $s = json::$space; $pp = json::$pretty_print; if ($pp) $out .= $n.str_repeat($t, $depth); $out .= "["; $values = array_values($mixed); for ($i = 0; $i < count($values); $i++) { if ($pp) $out .= $n.str_repeat($t, $depth).$s.$s; $out .= $this->format($values[$i], $depth + 1); $out .= ($i < count($values) - 1) ? "," : ""; } if ($pp) $out .= $n.str_repeat($t, $depth); $out .= "]"; return $out; } if (is_bool($mixed)) return $mixed ? "true" : "false"; if (is_numeric($mixed)) return $mixed; if (is_null($mixed)) return "null"; } public function get($pretty_print = false) { json::$pretty_print = $pretty_print; $out = &json::$output; $n = json::$n; $t = json::$tab; $s = json::$space; $pp = json::$pretty_print; $depth = 0; $out = "{"; foreach ($this as $name => $value) { if ($pp) $out .= $n.$s.$s; $out .= '"' . $name . '"'; $out .= ':'; $out .= $this->format($value, $depth + 1); $out .= ','; } $out = rtrim(json::$output, ","); if ($pp) $out .= $n; $out .= "}"; return $out; } } $json = new json; $json->foo = "Hello, World! (c) \x09"; $json->test = array(1,"bar","qaz"); $json->boolean1 = true; $json->boolean2 = false; $json->empty = null; $json->num1 = 123; $json->num2 = 0.123e-234; $json->obj = new stdClass; $json->obj->str1 = "Hello, World"; $json->obj->num1 = 1; $nestedObj = new stdClass; $nestedObj->str1 = "nested"; $json->obj->arr1 = array("123", 1, $nestedObj); print $json->get(true);

« previous php.notes (#147994) next »