Warning
[EXPERIMENTAL] [WORK IN PROGRESS]
Reference implementation of the fitzJSON semantics interpreter in JavaScript.
Depends on the reference tree-sitter-fitzjson syntax.
Provides fitzJSON.parse
and fitzJSON.stringify
that work analogous to JSON.parse
and JSON.stringify
(see below for details).
npm i @xtao-org/fitzjson.js
To use, a parser object must be asynchronously produced first by makeFitzJSON
.
import {makeFitzJSON} from '@xtao-org/fitzjson.js'
const fitzJSON = await makeFitzJSON()
const input = `{"a":@bigint 2219302139021039219030213902193}`
const parsed = fitzJSON.parse(input)
const stringified = fitzJSON.stringify(parsed)
console.assert(input === stringified)
This library provides fitzJSON.parse
and fitzJSON.stringify
that work analogous to JSON.parse
and JSON.stringify
.
Aims to support the exact same interface and a superset of the behavior of these functions. For documentation of these, see MDN:
The implementations here were written according to these specifications, extending functionality in accordance with the features of fitzJSON.
In particular, in fitzJSON.parse:
- The second argument can be either a reviver function (like in JSON.strigify) OR an options object (unlike JSON.stringify). The options object may contain the reviver function in its
reviver
property.
Also, in fitzJSON.stringify:
- if a value has a
.toFitzJSON
method, it will be preferred over.toJSON
. The method has the same interface.
- BigInts will be represented as
@bigint <numerical value>
- Dates will be represented as
@date <ISO string produced by .toISOString()>
- +-Infinity and NaN will be represented literally
Map
s with string keys will be represented the same as objects
Map
s with nonstring keys will cause an error
- a special
Decorated
class is provided which can be used to put custom decorators in the output. An instance ofDecorated
will be represented as@<instance decorator> @<instance value>
. Such an instance should be constructed withnew Decorated(decoratorName, value)
wheredecoratorName
should be a valid fitzJSON identifier (pretty much the same as a JavaScript identifier), otherwise the constructor will throw an error.value
can be any stringifiable value.
Note: the well-formed JSON.stringify() specification is implemented, so lone UTF-16 surrogates will be properly escaped in the output.
node --test