<?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; JSON</title>
	<atom:link href="http://maccherone.com/larry/tag/json/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>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>
