note 92377 added to function.json-encode

From: Date: Wed, 22 Jul 2009 08:24:01 +0000
Subject: note 92377 added to function.json-encode
Groups: php.notes 
Request: Send a blank email to php-notes+get-158567@lists.php.net to get a copy of this message
Encode to JSON and XML (or implement any other). Should be TESTED. Also, insecurity (adding addslashes, escaping characters). Function now calls __get_serialize for custom handling, could be replaced with __wake_up. Suggestions always welcome. Use it freely. <?php class Serialize { public static $OPT_FMT_XML = 1; public static $OPT_FMT_JSON = 2; public static $OPT_FMT_PHP = 4; private static $PARSE_PART_CONTENT = 1; private static $PARSE_PART_OPENING = 2; private static $PARSE_PART_CLOSING = 3; private static $PARSE_PART_DELIMITER = 4; private static $PARSE_PART_ARRAY_OPENING = 5; private static $PARSE_PART_ARRAY_CLOSING = 6; private static $PARSE_PART_ITEM_OPENING = 7; private static $PARSE_PART_ITEM_CLOSING = 8; public static $buffer = ''; public static $fmt = 1; public static function format($mVal, $iFmt = 1) { // Reset all values self::$buffer = ''; self::$fmt = $iFmt; // Prepend it with something? if (is_object($mVal)) self::$buffer .= self::output(get_class($mVal), self::$PARSE_PART_OPENING); else self::$buffer .= self::output('root', self::$PARSE_PART_OPENING); // Start parsing from root self::parse($mVal); // Append something to it? if (is_object($mVal)) self::$buffer .= self::output(get_class($mVal), self::$PARSE_PART_CLOSING); else self::$buffer .= self::output('root', self::$PARSE_PART_CLOSING); return self::$buffer; } public static function parse($mVal) { if (is_object($mVal) && method_exists($mVal, '__get_serialize')) self::parse($mVal->__get_serialize()); elseif ( is_object($mVal) || is_array($mVal) ) { self::$buffer .= self::output($mVal, self::$PARSE_PART_ARRAY_OPENING); self::walk($mVal); self::$buffer .= self::output($mVal, self::$PARSE_PART_ARRAY_CLOSING); } else { self::$buffer .= self::output($mVal, self::$PARSE_PART_CONTENT); } return self::$buffer; } private static function output($mVal, $iPart = 1) { switch (self::$fmt) { case self::$OPT_FMT_XML: return self::toXML($mVal, $iPart); break; case self::$OPT_FMT_JSON: return self::toJSON($mVal, $iPart); break; case self::$OPT_FMT_PHP: return self::toPHP($mVal, $iPart); break; default: throw new Exception("Desired format is not supported by Serialize-class."); } return false; } private static function walk($mVal) { foreach ($mVal as $sItemIndex => $mItemValue) { self::$buffer .= self::output($sItemIndex, self::$PARSE_PART_OPENING); self::parse($mItemValue); self::$buffer .= self::output($sItemIndex, self::$PARSE_PART_CLOSING); self::$buffer .= self::output($sItemIndex, self::$PARSE_PART_DELIMITER); } // Remove last delimiter self::$buffer = substr(self::$buffer, 0, strlen(self::$buffer)-strlen(self::output($sItemIndex, self::$PARSE_PART_DELIMITER)) ); } private static function addQuotes($mValue) { if (is_numeric($mValue)) return $mValue; else return '"'.$mValue.'"'; } private static function toJSON($mVal, $iPart = 1) { switch ($iPart) { case self::$PARSE_PART_CONTENT: return self::addQuotes($mVal); break; case self::$PARSE_PART_OPENING: if ($mVal == 'root') return ''; return self::addQuotes($mVal).':'; break; case self::$PARSE_PART_DELIMITER: return ","; break; case self::$PARSE_PART_ARRAY_OPENING: return '{'; break; case self::$PARSE_PART_ARRAY_CLOSING: return '}'; break; default: return ''; } return false; } private static function toPHP($mVal, $iPart = 1) { return 'PHP dump format not implemented yet'; } } ?> ---- Server IP: 83.137.20.135 Probable Submitter: 85.150.120.108 ---- Manual Page -- http://www.php.net/manual/en/function.json-encode.php Edit -- https://master.php.net/note/edit/92377 Del: integrated -- https://master.php.net/note/delete/92377/integrated Del: useless -- https://master.php.net/note/delete/92377/useless Del: bad code -- https://master.php.net/note/delete/92377/bad+code Del: spam -- https://master.php.net/note/delete/92377/spam Del: non-english -- https://master.php.net/note/delete/92377/non-english Del: in docs -- https://master.php.net/note/delete/92377/in+docs Del: other reasons-- https://master.php.net/note/delete/92377 Reject -- https://master.php.net/note/reject/92377 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#158567) next »