<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Mike Griffiths &#187; json_encode</title>
	<atom:link href="http://www.mike-griffiths.co.uk/tag/json_encode/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mike-griffiths.co.uk</link>
	<description>Blog</description>
	<lastBuildDate>Tue, 13 Dec 2011 13:01:09 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP json_encode Alternative</title>
		<link>http://www.mike-griffiths.co.uk/php-json_encode-alternative/</link>
		<comments>http://www.mike-griffiths.co.uk/php-json_encode-alternative/#comments</comments>
		<pubDate>Mon, 25 Jan 2010 00:19:30 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[json_encode]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.mike-griffiths.co.uk/?p=144</guid>
		<description><![CDATA[The amount of times I&#8217;ve been working on a project and the environment hasn&#8217;t had the useful json_encode function installed due to PHP being < version 5.2 is quite worrying.  So, I decided to write my own replacement.  How very decent of me.  You can use it as you will.  I [...]]]></description>
			<content:encoded><![CDATA[<p>The amount of times I&#8217;ve been working on a project and the environment hasn&#8217;t had the useful <em>json_encode</em> function installed due to PHP being < version 5.2 is quite worrying.  So, I decided to write my own replacement.  How very decent of me.  You can use it as you will.  I wrote this a long time ago, but I've noticed it's somehow wormed its way onto the php.net site under someone elses name, and someone else seems to be taking credit for it.  Oh well...</p>
<pre lang="php">&lt;?php
if (!function_exists(&#8217;json_encode&#8217;))
{
	function json_encode($a=false)
	{
		// Some basic debugging to ensure we have something returned
		if (is_null($a)) return &#8216;null&#8217;;
		if ($a === false) return &#8216;false&#8217;;
		if ($a === true) return &#8216;true&#8217;;
		if (is_scalar($a))
		{
			if (is_float($a))
			{
				// Always use &#8220;.&#8221; for floats.
				return floatval(str_replace(&#8221;,&#8221;, &#8220;.&#8221;, strval($a)));
			}

			if (is_string($a))
			{
				static $jsonReplaces = array(array(&#8221;\\&#8221;, &#8220;/&#8221;, &#8220;\n&#8221;, &#8220;\t&#8221;, &#8220;\r&#8221;, &#8220;\b&#8221;, &#8220;\f&#8221;, &#8216;&#8221;&#8216;), array(&#8217;\\\\&#8217;, &#8216;\\/&#8217;, &#8216;\\n&#8217;, &#8216;\\t&#8217;, &#8216;\\r&#8217;, &#8216;\\b&#8217;, &#8216;\\f&#8217;, &#8216;\&#8221;&#8216;));
				return &#8216;&#8221;&#8216; . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . &#8216;&#8221;&#8216;;
			}
			else
				return $a;
		}
		$isList = true;
		for ($i = 0, reset($a); $i {
			if (key($a) !== $i)
			{
				$isList = false;
				break;
			}
		}
		$result = array();
		if ($isList)
		{
			foreach ($a as $v) $result[] = json_encode($v);
			return &#8216;[' . join(',', $result) . ']&#8216;;
		}
		else
		{
			foreach ($a as $k => $v) $result[] = json_encode($k).&#8217;:&#8217;.json_encode($v);
			return &#8216;{&#8217; . join(&#8217;,', $result) . &#8216;}&#8217;;
		}
	}
}
?&gt; </pre>
]]></content:encoded>
			<wfw:commentRss>http://www.mike-griffiths.co.uk/php-json_encode-alternative/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

