|(require csexp)||package: csexp|
Canonical s-expressions (csexp) are a way to format s-expressions into data packets suitable for transmission over a network.
Consider the following expression:
'(bag "Inventory" (content cookie caramel aligator))
To get a canonical s-exp, it must first be changed into a tree of bytestrings:
Which can then be transmitted as:
Spaces are removed and all strings are suffixed by their size and a colon : separator. This makes it easy to read the expression in a stream, and gives a unique (canonical) way of writing a given s-expression.
Canonical s-expressions can have an optional "hint", which is a bytestring attached to the front of a csexp atom, inside square brackets:
Here the hint is attached to the atom The Cat and has the value charset=utf-8.
When parsing a csexp, hints are rendered by default as hinted structures.
> (bytes->csexp #"(6:animal(4:name[13:charset=utf-8]7:The Cat)(5:noise4:meow))")
(list #"name" (hinted #"The Cat" #"charset=utf-8"))
If hinting is 'if-present, it generates a hinted instance every time a hint
If hinting is 'always, it generates hinted instances for each atom, with #f as the default hint value.
If hinting is 'never, it ignores any hint and just returns bytestrings.
port : input-port? hinting : (or/c 'if-present 'never 'always) = 'if-present