<?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>PyRuby blog &#187; dsl</title>
	<atom:link href="http://www.pyruby.com/tag/dsl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.pyruby.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Mon, 12 Dec 2011 11:03:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Scala JSON Generator</title>
		<link>http://www.pyruby.com/2010/04/29/scala-json-generator/</link>
		<comments>http://www.pyruby.com/2010/04/29/scala-json-generator/#comments</comments>
		<pubDate>Fri, 30 Apr 2010 05:56:08 +0000</pubDate>
		<dc:creator>PyRuby</dc:creator>
				<category><![CDATA[geek]]></category>
		<category><![CDATA[scala]]></category>
		<category><![CDATA[dsl]]></category>
		<category><![CDATA[json]]></category>

		<guid isPermaLink="false">http://www.pyruby.com/?p=72</guid>
		<description><![CDATA[<p>I am working with Scala more and more at the moment.  I find it a really powerful and fun language to work in.  One of the recent tasks has been to translate various tab delimited data files into a common JSON format.  In java land, I&#8217;ve used Jackson to great effect, but [...]]]></description>
			<content:encoded><![CDATA[<p>I am working with <a href="http://www.scala-lang.org/">Scala</a> more and more at the moment.  I find it a really powerful and fun language to work in.  One of the recent tasks has been to translate various tab delimited data files into a common JSON format.  In java land, I&#8217;ve used <a href="http://jackson.codehaus.org">Jackson</a> to great effect, but it really doesn&#8217;t feel very Scala.  My colleague and I were busy following the Jackson route and had a working implementation, but it just didn&#8217;t feel clean, simple and Scala idiomatic.  That&#8217;s when we decided to define how we thought the api should work.</p>
<p>One of the great things about using Scala, is it&#8217;s a fantastic language for creating <a href="http://en.wikipedia.org/wiki/Domain-specific_language">DSLs</a>.  When I really don&#8217;t like the way the code that solves a specific problem looks, I like to write the pseudo code that feels right, and then figure out how to make that work.  With Scala, I have had a great deal of success in this approach.  We started off defining a JSON generator DSL that looked like this:-</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">def mapToJson(myMap: Map[String, String]) = {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; val json = jsonObject {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; field(&quot;cheese&quot;, myMap.get(&quot;name&quot;)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; jsonObject(&quot;Score&quot;) {
</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; field(&quot;aroma&quot;, myMap.get(&quot;smell&quot;)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; field(&quot;flavor&quot;, myMap.get(&quot;taste&quot;)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; field(&quot;texture&quot;, myMap.get(&quot;squidginess&quot;)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; }
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; jsonArray(&quot;servedOn&quot;) {
</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; jsonObject {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; field(&quot;cracker&quot;, myMap.get(&quot;crackerType&quot;)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; field(&quot;maker&quot;, myMap.get(&quot;crackerMaker&quot;)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; }
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; }
</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; jsonArray(&quot;sampledOn&quot;) {
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; value(myMap.get(&quot;firstTried&quot;)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; value(myMap.get(&quot;secondTried&quot;)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; value(myMap.get(&quot;thirdTried&quot;)
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; }
</div>
</li>
<li class="li2">
<div class="de2">}</div>
</li>
</ol>
</div>
<p>There was another case for jsonArray, which doesn&#8217;t involve having a field name, i.e. jsonArray { &#8230; }.  Whilst attempting to find a way to implement the DSL that would satisfy this, we had to make a small compromise.  The call jsonObject {&#8230;} and jsonObject(&#8220;fieldName&#8221;) {&#8230;} caused problems because the signatures of the DSL methods were indistinguishable when using them.  The compromise we struck on was jsonObject() {&#8230;} and jsonObject(&#8220;fieldName&#8221;) {&#8230;}, i.e. just adding the empty parenthesis.  This then is the resulting syntax, and the implementation can be found <a href="http://github.com/jtownley/Scala-Json-Generator">here</a>.  </p>
<p>This is a first cut, but as this library was introduced to solve a problem in our production app.  It will be tweaked and maintained so any bugs or extra features will quickly get incorporated.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pyruby.com/2010/04/29/scala-json-generator/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

