Simple encoding and decoding
The basic OpenMath API can be used to create OpenMath data structures of arbitrary complexity and type. But most use cases can be handled with only a subset of that full complexity, and we provide the following tools for doing so.
OMNode.simpleDecode() takes a string as input (like
does), but this string is in a much simpler form. Here are the formats it
anyIdentifierwill be treated as a variable. Examples:
ident1.ident2will be treated as a symbol (CD and name, respectively). Examples:
- any integer will be treated as an integer. Examples:
- 57328074078459027340 (value will be a string, due to size)
- any float will be treated as a float. Examples:
- a string literal enclosed in quotation marks (
") will be treated as a string, but with no support for escape codes, other than
"this is a string"
- a string literal enclosed in single quotes (
') behaves the same way, escaping only
'this is also a string, ain\'t it?'
Fis any valid form and each
Aiis as well, is interpreted as the application of
Aiin the order given. Here
nmay be zero. Examples:
F[A1,...,An]behaves the same as the previous case, except that the
Anmust all be variables, and they will be bound; i.e., this yields an OpenMath binding object, not an application object. Examples:
This syntax does not allow for the expression of OpenMath error objects, attributions, symbol URIs, byte arrays, or very large integers.
The inverse to the above function is a simple encoding function. It can operate on only a subset of the full complexity of OMNode trees, and thus in some cases it gives results that are not representative of the input. Here are the details:
- integers, floats, and strings will all be correctly encoded
- variables without dots in their names will be correctly encoded; those with dots in their names conflict with the naming of symbols in the simple encoding, but will be encoded as their names
- symbols will be correctly encoded with the exception that any URI will be dropped, and the same issue with dots applies to symbol and CD names
- byte arrays and errors have no simple encoding, and will thus all be converted to a string containing the words "byte array" or "error," respectively
- all attributions are dropped
const sum = OM.simple( 'arith1.plus( 3, 4 )' )