Braces are usually used to indicate the begining and ending of a block. There's no semantics behind the brace, it is usually captured by some other nearby symbol such as while
, if
, etc... tags such as <foo></foo>
indicate the begining and ending of the block and capture the semantics.
I suppose you could change tag-based markup from
<ns:foo attrib="test" attrib2="123/>
some values and nested elements can go here
</ns:foo>
to:
ns:foo(attrib="test" attrib2="123")
{
some values and nested elements can go here
}
which still contains the semantics of the first example (we know that we are dealing with a foo
element from the ns
namespace and we have the element's attribute values) but in a different format.
Why markup is done with tags is as far as I can tell to make it easier to be read by humans. Scanning a sequence of closing }
's with eyes, it's easy to get lost as to what element is being closed. Fancy editors that highlight and keep track of what element your cursor is in can help, but you can't always garauntee you'll have this (such as when fixing an XML config file on a remote server using pico).
Basically, this:
</bar>
</foo>
</fizz>
</bang>
Is easier to read than:
}
}
}
}
I've seen XML with elements nested > 10 levels deep (much more often than I ever see code nested like that), and when working with these files, knowing which elements is being closed really helps.
In response to your question update:
I am not a JSON expert but a quick google showed me some examples. JSON's key-value pairs look like a nicer alternative to single-valued items, such as <foo>some value</foo>
but I still think that for deeply nested items, the issue of not knowing where you are on the screen is mitigated by having the closing tags.
An ideal, theortical, off-the-top-of-my-head combination, based on a wikipedia example:
<person>
"firstName": "John",
"lastName": "Smith",
"age": 25,
<address>
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
</address>
<phoneNumbers>
<phoneNumber>
"type": "home",
"number": "212 555-1234"
</phoneNumber>,
<phoneNumber>
"type": "fax",
"number": "646 555-4567"
</phoneNumber>
</phoneNumber>
</person>
Of course, we could have done most of those key-value pairs as attributes, such as:
<person firstName="John"
lastName="Smith"
age="25">
<address
streetAddress="21 2nd Street ...>
...
</person>
I think this would be even cleaner than most JSON syntax, and simple elements could be done as
<foo value="some value"/>