ECMAScript, Javascript, JScript and Sofu

March 8, 2008

Some say JavaScript is annoying and should be turned off in the Browser.
Some others say Javascript is a very ugly language to program with.I say they are both right.
I did one thing in Javascript till today (Plasma’s 3d map view to be exact), and I don’t liked it that much. Today I had one of those stupid ideas that take away your whole weekend (And more). After finishing the dokumentation of Sofu.Net (0.2.1), I thought: “Why not do Sofu in JavaScript?” and “How hard can it be?”.

Why Sofu in JavaScript: Sofu is a file format, JavaScript can’t access files. Makes not a lot of sense, does it….
Well there is one application. Since Perl and .net (escpecially ASP.NET) now can now work with sofu files, you can use it to send structured data to the client (Webbrowser). This is done mostly during AJAX calls to the server.
Lets review what other structured data formats are there for AJAX communication:

  1. XML: This is a big one, it has bindings to almost any language running on webservers, but it is quite slow to parse and it tends to get very big if you write understandable tags.
  2. JSON: (Java Script Object Notation) Its easy to generate in most, easy to parse for JS (unsecure if parsed wrong) but it doesn’t support references and circular data structures…
  3. YAML: (http://www.yaml.org/) This is the way to go, mostly. Its quite fast, a superset of JSON, safe and supports references. Has also bindings in almost every language (even .Net, but that one hasn’t been active for almost 2 years now).

So where does Sofu fit in?
Sofu also supports references and the same datastructures as YAML, and its smaller than XML. And there are bindings to .Net, Perl and D.

But mostly I’m doing it to see if I can do it.

Current progress:

  • Not documented for now (How do I document JavaScript anyway, is there a nice API doc tool like POD for perl or Sandcastle for .net)?
  • API is very much the same as SofuD or Data::Sofu::Object of sofu.pm (Or Sofu.Net if you write the first letter of each method in lowercase).
  • Still missing a way to overload “for (var x in SofuMap) {}”, but I guess there is none.
  • Output is also working. (Well into a textarea of the browser).
  • Parsing is due after I sleep.
  • No Binary Sofu (I don’t think its useful and/or needed).
  • No SofuML (SofuML output: maybe, Parsing: Nope, not needing the parsing problem again)
  • Rest is working fine.
Advertisements

Sofu – SofuML

February 16, 2008

Got bored (again) and wrote SofuML and binary file support for Sofu.Net.

Don’t get me wrong, but I don’t like XML for saving structured data. XML is fine for doing layout and stuff, but if you don’t use proper indentation (as most exporters do) it looks kinda confusing. If you do indentation with tabs, it looks better, but than there is the whole white-space issue: Try saving a value with a leading whitespace, you can’t difference it from an indentation. Sure, you can use xml:space = “preserve” (20 or 22 more bytes in every element), but than you can’t use indentation again.

Than you will have to write the whole name of the element again in the closing tag, (more space used) Which only serves one purpose: To help the parser (or something). You can’t close any other tag than the last opened. This leads to XML elements getting named very short, which defies the whole readability purpose. (Anyone who has seen a MS-Word XML file will probably agree here)

One the other hand XML is a standard and the whole shebang around it has gotten quite powerful. XSLT (with XPath) is one example, no other data-format can be converted that easily (especially to produce HTML output).

That’s why I thought it would be neat to convert .sofu files to .xml files. and I called it SofuML. So you can transform it easily without having to code a program.

XML already brings a way to reference other element (in the same file) and the rest that is needed. (This will still keep the whole whitespace issue, but you can escape leading and trailing whitespaces. (The whole .sofu file will not be changed after being written and read again, but the final product after converting depends a lot on the parser)

Take this .sofu file for example:

somemap={
somelist=(1 2 3)
somevalue="Hello World"
}

Will be converted to this:

<Sofu>
<Element key="somemap">
<Map>
<Element key="somelist">
<List>
<Value>1</Value>
<Value>2</Value>
<Value>3</Value>
</List>
</Element>
<Element key="somevalue">
<Value>Hello World</Value>
</Element>
</Map>
</Element>
</Sofu>

This is not that nice and small, but you can use XSLT to convert it into HTML or even back to Sofu.

The XSLT to convert it to sofu is not complete, (I have found no way to escape keys and values if needed,) but it is funny anyways.

This is what happens if you put it through a compatible browser (IE or Firefox works great): http://sofu.sf.net/test.xml, also check “View Sourcecode” and you will see its just the XML file.