Thursday, May 05, 2011

Tcl Object Notation

In the beginning, everything was a plain text file or a binary file. Binary files were easy for computers (just copy the bytes from disk into memory) and hard for people. Text files were easy for people and hard for computers.

On top of this, things were always changing. So the binary files from Word 1.0 didn't want to be read into Word 2.0 (well, 2.0 would upgrade them - but eventually, support could disappear). Text files have a way of expanding into their own Turing-complete language.

Then some mega-genius said, "Hey, let's make a text format that easy for computers and people, and backwards/forwards compatible! We'll call it XML."

And so we got this:
<?xml version="1.0"?>
<NSTARS_UNIV DIMENSIONS="2" X="400" Y="400">
<UNIV_HDR>
<STAR_COUNT>2</STAR_COUNT>
</UNIV_HDR>
<GAME_YEAR>2406</GAME_YEAR>
<TECH_COSTS>50 80 130 210 340 550 890 1440 2330 3770 6100 9870 13850 18040
22440 27050 31870 36900 42140 47590 53250 59120 65200 71490 77990 84700</TECH_C
OSTS>
<COMPONENTFILENAME>newStarsComponents.xml</COMPONENTFILENAME>
<HULLFILENAME>newStarsHulls.xml</HULLFILENAME>
<PLAYERFILEBASENAME>tiny_sparse</PLAYERFILEBASENAME>
<MASTER_COPY>1</MASTER_COPY>
<NUMBER_OF_PLAYERS>2</NUMBER_OF_PLAYERS>
<PLAYERDATA>
<RACELIST>
<RACE>
<SINGULARNAME>Ugly Duckling</SINGULARNAME>
<PLURALNAME>Baby Swans</PLURALNAME>


That's the first 771 bytes of the NewStars master file (which is 3530 bytes in full). That's turn 6 for a tiny universe. I generated a huge turn once, it was many megabytes... Every AJAX request is generating and shipping around stuff like this. If you wonder why servers can't handle many clients, why the Internet is so slow (even though we have a lot more bandwidth than the old 56k modems), and clients are so slow - XML is a big part of it.

Then another (smarter) genius said, "All these angle brackets and matching tags are just a pain. Why can't we have a simpler format?" That gave us JSON (JavaScript Object Notation, and AJAJ). Here is the JSON file from my Space Battle project:
{
"force1" : {
"name" : "Imperials",
"ships" : [
"idest"
]
},

"force2" : {
"name" : "Bugs",
"ships" : [
"bship"
]
}
}


A lot more concise. But, why do I need all the quotes and colons? Why are some things in square brackets, while others are in curlies? We can do better:
Name {Tester (3)}
FactionType { (War 1, Trade 1, Magic 1)}
Month March
Year 1
VerString {4.1.0}
Rulesetname {Ceran}
Rulesetversion {2.0.4 (beta)}
Newssheet 1
Password {none}
TurnCountdown -1
Quit 0
TaxRegion 0
MaxTax 10
TradeRegion 0
MaxTrade 10
NumMage 1
MaxMage 2


This is "Tcl Object Notation" (TON). Just as JSON yields a JavaScript dictionary, this is a Tcl dictionary. Since everything is a string, no quotes needed. No colons (dictionaries are lists with "key" "value" pairs). You just need {} for things which might have spaces (or sub-dictionaries).

This will be the turn file format for the True Atlanteans, Atlantis PBEM GUI Client.

No comments: