why xaml when you can json?

toshok | moonlight, mono, geek | Tuesday, August 14th, 2007

On Lake Washington last night, miguel mentioned that a cool hack would be to add a “create from json” method for moonlight, to compliment “createFromXaml”. The idea being that xaml (like all xml) is overly verbose and isn’t a first class part of the language in JS (so you don’t get syntax errors on missed quotes, etc).

I woke up this morning and ran the idea through my head, and figured it would be an hour hack. I was wrong. It took 1.5 hours.

So, you start with something like:

var json = {
  Canvas: {
    name: "Toplevel Canvas",    children: {
      TextBlock: {
        Text: "Hello World"
      }
    }
  }
}

and it converts that to the following xaml:

<Canvas xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml” x:Name=”Toplevel Canvas”><TextBlock Text=”Hello World”/></Canvas>

Here it is: createFromJson.js
.

Powered by WordPress | Theme by Roy Tanck