<?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>Maccherone &#187; Flex/Flash/ActionScript</title>
	<atom:link href="http://maccherone.com/larry/category/code/flexflashactionscript/feed/" rel="self" type="application/rss+xml" />
	<link>http://maccherone.com/larry</link>
	<description>Software engineering and craftsmanship; Adobe Flex/Flash/ActionScript/AIR; Measurement, Analysis, and Visualization</description>
	<lastBuildDate>Thu, 17 Jun 2010 17:51:13 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>A multiple file loader for Flex/Flash/ActionScript 3 (AS3)</title>
		<link>http://maccherone.com/larry/2009/05/31/a-multiple-file-loader-for-flexflashactionscript-3-as3/</link>
		<comments>http://maccherone.com/larry/2009/05/31/a-multiple-file-loader-for-flexflashactionscript-3-as3/#comments</comments>
		<pubDate>Mon, 01 Jun 2009 01:28:16 +0000</pubDate>
		<dc:creator>Larry Maccherone</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Flex/Flash/ActionScript]]></category>
		<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://maccherone.com/larry/?p=120</guid>
		<description><![CDATA[
		
		
		
		The URLMultiLoader class in this library will load multiple files and optionally &#8220;process&#8221; them before calling the method specified for Event.COMPLETE. Since file loading in the Flash/Flex/AS3 world is completely asynchronous, when you need to load more than one file, the hackish solution is to make the COMPLETE handler for the first one initiate the [...]]]></description>
			<content:encoded><![CDATA[<!--S-ButtonZ 1.1.5 Start--><div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;">
		<script type="text/javascript">
		<!--
		var dzone_url = "http://maccherone.com/larry/2009/05/31/a-multiple-file-loader-for-flexflashactionscript-3-as3/";
		var dzone_title = "A multiple file loader for Flex/Flash/ActionScript 3 (AS3)";
		var dzone_style = "1";
		var dzone_blurb = "The URLMultiLoader class in this library will load multiple files and optionally &#8220;process&#8221; them before calling the method specified for Event.COMPLETE. Since file loading in the Flash/Flex/AS3 world is completely asynchronous, when you need...";
		//-->
		</script>
		<script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script></div><!--S-ButtonZ 1.1.5 End--><p>The URLMultiLoader class in this library will load multiple files and optionally &#8220;process&#8221; them before calling the method specified for Event.COMPLETE. Since file loading in the Flash/Flex/AS3 world is completely asynchronous, when you need to load more than one file, the hackish solution is to make the COMPLETE handler for the first one initiate the load for the second, etc. until all the files are loaded. URLMultiLoader will allow you to setup one COMPLETE handler which will not be called until all the files you specify are loaded (and optionally &#8220;processed&#8221;).</p>
<p>When I first had need for this, I said to myself that someone must have done this before. It seems like a fairly common need. <span style="text-decoration: line-through;">However, when I went looking, I couldn&#8217;t find something that fit the bill, so I decided to write my own. </span>It was actually a very good way to get familiar with the event system. Also, while I was at it, I figured I&#8217;d allow the injection of a processor for each file and make sure that got processed before proceeding. <span style="text-decoration: line-through;">If anyone knows of another tool like this please post a link to it in the comments. Actually, it wouldn&#8217;t surprise me if this functionality is built into the Flex framework somewhere and I just missed it.</span></p>
<p>Update: The functionality mustn&#8217;t be in Flex because I have now found several other similar controls:</p>
<ul>
<li><a href="http://www.stimuli.com.br/trane/2007/nov/25/loading-reloaded/">BulkLoader</a></li>
<li><a href="http://code.google.com/p/queueloader-as3/">QueueLoader</a></li>
<li><a href="http://code.google.com/p/ultimateloader/">UltimateLoader</a></li>
</ul>
<p>Mine is relatively simple compared to some of these. BulkLoader seems particularly featureful. It has bandwidth stats and progress indicators. For my loading needs, the sizes were small enough that I wasn&#8217;t worried about progress or bandwidth, but I may update mine to include these features in the future.</p>
<p>One feature that mine has that many do not have is the optional ability to inject in a an IDataProcessor that will pre-process your data before returning it to you.</p>
<p>DataProcessorXMLStringToArray is provided as an example IDataProcessor that can optionally be passed in when adding a new URLRequest to the queue. If provided, an IDataProcessor will convert the raw file string (or binary, or url variables) into some other form before returning. Complete documentation for DataProcessorXMLStringToArray is provided in the ASDoc header for the class but it is offered here primarily as an example. You can easily create your own and inject them when setting up the URLMultiLoader. You just need to follow the IDataProcessor interface which has one method with the following signature:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">function processData(data:*):*</pre></div></div>

<p>Remember, the processor is totally optional. If omitted, URLMultiLoader will simply copy the file contents into it&#8217;s output data field. The type of the data in that case will depend upon the URLLoaderDataFormat: String for TEXT (default), ByteArray for BINARY, and URLVariables for VARIABLES.</p>
<p>Let&#8217;s see it in action.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> com.<span style="color: #006600;">maccherone</span>.<span style="color: #006600;">urlmultiloader</span>.<span style="color: #66cc66;">*</span>;
	<span style="color: #0066CC;">import</span> com.<span style="color: #006600;">maccherone</span>.<span style="color: #006600;">json</span>.<span style="color: #006600;">JSON</span>;  <span style="color: #808080; font-style: italic;">// Only used for pretty output</span>
&nbsp;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Bitmap</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Loader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">IOErrorEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLLoaderDataFormat</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> URLMultiLoaderTest <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> urlMultiLoader:URLMultiLoader = <span style="color: #000000; font-weight: bold;">new</span> URLMultiLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> baseURL:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;data/&quot;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> urlRequest1:URLRequest = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>baseURL + <span style="color: #ff0000;">&quot;file.xml&quot;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> urlRequest2:URLRequest = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>baseURL + <span style="color: #ff0000;">&quot;file.xml&quot;</span><span style="color: #66cc66;">&#41;</span>  <span style="color: #808080; font-style: italic;">// Same file but we'll get it in a different format</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> urlRequest3:URLRequest = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>baseURL + <span style="color: #ff0000;">&quot;smile.gif&quot;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> URLMultiLoaderTest<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> urlMultiLoader:URLMultiLoader = <span style="color: #000000; font-weight: bold;">new</span> URLMultiLoader
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> dataProcessor:IDataProcessor = <span style="color: #000000; font-weight: bold;">new</span> DataProcessorXMLStringToArray<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>  <span style="color: #808080; font-style: italic;">// Example provided with URLMultiLoader. You can create your own.</span>
&nbsp;
			urlMultiLoader.<span style="color: #006600;">addURLRequest</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Request1&quot;</span>, urlRequest1, dataProcessor<span style="color: #66cc66;">&#41;</span>
			urlMultiLoader.<span style="color: #006600;">addURLRequest</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Request2&quot;</span>, urlRequest2<span style="color: #66cc66;">&#41;</span>  <span style="color: #808080; font-style: italic;">// If no IDataProcessor is provided, then file's contents is returned as String, ByteArray, or</span>
			                                           <span style="color: #808080; font-style: italic;">// URLVariables depending upon the URLLoaderDataFormat TEXT, BINARY, or VARIABLES respectively</span>
			urlMultiLoader.<span style="color: #006600;">addURLRequest</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Request3&quot;</span>, urlRequest3, <span style="color: #000000; font-weight: bold;">null</span>, URLLoaderDataFormat.<span style="color: #006600;">BINARY</span><span style="color: #66cc66;">&#41;</span>  <span style="color: #808080; font-style: italic;">// Loads smile.gif as a ByteArray</span>
&nbsp;
			urlMultiLoader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, filesLoaded<span style="color: #66cc66;">&#41;</span>
			urlMultiLoader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>IOErrorEvent.<span style="color: #006600;">IO_ERROR</span>, onError<span style="color: #66cc66;">&#41;</span>
			urlMultiLoader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> filesLoaded<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">data</span>:<span style="color: #0066CC;">Object</span> = <span style="color: #66cc66;">&#40;</span>event.<span style="color: #0066CC;">target</span> as URLMultiLoader<span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">data</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Array of Objects:<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> + JSON.<span style="color: #006600;">encode</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;Request1&quot;</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">// Uses JSON.encode for pretty output</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;String of file contents:<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> + <span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;Request2&quot;</span><span style="color: #66cc66;">&#93;</span> + <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">var</span> loader:Loader = <span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			loader.<span style="color: #006600;">loadBytes</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;Request3&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>loader<span style="color: #66cc66;">&#41;</span>  <span style="color: #808080; font-style: italic;">// Displays smile.gif in Flash player</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> onError<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>event<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Assuming you put the file.xml and the smile.gif , in a data/ folder below the bin-debug directory and you have the correct security settings, the above code would result in the following output:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Array of Objects:
[
    {&quot;id&quot;: 101, &quot;name&quot;: &quot;/db/node/visitor&quot;},
    {&quot;id&quot;: 102, &quot;name&quot;: &quot;/db/node/observer&quot;},
    {&quot;id&quot;: 103, &quot;name&quot;: &quot;/ui/button&quot;}
]
&nbsp;
String of file contents:
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;root&gt;
  &lt;file&gt;
    &lt;id&gt;101&lt;/id&gt;
    &lt;name&gt;/db/node/visitor&lt;/name&gt;
  &lt;/file&gt;
  &lt;file&gt;
    &lt;id&gt;102&lt;/id&gt;
    &lt;name&gt;/db/node/observer&lt;/name&gt;
  &lt;/file&gt;
  &lt;file&gt;
    &lt;id&gt;103&lt;/id&gt;
    &lt;name&gt;/ui/button&lt;/name&gt;
  &lt;/file&gt;
&lt;/root&gt;</pre></div></div>

<p>Plus it will display smile.gif in the Flash player like this:</p>
<p><img class="alignnone size-full wp-image-127" title="smile_in_flash_player" src="http://maccherone.com/larry/wp-content/uploads/2009/05/smile_in_flash_player.png" alt="smile_in_flash_player" width="290" height="251" /></p>
<p>You can download it from <a href="http://maccherone.com/larry/projects/a-multiple-file-loader-for-flashflex-in-actionscript-3-as3/">here</a>.</p>
<p>Update: I altered the URLMultiLoader to use a string as the key to retrieving the data after the loading is complete. A previous version used the URLRequest as the key for Dictionary object. This version does not depend upon Dictionary.</p>
<div style="clear:both;">&nbsp;</div>


Please vote for this on DZone at the top of this page and share it on:


	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fmaccherone.com%2Flarry%2F2009%2F05%2F31%2Fa-multiple-file-loader-for-flexflashactionscript-3-as3%2F&amp;t=A%20multiple%20file%20loader%20for%20Flex%2FFlash%2FActionScript%203%20%28AS3%29" title="Facebook"><img src="http://maccherone.com/larry/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=A%20multiple%20file%20loader%20for%20Flex%2FFlash%2FActionScript%203%20%28AS3%29%20-%20http%3A%2F%2Fmaccherone.com%2Flarry%2F2009%2F05%2F31%2Fa-multiple-file-loader-for-flexflashactionscript-3-as3%2F" title="Twitter"><img src="http://maccherone.com/larry/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fmaccherone.com%2Flarry%2F2009%2F05%2F31%2Fa-multiple-file-loader-for-flexflashactionscript-3-as3%2F&amp;title=A%20multiple%20file%20loader%20for%20Flex%2FFlash%2FActionScript%203%20%28AS3%29&amp;bodytext=The%20URLMultiLoader%20class%20in%20this%20library%20will%20load%20multiple%20files%20and%20optionally%20%22process%22%20them%20before%20calling%20the%20method%20specified%20for%20Event.COMPLETE.%20Since%20file%20loading%20in%20the%20Flash%2FFlex%2FAS3%20world%20is%20completely%20asynchronous%2C%20when%20you%20need%20to%20load%20m" title="Digg"><img src="http://maccherone.com/larry/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fmaccherone.com%2Flarry%2F2009%2F05%2F31%2Fa-multiple-file-loader-for-flexflashactionscript-3-as3%2F&amp;title=A%20multiple%20file%20loader%20for%20Flex%2FFlash%2FActionScript%203%20%28AS3%29" title="StumbleUpon"><img src="http://maccherone.com/larry/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fmaccherone.com%2Flarry%2F2009%2F05%2F31%2Fa-multiple-file-loader-for-flexflashactionscript-3-as3%2F&amp;title=A%20multiple%20file%20loader%20for%20Flex%2FFlash%2FActionScript%203%20%28AS3%29&amp;notes=The%20URLMultiLoader%20class%20in%20this%20library%20will%20load%20multiple%20files%20and%20optionally%20%22process%22%20them%20before%20calling%20the%20method%20specified%20for%20Event.COMPLETE.%20Since%20file%20loading%20in%20the%20Flash%2FFlex%2FAS3%20world%20is%20completely%20asynchronous%2C%20when%20you%20need%20to%20load%20m" title="del.icio.us"><img src="http://maccherone.com/larry/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fmaccherone.com%2Flarry%2F2009%2F05%2F31%2Fa-multiple-file-loader-for-flexflashactionscript-3-as3%2F&amp;title=A%20multiple%20file%20loader%20for%20Flex%2FFlash%2FActionScript%203%20%28AS3%29&amp;annotation=The%20URLMultiLoader%20class%20in%20this%20library%20will%20load%20multiple%20files%20and%20optionally%20%22process%22%20them%20before%20calling%20the%20method%20specified%20for%20Event.COMPLETE.%20Since%20file%20loading%20in%20the%20Flash%2FFlex%2FAS3%20world%20is%20completely%20asynchronous%2C%20when%20you%20need%20to%20load%20m" title="Google Bookmarks"><img src="http://maccherone.com/larry/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.dzone.com/links/add.html?url=http%3A%2F%2Fmaccherone.com%2Flarry%2F2009%2F05%2F31%2Fa-multiple-file-loader-for-flexflashactionscript-3-as3%2F&amp;title=A%20multiple%20file%20loader%20for%20Flex%2FFlash%2FActionScript%203%20%28AS3%29" title="DZone"><img src="http://maccherone.com/larry/wp-content/plugins/sociable/images/dzone.png" title="DZone" alt="DZone" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://maccherone.com/larry/?page_id=0?</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ActionScript 3 (AS3) JSON encoder with &#8220;pretty&#8221; output by adding linefeeds and spaces</title>
		<link>http://maccherone.com/larry/2009/05/28/actionscript-3-as3-json-encoder-with-pretty-output-by-adding-linefeeds-and-spaces/</link>
		<comments>http://maccherone.com/larry/2009/05/28/actionscript-3-as3-json-encoder-with-pretty-output-by-adding-linefeeds-and-spaces/#comments</comments>
		<pubDate>Fri, 29 May 2009 02:50:51 +0000</pubDate>
		<dc:creator>Larry Maccherone</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Flex/Flash/ActionScript]]></category>
		<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[JSON]]></category>

		<guid isPermaLink="false">http://maccherone.com/larry/?p=65</guid>
		<description><![CDATA[
		
		
		
		I&#8217;m sure many ActionScript 3 or Flex developers have used the as3corelib for one reason or another. It&#8217;s a wonderful little library with lots of useful functionality. I&#8217;ve frequently used its JSON  encoding and decoding functionality. It works great, however, it doesn&#8217;t add spaces or linefeeds to make the resulting JSON string more readable. That&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<!--S-ButtonZ 1.1.5 Start--><div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;">
		<script type="text/javascript">
		<!--
		var dzone_url = "http://maccherone.com/larry/2009/05/28/actionscript-3-as3-json-encoder-with-pretty-output-by-adding-linefeeds-and-spaces/";
		var dzone_title = "ActionScript 3 (AS3) JSON encoder with &#8220;pretty&#8221; output by adding linefeeds and spaces";
		var dzone_style = "1";
		var dzone_blurb = "I&#8217;m sure many ActionScript 3 or Flex developers have used the as3corelib for one reason or another. It&#8217;s a wonderful little library with lots of useful functionality. I&#8217;ve frequently used its JSON  encoding and decoding functionality....";
		//-->
		</script>
		<script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script></div><!--S-ButtonZ 1.1.5 End--><p>I&#8217;m sure many ActionScript 3 or Flex developers have used the <a href="http://code.google.com/p/as3corelib/" target="_blank">as3corelib</a> for one reason or another. It&#8217;s a wonderful little library with lots of useful functionality. I&#8217;ve frequently used its JSON  encoding and decoding functionality. It works great, however, it doesn&#8217;t add spaces or linefeeds to make the resulting JSON string more readable. That&#8217;s fine if you are just serializing something to send over the wire but not if you want it to render something that is easily read by a human. In my case, I want a user to actually be able to edit the resulting JSON. To make this workable, I needed a JSON encoder that would add appropriate linefeeds and spaces. Rather than write my own, I simply adapted the one in as3corelib.</p>
<p>One side benefit of having done this is that you can now get JSON serialization without getting the entire as3corelib library.</p>
<p>The default interface is identical to the one in as3corelib. If you just call JSON.encode(my_object), it will behave <span style="text-decoration: line-through;">almost </span>exactly like the one in as3corelib. <span style="text-decoration: line-through;">I say &#8220;almost&#8221; because my version add a space after each &#8220;:&#8221; and &#8220;,&#8221; even in default mode.</span> <strong>Update</strong>: I&#8217;ve changed it so it behaves exactly like the serializer in as3corelib so no extra spaces are added unless you use the optional parameters described below.</p>
<p>If you want linefeeds and truly &#8220;pretty&#8221; output, you can add an optional second parameter, like so JSON.encode(my_object, true). This will cause any array [ ] or object { } that would be longer than 60 characters to wrap to newlines, which works out about right for my purposes.</p>
<p>You can also adjust the maximum line length with an optional third parameter, like this JSON.encode(my_object, true, 10). This will cause any line above 10 to wrap. If you want every array [ ] and object { } to wrap, just use any number 2 or lower in this third parameter. If you want it to wrap everything but empty objects or arrays, use 3 for this parameter.</p>
<p>Let&#8217;s see it in action.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">import</span> com.<span style="color: #006600;">maccherone</span>.<span style="color: #006600;">json</span>.<span style="color: #006600;">JSON</span>;
    <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;    
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Tester <span style="color: #0066CC;">extends</span> Sprite
    <span style="color: #66cc66;">&#123;</span>
&nbsp;
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Tester<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">var</span> obj1:<span style="color: #0066CC;">Object</span> = <span style="color: #66cc66;">&#123;</span>
                commit: <span style="color: #66cc66;">&#123;</span>file: <span style="color: #ff0000;">&quot;commit.xml&quot;</span><span style="color: #66cc66;">&#125;</span>,
                commit_detail: <span style="color: #66cc66;">&#123;</span>file: <span style="color: #ff0000;">&quot;commit_detail.xml&quot;</span><span style="color: #66cc66;">&#125;</span>,
                file: <span style="color: #66cc66;">&#123;</span>file: <span style="color: #ff0000;">&quot;file.xml&quot;</span><span style="color: #66cc66;">&#125;</span>,
                person: <span style="color: #66cc66;">&#123;</span>file: <span style="color: #ff0000;">&quot;person.xml&quot;</span><span style="color: #66cc66;">&#125;</span>,
                count: <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span>
            <span style="color: #66cc66;">&#125;</span>
            <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Just like as3corelib (no line feeds or spaces):<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> + JSON.<span style="color: #006600;">encode</span><span style="color: #66cc66;">&#40;</span>obj1<span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'&quot;Smart&quot; linefeeds:<span style="color: #000099; font-weight: bold;">\n</span>'</span> + JSON.<span style="color: #006600;">encode</span><span style="color: #66cc66;">&#40;</span>obj1, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Only allow short lines:<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> + JSON.<span style="color: #006600;">encode</span><span style="color: #66cc66;">&#40;</span>obj1, <span style="color: #000000; font-weight: bold;">true</span>, <span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
            <span style="color: #000000; font-weight: bold;">var</span> obj2:<span style="color: #0066CC;">Object</span> = <span style="color: #66cc66;">&#123;</span>
                <span style="color: #ff0000;">&quot;glossary&quot;</span>: <span style="color: #66cc66;">&#123;</span>
                    <span style="color: #ff0000;">&quot;title&quot;</span>: <span style="color: #ff0000;">&quot;example glossary&quot;</span>,
                    <span style="color: #ff0000;">&quot;GlossDiv&quot;</span>: <span style="color: #66cc66;">&#123;</span>
                        <span style="color: #ff0000;">&quot;title&quot;</span>: <span style="color: #ff0000;">&quot;S&quot;</span>,
                        <span style="color: #ff0000;">&quot;GlossList&quot;</span>: <span style="color: #66cc66;">&#123;</span>
                            <span style="color: #ff0000;">&quot;GlossEntry&quot;</span>: <span style="color: #66cc66;">&#123;</span>
                                <span style="color: #ff0000;">&quot;ID&quot;</span>: <span style="color: #ff0000;">&quot;SGML&quot;</span>,
                                <span style="color: #ff0000;">&quot;SortAs&quot;</span>: <span style="color: #ff0000;">&quot;SGML&quot;</span>,
                                <span style="color: #ff0000;">&quot;GlossTerm&quot;</span>: <span style="color: #ff0000;">&quot;Standard Generalized Markup Language&quot;</span>,
                                <span style="color: #ff0000;">&quot;Acronym&quot;</span>: <span style="color: #ff0000;">&quot;SGML&quot;</span>,
                                <span style="color: #ff0000;">&quot;Abbrev&quot;</span>: <span style="color: #ff0000;">&quot;ISO 8879:1986&quot;</span>,
                                <span style="color: #ff0000;">&quot;GlossDef&quot;</span>: <span style="color: #66cc66;">&#123;</span>
                                    <span style="color: #ff0000;">&quot;para&quot;</span>: <span style="color: #ff0000;">&quot;A meta-markup language, used to create markup languages such as DocBook.&quot;</span>,
                                    <span style="color: #ff0000;">&quot;GlossSeeAlso&quot;</span>: <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;GML&quot;</span>, <span style="color: #ff0000;">&quot;XML&quot;</span><span style="color: #66cc66;">&#93;</span>
                                <span style="color: #66cc66;">&#125;</span>,
                                <span style="color: #ff0000;">&quot;GlossSee&quot;</span>: <span style="color: #ff0000;">&quot;markup&quot;</span>
                            <span style="color: #66cc66;">&#125;</span>
                        <span style="color: #66cc66;">&#125;</span>
                    <span style="color: #66cc66;">&#125;</span>
                <span style="color: #66cc66;">&#125;</span>
            <span style="color: #66cc66;">&#125;</span>
            <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;A bigger example from JSON.org:<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> + JSON.<span style="color: #006600;">encode</span><span style="color: #66cc66;">&#40;</span>obj2, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The above code would result in the following output:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Just like as3corelib (no line feeds or spaces):
{&quot;file&quot;:{&quot;file&quot;:&quot;file.xml&quot;},&quot;commit&quot;:{&quot;file&quot;:&quot;commit.xml&quot;},&quot;commit_detail&quot;:{&quot;file&quot;:&quot;commit_detail.xml&quot;},&quot;person&quot;:{&quot;file&quot;:&quot;person.xml&quot;},&quot;count&quot;:[1,2,3]}
&nbsp;
&quot;Smart&quot; linefeeds:
{
    &quot;file&quot;: {&quot;file&quot;: &quot;file.xml&quot;},
    &quot;commit&quot;: {&quot;file&quot;: &quot;commit.xml&quot;},
    &quot;commit_detail&quot;: {&quot;file&quot;: &quot;commit_detail.xml&quot;},
    &quot;person&quot;: {&quot;file&quot;: &quot;person.xml&quot;},
    &quot;count&quot;: [1, 2, 3]
}
&nbsp;
Only allow short lines:
{
    &quot;file&quot;: {
        &quot;file&quot;: &quot;file.xml&quot;
    },
    &quot;commit&quot;: {
        &quot;file&quot;: &quot;commit.xml&quot;
    },
    &quot;commit_detail&quot;: {
        &quot;file&quot;: &quot;commit_detail.xml&quot;
    },
    &quot;person&quot;: {
        &quot;file&quot;: &quot;person.xml&quot;
    },
    &quot;count&quot;: [1, 2, 3]
}
&nbsp;
A bigger example from JSON.org:
{
    &quot;glossary&quot;: {
        &quot;GlossDiv&quot;: {
            &quot;GlossList&quot;: {
                &quot;GlossEntry&quot;: {
                    &quot;GlossSee&quot;: &quot;markup&quot;,
                    &quot;GlossTerm&quot;: &quot;Standard Generalized Markup Language&quot;,
                    &quot;ID&quot;: &quot;SGML&quot;,
                    &quot;GlossDef&quot;: {
                        &quot;para&quot;: &quot;A meta-markup language, used to create markup languages such as DocBook.&quot;,
                        &quot;GlossSeeAlso&quot;: [&quot;GML&quot;, &quot;XML&quot;]
                    },
                    &quot;Abbrev&quot;: &quot;ISO 8879:1986&quot;,
                    &quot;Acronym&quot;: &quot;SGML&quot;,
                    &quot;SortAs&quot;: &quot;SGML&quot;
                }
            },
            &quot;title&quot;: &quot;S&quot;
        },
        &quot;title&quot;: &quot;example glossary&quot;
    }
}</pre></div></div>

<p>Note that (unlike XML) the order of the elements in a JSON object { } is indeterminant. Of course the order of an array [ ] is preserved.</p>
<p>You can download it from <a href="http://maccherone.com/larry/projects/a-pretty-json-encoder-for-actionscript-3-as3/">here</a>.</p>
<div style="clear:both;">&nbsp;</div>


Please vote for this on DZone at the top of this page and share it on:


	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fmaccherone.com%2Flarry%2F2009%2F05%2F28%2Factionscript-3-as3-json-encoder-with-pretty-output-by-adding-linefeeds-and-spaces%2F&amp;t=ActionScript%203%20%28AS3%29%20JSON%20encoder%20with%20%22pretty%22%20output%20by%20adding%20linefeeds%20and%20spaces" title="Facebook"><img src="http://maccherone.com/larry/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=ActionScript%203%20%28AS3%29%20JSON%20encoder%20with%20%22pretty%22%20output%20by%20adding%20linefeeds%20and%20spaces%20-%20http%3A%2F%2Fmaccherone.com%2Flarry%2F2009%2F05%2F28%2Factionscript-3-as3-json-encoder-with-pretty-output-by-adding-linefeeds-and-spaces%2F" title="Twitter"><img src="http://maccherone.com/larry/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fmaccherone.com%2Flarry%2F2009%2F05%2F28%2Factionscript-3-as3-json-encoder-with-pretty-output-by-adding-linefeeds-and-spaces%2F&amp;title=ActionScript%203%20%28AS3%29%20JSON%20encoder%20with%20%22pretty%22%20output%20by%20adding%20linefeeds%20and%20spaces&amp;bodytext=I%27m%20sure%20many%20ActionScript%203%20or%20Flex%20developers%20have%20used%20the%20as3corelib%20for%20one%20reason%20or%20another.%20It%27s%20a%20wonderful%20little%20library%20with%20lots%20of%20useful%20functionality.%20I%27ve%20frequently%20used%20its%20JSON%C2%A0%20encoding%20and%20decoding%20functionality.%20It%20works%20great" title="Digg"><img src="http://maccherone.com/larry/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fmaccherone.com%2Flarry%2F2009%2F05%2F28%2Factionscript-3-as3-json-encoder-with-pretty-output-by-adding-linefeeds-and-spaces%2F&amp;title=ActionScript%203%20%28AS3%29%20JSON%20encoder%20with%20%22pretty%22%20output%20by%20adding%20linefeeds%20and%20spaces" title="StumbleUpon"><img src="http://maccherone.com/larry/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fmaccherone.com%2Flarry%2F2009%2F05%2F28%2Factionscript-3-as3-json-encoder-with-pretty-output-by-adding-linefeeds-and-spaces%2F&amp;title=ActionScript%203%20%28AS3%29%20JSON%20encoder%20with%20%22pretty%22%20output%20by%20adding%20linefeeds%20and%20spaces&amp;notes=I%27m%20sure%20many%20ActionScript%203%20or%20Flex%20developers%20have%20used%20the%20as3corelib%20for%20one%20reason%20or%20another.%20It%27s%20a%20wonderful%20little%20library%20with%20lots%20of%20useful%20functionality.%20I%27ve%20frequently%20used%20its%20JSON%C2%A0%20encoding%20and%20decoding%20functionality.%20It%20works%20great" title="del.icio.us"><img src="http://maccherone.com/larry/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fmaccherone.com%2Flarry%2F2009%2F05%2F28%2Factionscript-3-as3-json-encoder-with-pretty-output-by-adding-linefeeds-and-spaces%2F&amp;title=ActionScript%203%20%28AS3%29%20JSON%20encoder%20with%20%22pretty%22%20output%20by%20adding%20linefeeds%20and%20spaces&amp;annotation=I%27m%20sure%20many%20ActionScript%203%20or%20Flex%20developers%20have%20used%20the%20as3corelib%20for%20one%20reason%20or%20another.%20It%27s%20a%20wonderful%20little%20library%20with%20lots%20of%20useful%20functionality.%20I%27ve%20frequently%20used%20its%20JSON%C2%A0%20encoding%20and%20decoding%20functionality.%20It%20works%20great" title="Google Bookmarks"><img src="http://maccherone.com/larry/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.dzone.com/links/add.html?url=http%3A%2F%2Fmaccherone.com%2Flarry%2F2009%2F05%2F28%2Factionscript-3-as3-json-encoder-with-pretty-output-by-adding-linefeeds-and-spaces%2F&amp;title=ActionScript%203%20%28AS3%29%20JSON%20encoder%20with%20%22pretty%22%20output%20by%20adding%20linefeeds%20and%20spaces" title="DZone"><img src="http://maccherone.com/larry/wp-content/plugins/sociable/images/dzone.png" title="DZone" alt="DZone" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://maccherone.com/larry/?page_id=0?</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
