November 30, 2014

For my first post on my new, shiny code website, why not demonstrate a cool new feature of Dusk? I've been working on a new serialization notation, specifically created for conciseness, readability, flexibility, embedding simplicity, and lightweight-ness. The product of my work is Bang, a new serialization notation meant to be Dusk's "native" format.

Want a peek at some Bang notation? Here you go, in both JSON and Bang.

JSON

{
	"key1": true,
	"key2": "abc",
	"key3": [1, 2, 3, 4, 5],
	"key4": 1234567890
}

Bang

key1: true
key2: abc
key3: {1 2 3 4 5}
key4: 1234567890

The first difference between Bang and JSON you'll probably notice is that Bang has far less syntactic "noise" than JSON. One of Bang's aims is conciseness - when you're writing properties for your maps in Tiled, you've got to economize on space. JSON requires you to surround your table in curly braces or brackets, and (worse) requires you to place quotes around every identifier and commas after every value. None of that's needed. Bang uses no brackets or braces around the base table, allows you to omit quotes if strings - keys or values - are valid identifiers, and makes commas completely optional. Of course, if your fingers are cold some day, you can work them out by adding quotes around your strings and commas after your values. That's perfectly okay with Bang.

Something Bang has that wasn't shown in the sample is the idea of non-separate arrays and dictionaries. Lua has no distinctions between arrays and dictionaries, so a serialization language for Lua shouldn't have distinctions either. Bang is used to encode tables, not dictionaries and arrays, so you can mix array elements and dictionary elements together in one structure:

key: value "arrayElementWithQuotes" arrayElementWithoutQuotes, keyWithComma: true, arrayElementFinishingThingsOff

And here's another cool thing about Bang: the name. I picked the name "Bang" for a special reason - guess how you embed Bang notation in a Tiled property? To embed JSON, you prefix your value with !json!. To embed Bang, you prefix your value with !!! - the "this is a special prefix" exclamation marks with a "bang" symbol between them. The simple prefix makes Tiled properties very readable.

Finally, Bang is also extremely lightweight. Bang doesn't bloat Dusk up any - the whole library is only 88 lines long, counting blank lines and comments. Taking out comments and blank lines, it's only 44 lines.

Bang is built into Dusk as of today. Enjoy!