<?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>ria-coder.com &#187; JSON</title>
	<atom:link href="http://ria-coder.com/blog/tag/json/feed" rel="self" type="application/rss+xml" />
	<link>http://ria-coder.com/blog</link>
	<description></description>
	<lastBuildDate>Wed, 21 Apr 2010 19:35:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>JSON Utility 0.1 Released!</title>
		<link>http://ria-coder.com/blog/json-utility-01-released</link>
		<comments>http://ria-coder.com/blog/json-utility-01-released#comments</comments>
		<pubDate>Sun, 31 May 2009 00:15:53 +0000</pubDate>
		<dc:creator>Danny Kopping</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[as3corelib]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[utilities]]></category>

		<guid isPermaLink="false">http://ria-coder.com/blog/?p=300</guid>
		<description><![CDATA[I have just created an application (still a very early version!) that is aimed at making development with the JSON data-interchange format a whole lot easier]]></description>
			<content:encoded><![CDATA[<p>I have just created an application (still a very early version!) that is aimed at making development with the JSON data-interchange format a whole lot easier. This application basically accepts a JSON string, validates it (using the JSONDecoder class from <a title="AS3Corelib" href="http://code.google.com/p/as3corelib/" target="_blank">as3corelib</a>) and generates an ActionScript 3.0 class from it. You can download it <a title="JSON Utility.air" href="http://ria-coder.com/blog/wp-content/uploads/2009/05/jsonutil.air" target="_blank">here</a>!</p>
<p style="text-align: left;">The opening screen:</p>
<p style="text-align: left;"><a title="JSON Input" rel="lightbox" href="http://ria-coder.com/temp/wp-content/uploads/2009/05/json-view.png"><img class="size-medium wp-image-302 aligncenter" title="json-view" src="http://ria-coder.com/blog/wp-content/uploads/2009/05/json-view-300x233.png" alt="json-view" width="300" height="233" /></a></p>
<p style="text-align: left;">&#8230;and the output!</p>
<p style="text-align: left;"><a title="AS3 Output" rel="lightbox" href="http://ria-coder.com/temp/wp-content/uploads/2009/05/class-view.png"><img class="aligncenter size-medium wp-image-303" title="class-view" src="http://ria-coder.com/blog/wp-content/uploads/2009/05/class-view-300x233.png" alt="class-view" width="300" height="233" /></a></p>
<p>I&#8217;ve got quite a few more features planned (hence the version &#8220;0.1&#8243;). If you have any comments, queries or suggestions, leave them in a comment below.</p>
<p>Thanks to Dan White for the use of his skin <em><a title="Granite Skin on Scalenine" href="http://scalenine.com/themes/granite/Granite.html" target="_blank">Granite</a>!</em></p>
]]></content:encoded>
			<wfw:commentRss>http://ria-coder.com/blog/json-utility-01-released/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using MySQL and JSON</title>
		<link>http://ria-coder.com/blog/using-mysql-and-json</link>
		<comments>http://ria-coder.com/blog/using-mysql-and-json#comments</comments>
		<pubDate>Sat, 25 Apr 2009 14:09:03 +0000</pubDate>
		<dc:creator>Danny Kopping</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[as3corelib]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://ria-coder.com/blog/?p=258</guid>
		<description><![CDATA[JSON can be extremely useful when you want to pass data from any platform to any other platform as the format is - essentially - just plain ol' text and computers can parse JSON objects very easily.]]></description>
			<content:encoded><![CDATA[<p><a title="JSON" href="http://json.org/" target="_blank"><strong>JSON</strong></a><strong> </strong>has &#8211; over the past few months &#8211; start working its way more and more into my daily life as a web developer. I find myself using it for all sorts of solutions, whether i&#8217;m using ActionScript or PHP. For those of you who don&#8217;t know, JSON stands for <em>JavaScript Object Notation</em>. JSON is an extremely lightweight, human-readable and highly compacted <strong>data-interchange</strong> format. JSON works on the principle of <em>name/value</em> pairing, and it&#8217;s very easy to read, write and parse.</p>
<p>A typical JSON representation of the following PHP object would be:</p>
<pre>{"title":"Using MySQL and JSON","date":"25 April 2009","timeOfWriting":"15h34"}</pre>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
      	<span style="color: #000088;">$blogPost</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> stdClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$blogPost</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Using MySQL and JSON&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$blogPost</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">date</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;25 April 2009&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$blogPost</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">timeOfWriting</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;15h34&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>and the following ActionScript object would also be represented as it was above:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> blogPost<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Object</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
blogPost<span style="color: #000066; font-weight: bold;">.</span>title = <span style="color: #990000;">&quot;Using MySQL and JSON&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
blogPost<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">date</span> = <span style="color: #990000;">&quot;25 April 2009&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
blogPost<span style="color: #000066; font-weight: bold;">.</span>timeOfWriting = <span style="color: #990000;">&quot;15h34&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">// -----or-----</span>
<span style="color: #009900; font-style: italic;">//var blogPost:Object = {title:&quot;Using MySQL and JSON&quot;, date:&quot;25 April 2009&quot;, timeOfWriting:&quot;15h34&quot;};</span></pre></td></tr></table></div>

<p>JSON can be extremely useful when you want to pass data from any platform to any other platform as the format is &#8211; essentially &#8211; just plain ol&#8217; text and computers can parse JSON objects very easily.</p>
<p>In PHP5, this is supported natively, using the <strong>json_encode</strong> and <strong>json_decode</strong> functions.<br />
However, in ActionScript 3.0, the parsing of JSON strings is not supported in the native ActionScript 3.0 API, so you can either write one yourself or use the <strong>JSONEncoder </strong>or <strong>JSONDecoder</strong> classes found in the <strong><a title="AS3CoreLib" href="http://code.google.com/p/as3corelib/" target="_blank">as3corelib</a></strong><a title="AS3CoreLib" href="http://code.google.com/p/as3corelib/" target="_blank"></a> package.</p>
<p>Here&#8217;s how to encode/decode a JSON string in PHP:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #000088;">$blogPost</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> stdClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$blogPost</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Using MySQL and JSON&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$blogPost</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">date</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;25 April 2009&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$blogPost</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">timeOfWriting</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;15h34&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// -----or-----</span>
	<span style="color: #666666; font-style: italic;">// $blogPost = array(&quot;title&quot; =&gt; &quot;Using MySQL and JSON&quot;,</span>
	<span style="color: #666666; font-style: italic;">//					&quot;date&quot; =&gt; &quot;25 April 2009&quot;,</span>
	<span style="color: #666666; font-style: italic;">//					&quot;timeOfWriting&quot; =&gt; &quot;15h34&quot;);</span>
&nbsp;
	<span style="color: #000088;">$json</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$blogPost</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Encoded JSON: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$json</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;br/&gt;Decoded JSON (as associative array):&lt;br/&gt;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">json_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$json</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;br/&gt;Decoded JSON (as stdClass object):&lt;br/&gt;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">json_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$json</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>&#8230;and in ActionScript 3.0 using the <strong>as3corelib</strong> package:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">import</span> mx<span style="color: #000066; font-weight: bold;">.</span>utils<span style="color: #000066; font-weight: bold;">.</span>ObjectUtil<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #0033ff; font-weight: bold;">import</span> com<span style="color: #000066; font-weight: bold;">.</span>adobe<span style="color: #000066; font-weight: bold;">.</span>serialization<span style="color: #000066; font-weight: bold;">.</span>json<span style="color: #000066; font-weight: bold;">.</span>JSONDecoder<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #0033ff; font-weight: bold;">import</span> com<span style="color: #000066; font-weight: bold;">.</span>adobe<span style="color: #000066; font-weight: bold;">.</span>serialization<span style="color: #000066; font-weight: bold;">.</span>json<span style="color: #000066; font-weight: bold;">.</span>JSONEncoder<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> blogPost<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Object</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
blogPost<span style="color: #000066; font-weight: bold;">.</span>title = <span style="color: #990000;">&quot;Using MySQL and JSON&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
blogPost<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">date</span> = <span style="color: #990000;">&quot;25 April 2009&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
blogPost<span style="color: #000066; font-weight: bold;">.</span>timeOfWriting = <span style="color: #990000;">&quot;15h34&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">// -----or-----</span>
<span style="color: #009900; font-style: italic;">//var blogPost:Object = {title:&quot;Using MySQL and JSON&quot;, date:&quot;25 April 2009&quot;, timeOfWriting:&quot;15h34&quot;};</span>
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> json<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #0033ff; font-weight: bold;">new</span> JSONEncoder<span style="color: #000000;">&#40;</span>blogPost<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">.</span>getString<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Encoded JSON: &quot;</span> <span style="color: #000066; font-weight: bold;">+</span> json<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Decoded JSON: &quot;</span> <span style="color: #000066; font-weight: bold;">+</span> ObjectUtil<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> JSONDecoder<span style="color: #000000;">&#40;</span>json<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">.</span>getValue<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></td></tr></table></div>

<p>Now that we&#8217;ve gotten that out of the way, let&#8217;s have a look at how we can use JSON inside of MySQL. First off, let me say that i&#8217;m a big fan of structuring one&#8217;s databases correctly and not taking hideous shortcuts like i&#8217;m about to show you, but sometimes doing this hideous shortcut might actually help you develop your appplications faster, and keep things extensible.</p>
<p>I&#8217;ve found that i often have to keep changing my database structure when clients have changes of heart (and man, do they change them often!). It becomes extremely frustrating to have to restructure an entire database (and all associated PHP/AS3 code) every time a client remembers that he forgot about needing to store something as ridiculous the big-toe girdths of his future website&#8217;s users.</p>
<p>Now, i&#8217;m certainly not a patient person, and &#8211; to be honest &#8211; i don&#8217;t have time to fuck about and keep changes logic just to accomodate menial extra pieces of info. What i do therefore is usually keep a <em>settings</em> column in the <em>users</em> table, set it&#8217;s type to <strong>Text</strong> and store JSON strings in there to keep track of all the silly idiosyncratic (and sometimes idiotic) requirements of the client. Then all i have to do is parse out that string and apply some business logic to the values i find in that row under the <em>settings</em> column.</p>
<p>I find that it makes things a lot simpler and with orders of magnitude less ballaches.</p>
<p>What do you think?</p>
]]></content:encoded>
			<wfw:commentRss>http://ria-coder.com/blog/using-mysql-and-json/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

