Earlier quoted context omitted.
> XML was never designed as a data serialization format. It's a markup language. Those two things are not mutually exclusive. > Likewise, JSON is a subset of a general-purpose programming language's literal notation that happened to be very fast to parse in a browser by virtue of the browser implementing that language. That's true. That is not in conflict with anything I said. > The problem is that there's no one-siz…
One downside to S-exprs compared to, say, JSON: they do not have direct support for unordered mappings (hash tables, dictionaries, whatever you want to call them). You can represent them as trees, but basically every language these days (including, of course, Lisps) has a mapping type as a core concept; requiring the user to figure out what parts of the input data should be converted to that type is annoying, and mak…
However, Sexps easily support hash tables.
Example:
$ txr -p "(let ((x (hash))) (set [x 'a] 1) (set [x 'b] 2) x)"
#H(() (b 2) (a 1))
Here I have chosen a "hash capital H" syntax for hash tables. The first part () has the hash attributes (there are none, so it's an eql-equality-based hash table, with strong keys and values). Then, entries consisting of two element list pairs give the keys and values.This hash prefix notation builds on existing Lisp concept of using simple prefixes to distinguish various kinds of objects. In Common Lisp we have:
#(1 2 3) ;; this is a vector
#C(3.0 4.9) ;; this is the complex number 3.0 + 4.9i
The scanning is very simple: you just recognize the prefix like #C( or #( and then recurse into the scanner for list elements that stops at a closing parenthesis.No whitespace is allowed: it cannot be # (1 2 3) or # C (3.0 4.9).
TXR Lisp above not only reads back the hash notation, so it can be used as a literal, but allows backquoting over it. We can splice keys and values into the syntax to produce a hash table:
$ txr -p "(let ((keys '(a b c)) (vals '(1 2 3)))
^#H(() ,*(zip keys vals)))"
#H(() (c 3) (b 2) (a 1))