<?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>clay loveless &#187; json</title>
	<atom:link href="http://claylo.com/tag/json/feed" rel="self" type="application/rss+xml" />
	<link>http://claylo.com</link>
	<description></description>
	<lastBuildDate>Tue, 27 Oct 2009 08:17:46 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP and JSON: Cut #987</title>
		<link>http://claylo.com/php-and-json-cut-987</link>
		<comments>http://claylo.com/php-and-json-cut-987#comments</comments>
		<pubDate>Wed, 28 Feb 2007 16:47:32 +0000</pubDate>
		<dc:creator>@claylo</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[standards]]></category>

		<guid isPermaLink="false">http://claylo.com/2007/02/28/php-and-json-cut-987/</guid>
		<description><![CDATA[JSON Decoding in PHP 5.2.1 is Broken
As of PHP 5.2.1, json_decode() no longer follows the published standards for JSON-encoded texts.
Why not? For no reason other than the convenience of those ignorant of JSON standards.
Prior to PHP 5.2.1, this:
var_dump(json_decode('true'));


resulted in:
NULL
As of PHP 5.2.1, it results in:
bool(true)
Nice and handy, perhaps &#8230; but a blatant violation of JSON [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin-right: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fclaylo.com%2Fphp-and-json-cut-987"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fclaylo.com%2Fphp-and-json-cut-987" height="61" width="51" /></a></div><h2 id="json_decoding_in_php_521_is_broken">JSON Decoding in PHP 5.2.1 is Broken</h2>
<p>As of PHP 5.2.1, <code>json_decode()</code> no longer follows <a href="http://www.ietf.org/rfc/rfc4627.txt">the published standards</a> for JSON-encoded texts.</p>
<p>Why not? For no reason other than the convenience of those ignorant of JSON standards.</p>
<p>Prior to PHP 5.2.1, this:</p>
<p><code><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #DD0000">'true'</span><span style="color: #007700">));</span><br />
</span><br />
</code></p>
<p>resulted in:</p>
<p><code>NULL</code></p>
<p>As of PHP 5.2.1, it results in:</p>
<p><code>bool(true)</code></p>
<p>Nice and handy, perhaps &#8230; but a blatant violation of JSON specifications, since <code>'true'</code> <strong>is not</strong> a valid JSON encoded text.</p>
<h2 id="a_little_history">A little history</h2>
<p>Back in August, I spent a lot of time with <a href="http://www.json.org/">JSON</a>. I was working on adding <a href="http://www.prototypejs.com/">Prototype</a>/<a href="http://script.aculo.us/">script.aculo.us</a> support to the <a href="http://solarphp.com/">Solar Framework</a>, and wanted a handy utility for passing options around in JavaScript with JSON.</p>
<p>Rather than roll some new JSON interpreter, I chose to leverage the <a href="http://mike.teczno.com/JSON/JSON.phps">Services_JSON</a> package along with <a href="http://pecl.php.net/package/json">ext/json</a> to build a package that was compatible with ext/json, but usable for those who did not have that extension installed. With compatibility built in, developers could move application code back and forth between systems without having to worry about whether or not the extension was installed &#8212; if it was, the application would benefit from the added performance of a native extension. If it wasn&#8217;t, everything should work exactly the same way.</p>
<p>In the course of my research for the Solar_Json package, I learned a lot about JSON and how it is supposed to behave. <a href="http://www.json.org/">JSON.org</a> is a spartan but complete resource about the format, and includes the <a href="http://www.json.org/JSON_checker/">JSON Checker</a> and a comprehensive JSON test suite. There&#8217;s also a link to <a href="http://www.ietf.org/rfc/rfc4627.txt">RFC 4627</a>, which details JSON&#8217;s structure in a proposal for the formal application/json media type.</p>
<p>While digging through all this JSON goodness, I came to appreciate ext/json&#8217;s strict adherence to JSON&#8217;s format. The version of ext/json bundled with PHP 5.2.0 (version 1.2.1) was right on the money in its parsing, and by the time I was done, Solar_Json matched it every step of the way. To ensure ext/json compatibility, I wrote <a href="http://solarphp.com/svn/trunk/tests/Solar/JsonTest.php">a series of unit tests</a> (26 in all) to ensure that Solar_Json&#8217;s &#8220;pure PHP&#8221; implementation matched ext/json&#8217;s output <em>exactly</em>.</p>
<p>It was challenging, but it worked out well. The result was <a href="http://solarphp.com/svn/trunk/Solar/Json.php">Solar_Json</a>.</p>
<h2 id="json_bundled_with_php_confusion_ensues">JSON Bundled with PHP, Confusion Ensues</h2>
<p>To my delight, ext/json was bundled with PHP 5.2.0, and enabled by default. This was great news for PHP developers everywhere who are working with rich applications that need to exchange a lot of data with JavaScript.</p>
<p>All was good, for awhile.</p>
<p><i>(Yesterday, <a href="http://www.paul-m-jones.com/">Paul M. Jones</a> re-ran the JSON unit tests I'd written for Solar_Json using PHP 5.2.1 in preparation for a new release of Solar. He mentioned that some of the tests started failing, which sparked this discussion. Thanks, Paul!)</i></p>
<p>Sure, a couple people (myself included) didn&#8217;t fully understand JSON for awhile. I even <a href="http://bugs.php.net/bug.php?id=38440">opened (and quickly closed)</a> a bug in the way I <em>thought</em> certain strings should be decoded by the <code>json_decode()</code> function. <a href="http://bugs.php.net/bug.php?id=38680">Others had the same confusion</a>.</p>
<p>What&#8217;s so confusing?</p>
<p>Well, the common thing that people want to do is something like this:</p>
<p><code><span style="color: #000000"><br />
<span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">));<br /></span><span style="color: #FF8000">//&nbsp;or<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #DD0000">'true'</span><span style="color: #007700">));</span><br />
</span><br />
</code></p>
<p>The confusing part about these two snippets is that they both return <code>NULL</code> instead of <code>true</code>. Based on the two bug reports (<a href="http://bugs.php.net/bug.php?id=38440">#38440</a> and <a href="http://bugs.php.net/bug.php?id=38680">#38680</a>), it&#8217;s common for people to expect the output to be a boolean <code>true</code> in these examples.</p>
<p>However, if you understand JSON at all, you&#8217;ll know that <code>NULL</code> is a perfectly reasonable result, because <code>true</code> and <code>'true'</code> <em>are not valid JSON texts</em>.</p>
<h2 id="standard_shmandard">Standard, Shmandard</h2>
<p>Note that I said &#8220;if you understand JSON at all&#8221;, you&#8217;ll realize that <code>NULL</code> is a perfectly reasonable result when attempting to decode an invalid JSON text.</p>
<p>I should amend that: If you understand JSON at all <strong><em>and actually care about standards and compatibility</em></strong>, you&#8217;ll realize that <code>NULL</code> is a perfectly reasonable result of parsing an invalid JSON text.</p>
<p>Just like other formats, there&#8217;s actually <a href="http://www.ietf.org/rfc/rfc4627.txt">a specification</a> that defines what is valid and what is not when it comes to JSON. <a href="http://www.ietf.org/rfc/rfc4627.txt">No, really</a>.</p>
<p>Section 2 of the standard states very plainly:</p>
<blockquote>
<p>A JSON text is a serialized object or array.</p>
<p><code>JSON-text = object / array</code></p>
</blockquote>
<p>Translated, that means that a valid JSON-text is either an object or an array. It&#8217;s <em>not</em> a string literal, an integer, a boolean. The list of what a valid JSON-text can be is short. It can be an object. It can be an array. It can be &#8230; whoops, that&#8217;s it. An object, an array, or it just isn&#8217;t JSON.</p>
<p>I mean, think about it: <strong>J</strong>ava<strong>S</strong>cript <strong>O</strong>bject <strong>N</strong>otation. Not JavaScript Boolean Notation. Not JavaScript Assorted Stuff Notation. Objects. Arrays thrown in because in JavaScript, they&#8217;re basically the same thing. Period, the end.</p>
<p><em>DAMN, that&#8217;s inconvenient,</em> you may be thinking. Yep, it is. But, it is what it is. If you don&#8217;t like it, submit an RFC to have it changed. That&#8217;s the way this crazy thing called the internet works.</p>
<p>Put another way: if you don&#8217;t like it, you do not just <a href="http://tinyurl.com/2lxsba">start making things up</a>. Apparently, enough people unclear on the concept of JSON <a href="http://bugs.php.net/bug.php?id=38680">complained</a> about their lack of understanding that PHP now just does whatever it wants with JSON. <a href="http://tinyurl.com/2lxsba">Check this out</a> for the details. (And to reiterate, I&#8217;m not knocking the people who aren&#8217;t clear about JSON. <a href="http://bugs.php.net/bug.php?id=38440">I was one of them too</a>, up until I actually researched how JSON is <em>supposed</em> to behave.)</p>
<p>Imagine if the core team behind every language did that. Hey, if you don&#8217;t like the standards, just ignore them! We can explain it away with documentation, right?</p>
<h2 id="cut_987">Cut #987</h2>
<p>The <a href="http://word.sc/cavalier" title="given to haughty disregard of others">cavalier</a> attitude taken by the PHP internals team on this issue is inexcusable. Yep, <em>cavalier</em> &#8212; a colleague who spoke to a member of the PHP internals team about this change confirmed that the break from the JSON spec is deliberate and intentional.</p>
<p>To make matters worse, the version number of ext/json did not change between PHP 5.2.0 and PHP 5.2.1. In both releases, ext/json claims to be at version 1.2.1, despite this significant change.</p>
<p>While some are lobbying to compile the definitive <a href="http://blog.stuartherbert.com/php/2007/02/08/what-does-the-business-case-for-php-need-to-cover/">business case for PHP</a> (and I even <a href="http://tinyurl.com/2q7yoc">piped in and agreed</a> that it was necessary), some PHP internals folks are effectively shooting that effort in the foot by disregarding published standards.</p>
<p>I&#8217;ve spent the better part of the last two years defending my choice of PHP 5 as my preferred language, first at <a href="http://www.feedster.com/">Feedster</a>, now at <a href="http://www.mashery.com/">Mashery</a>. With all the buzz about other languages these days, the case for PHP is getting harder to make. Incidents like this will not make the case for PHP any easier.</p>
<p>Is this a big flap over a little thing? That&#8217;s certainly one way of looking at it. I see this flagrant disregard for published specs as one more cut toward <a href="http://en.wikipedia.org/wiki/Death_by_a_thousand_cuts">a death by a thousand cuts</a>.</p>
<p>Talented and notable developers are dropping PHP, or <a href="http://lukewelling.com/2007/02/07/maybe-it-is-time-i-got-out-of-php/">seriously considering</a> other languages. If PHP&#8217;s next 10 years are to be as poignant as its first, a significant attitude adjustment is required.</p>
]]></content:encoded>
			<wfw:commentRss>http://claylo.com/php-and-json-cut-987/feed</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
	</channel>
</rss>
