JSON values of an ESM document, in canonical form
The value type the document codecs (Esm.Document.Yaml
, Esm.Document.TypstDict
)
print and read. Numbers are already in the canonical form of CONFORMANCE §5.5.3.1:
- rule 1 and rule 3: a number whose value is integral and representable in
Int64is the integer literal (1.0IS the integer1), so it isJNum.int; - rule 2: every other finite number is
JNum.dec, its shortest round-trip decimal digits and the decimal exponent of the first digit. Binary64 is not modelled; the shortest digits are given data.
Object key order is part of the value (the codecs preserve it; they never sort).
inductive JNum where
| int (z : Int)
| dec (neg : Bool) (digits : List Nat) (e10 : Int)
deriving DecidableEq, Repr
A JSON number in canonical form.
int z: an integer literal (well formed whenzfits inInt64).dec neg ds e10: the value±d₁.d₂d₃… × 10^e10,-whenneg, withdsthe shortest round-trip digits.
def digitsVal (ds : List Nat) : Nat
The value of a big-endian decimal digit list.
def decInt? (neg : Bool) (ds : List Nat) (e10 : Int) : Option Int
The integer a decimal denotes, when the decimal is integral (its last digit sits at
or left of the units place), else none.
def WF : JNum → Prop
Canonical form of a number:
- an
intfits inInt64; - a
dechas nonempty digits, each below 10, with no leading and no trailing zero, and is not an integralInt64value (that number is anint, §5.5.3.1 rule 1).
inductive Json where
| null
| bool (b : Bool)
| num (n : JNum)
| str (s : String)
| arr (xs : List Json)
| obj (kvs : List (String × Json))
deriving Repr
A JSON value. Strings and keys are arbitrary Lean strings (any Unicode scalar values); object entries keep their order.
def WF : Json → Prop
Well formed: every number is canonical and every object has distinct keys.