94 points · by fogus · 16y ago · 113 comments
I am actually using such isomorphisms in my current porting project. I'm literally getting a couple of orders of magnitude more productivity out of this over porting by hand.
Also note: less syntax makes this easier.
You'll be especially enamored of their 200-line TCP/IP implementation, introduced on p17 and reproduced with documentation starting on p44. It's implemented as a grammar in their meta-language that parses the ASCII-art diagrams in the RFCs and executes them.
So count me in. I'll use JSON over XML for server-webclient data exchange whenever I can. It's just much easier.
The good thing about XML was it was a standard interchange format that every language had at least a couple parsers for. That said, I'm not sure if it was worth the annoyance and irritation of using stuff like XML Schema, DTDs, SOAP, or XSLT.
SCNR
There seems to be something inherent in us that feels the need to turn our tools into our religion. It's not terribly productive.
CL's reader is especially rich, and it's programmable....
No on both counts.
(1) S-expressions aren't literals.
(2) s-expressions are not "parsed" by calling lisp eval. Lisp eval's argument is an s-expression - it doesn't parse anything. (It also doesn't read anything.) Lisp read turns character sequences into s-expressions.
> But JSON wins over s-expressions too, because s-expressions are too simple. How do you represent John Smith as an s-expression?
If you'd like, exactly the same way you'd represent it in JSON, because lisp's read handles a superset of the JSON datatypes.
> software that consumes s-expressions has to know how to interpret their structure
As does JSON.
Take your example John Smith. What JSON datatype do you expect to get? (JSON just has numbers, strings, arrays, booleans, and hashes, where the keys have a restricted format.)
But, there was something Lisp did right that no other mainstream scripting language has - it separated the reader and the evaluator. You can eval Perl or Ruby or Python, but not securely read it. This goes for Javascript too - you can eval a JSON, but you'd be an idiot.
Huh? CL's reader handles structs, hashes, and vectors in addition to strings, lists, atoms (with packages), lots of number formats (including Roman), and the ability to express AGs, not just trees (yes, cyclic too). Plus some other things that I forget. (Arrays?)
What else do you want to read?
No matter - CL's reader is programmable....
It's probably bad you can express cyclic graphs. That means you'll have to watch for denial-of-service attacks phrased as cyclic data.
Programmable reader isn't the point. That gets you back into defining your own syntax. The point is to have a standard.
JSON just assumes messages are going to trivially fit into naive data structures, and so provides fewer options.
A generic XML DOM is still complicated to deal with. Even if you do the "right thing" and use XPath, you still have to deal with XPath because you can't get around the fact that you have an underlying representation that has at least two dimensions (attributes vs. CDATA). That is, just as the article says, you have more degrees of freedom in how you represent your data, and what is a "degree of freedom" but a near synonym of "dimensionality"? You can't abstract around dimensionality very effectively without losing fundamental capabilities in the underlying component (in fact a staggering number of abstraction failures in general can be shown to come from exactly this problem if you really learn to think this way), and the complexity comes poking out in the XPath. It's still better than groveling over the DOM yourself, but it's probably also the absolute peak of concision that is obtainable; there will be nothing better.
JSON is indeed simpler in that you don't really have 3 or 4 feasible choices per attribute; {"first-name": "John", "last-name": "Smith"} is pretty much your choice, full stop. That leaves the underlying library fundamentally, not accidentally, simpler. This can get you into some trouble in some cases, for instance XML is a better choice for HTML-type tagged text as the JSON for tagged-text is just hideous (and, interestingly, reopens the dimensionality problem as there is no one obvious solution), but many things are fundamentally simpler than tagged-text.
If you want to pick up a defined serialization format, my gut would be to say to default to JSON and back to XML if you really need it for something... but be aware that you may, and it's no better to try to jam JSON on top of a fundamentally XML problem. (Besides, your JSON can carry bits of XML in it without much pain, so "best of both worlds" is perfectly feasible.)
(Although I like it when my target web apps use XML; better tools support for attacking them.)
With json I have to resort to imperative means and care about the middle layers. That means refactorings are more likely to break software.
Fundamentally there is no reason why json cannot have it's own selector which traverses the tree. There just isn't one that I know of though.
I've gotten the informal impression that incidental complexity (you know, complexity arising not from the problem but the solution) is a factor behind the hugeness of the XML ecosystem, but I'd be happy to learn otherwise...
You are absolutely correct.
As Phil Wadler put it: "The essence of XML is this: the problem it solves is not hard, and it does not solve the problem well."
It makes a trivial issue into a byzantine enterprise.
But, it has created a whole industry of 'experts', standard committees and other busybodies, so it must be good for the economy at least!
The CPU is spending more time dealing with XML than it is doing useful work. If it's a big file, this is very significant.
In JSON, you get the same expressiveness without the hassle.
An XML parser has to track open tag names, with JSON it doesn't matter. XML has all stupid entities like & which look ugly and need to be parsed.
I think "time to write a parser" should be a good metric on how sane a data format is. The fact that writing an XML parser that covers all bases/eventualities is a major undertaking says alot about the data format.
<thingies> <thingy> <name>thingy1</name> </thingy> <thingy> ... </thingies>
I've seen that a billion times, if you're doing DOM, you have to pull that whole thing into memory and then run over it again. Most JSON-based storage systems I've seen recently are more record based so you can stream it through.
So I guess my beef on that one isn't specifically with XML, you could split the above snippet into separate entities.. but I'll note that it still involves about 90% markup and 10% data. Not exactly the most efficient thing possible.
You do that with JSON too, don't you? Are there SAX-like parsers for JSON?
If it's a big file it'll be big in JSON too.
XML should be used for markup, JSON or YAML for structured data.
The trouble was that it was the all-purpose data exchange tool for a long time, and everyone has seen XML put to all sorts of ungodly purposes. If it were invented today, nobody would be bashing it, because it would be less widely used -- the few people who did decide to use it would be putting it to good use, so they would love it. Everyone else would be using something else that was simpler.
This begs the question, "Why would you do something that convoluted?" Well, you can ask the same of the XML examples, and the answer probably boils down to requirements (or incompetence?).
<person first-name="John" last-Name="Smith" />
is better than <person>
<first-name>John</first-name>
<last-name>Smith</last-name>
</person>
is often very difficult. In the wild, I've seen both strategies used depending on the situation.There is also the case where you thought you conveyed the new meaning, while it hasn't caught on yet (meaning, you made a mistake). So, better stay safe and stick to the old meaning while we can.
I often don't know what the user meant. It makes for extra work to stop and think what possible meaning was intended. Some thing when people use "less" for "fewer". I have to think, did they mean "lesser" or "fewer"?
Conversions between XML and JSON can be a challenge (defining namespaces,etc.). The Google Data API handles this very well. Check out their nice side-by-side example: http://code.google.com/apis/gdata/docs/json.html
If the code is structured to reveal this intention explicitly, then job well done.
(Note: It's not always a good idea to rest the future security of your system on a comment in the code!)
The weakness in the Javascript "eco-system" is it's quasi (the 'quasi' qualifier applies frequently in this eco-system) support of objects. XSD/XML is powerful when used in an object paradigm, and object paradigms have proven (not just via community claims) to be very effective.
The question is, when will the JS community step up to the plate (i.e. mature)? So much energy is wasted now on making JSON work -- just in order to make Javascript easy. Wrong priorities.