note 92377 deleted from function.json-encode by danbrown
| From: | danbrown@php.net | Date: | Wed, 22 Jul 2009 16:55:09 +0000 |
| Subject: | note 92377 deleted from function.json-encode by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-158579@lists.php.net to get a copy of this message | ||
Note Submitter: email at robertdewilde dot nl
----
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';
}
}
?>