title: Noise-free JSON processing with Scala
body: <p>I'm coming from a dotnet land, but recently have been looking at the possibilities of alternative programming languages. Nothing really serious, just some bits here and there. Recently I've discovered Scala and I'm pretty fascinated with it. Despite non-deterministic tinkering, I've done some intermediate checks of stuff that is important for me in C# and I feel rather satisfied: functional notions - tick, ad-hoc polymorphism - tick, annotations - tick, reflection and codegen - tick.</p>
  
  <p>Now I'm thinking about how one would program an analogue of JSON processing library I've implemented in C# 4.0 with the help of DLR and "dynamic" syntactic sugar. Here's the feature set I'm looking for:</p>
  
  <ol>
  <li>Convenient browsing and construction of raw JSON.</li>
  <li>Automatic conversion between JSON and native objects/collections (in its general form the problem is unsolvable, though one can define conventions that will work 95% of the time - and that's fine for me).</li>
  </ol>
  
  <p>New features of C# 4.0 kinda rock here, since they let me override member access and type casts to perform completely custom logic (if a variable in C# 4.0 is typed as "dynamic", then anything you do with it will be compiled into calls to programmer-defined methods with reasonable default behaviour - see <a href="http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicmetaobject.aspx" rel="noreferrer">DynamicMetaObject.BindXXX methods at MSDN</a> for more info). E.g. I've overriden type casts to serialize/deserialize .NET objects and member accesses to manage raw JSON, so that I can write the following code:</p>
  
  <pre><code>var json = Json.Get("http://some.service");
  if (json.foo) Console.WriteLine((Foo)json.foo);
  json.bars = ((List&lt;Bar&gt;)json.bars).DoSomething();
  </code></pre>
  
  <p>Of course, this is not ideal, since dynamic binding in C# 4.0 has problems with extension methods and type inference, and, moreover, the code still feels rather heavyweight. But anyways, this is much better than using all those ((JsonObject)json["quux"])["baz"] I've used to in c# 3.5.</p>
  
  <p>Some basic research shows that Scala doesn't have dedicated language features that support late binding. However, there are so many tricks that maybe they can be used together to create a bearable emulation of the code shown above (or even to be better - I almost sure that this is possible). Could you, please, advise me something here?</p>