Re: note 92377 added to function.json-encode
| From: | Daniel Brown | Date: | Wed, 22 Jul 2009 16:47:25 +0000 |
| Subject: | Re: note 92377 added to function.json-encode | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-158581@lists.php.net to get a copy of this message | ||
Robert;
Under normal circumstances, large blocks of code - such as what
you've contributed below - would be removed from the online manual
notes, as that's more of a place for small snippets and hints to other
users. We try to discourage providing full solutions or large bits of
code.
Further, we do not generally allow folks to provide links to their
websites or other attempts at self-promotion.
However, I opine that the code you've provided would be a great
addition to the official examples, but too large to include in the
page (and beyond the scope of the documentation). So this would be
one of the rare cases where, if you have a website or blog where you
could host this, we would allow you to post a direct link.
Both of your note contributions are going to be deleted at this
time, but at your convenience, please reply-all to this message and
let us know the link where this code can be found online. Then post
it to the same area of the documentation as these contributions were
originally placed. I will attach a memo to your note to preserve it
and save it from deletion (so long as the link remains active and
hosts only your code, and no political or SPAM messages, of course).
Thanks for your generous contribution. It will continue to serve
the community and to make the official documentation better for
thousands of folks and years to come.
On Wed, Jul 22, 2009 at 04:24, <email@robertdewilde.nl> wrote:
> 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';
> }
> }
>
> ?>
--
</Daniel P. Brown>
daniel.brown@parasane.net || danbrown@php.net
http://www.parasane.net/ || http://www.pilotpig.net/
Check out our great hosting and dedicated server deals at
http://twitter.com/pilotpig