Json

Imports
open Array;
Table of Contents

Types

Json

(type)
type Json

Constructors

For each type in this module: inductive types list their declared constructors; opaque types list any local function whose name starts with mk and which returns a value of that type.

Json (opaque · mk*)

mkNull

def mkNull : {r : Json | kind r = 0}

mkBool

def mkBool (b : Bool) : {r : Json | kind r = 1}

mkInt

def mkInt (i : Int) : {r : Json | kind r = 2}

mkFloat

def mkFloat (f : Float) : {r : Json | kind r = 3}

mkString

def mkString (s : String) : {r : Json | kind r = 4}

mkArray

def mkArray (1 xs : (Array Json)) : {r : Json | kind r = 5 && size r = Array.size xs}

mkObject

An empty JSON object. Use setKey to populate.
def mkObject : {r : Json | kind r = 6 && size r = 0}

Functions

isValid

def isValid (s : String) : {b : Bool | b = validSource s}

json

def json : Unit

parse

Parses JSON validated with isValid, or produced by stringify.
def parse (s : String | validSource s) : Json

parseFile

Parses a JSON file at path.
def parseFile (path : String | path ≠ "") : Json

stringify

Serializes a Json value to its string form. The result is always non-empty (json.dumps(None) yields "null").
def stringify (j : Json) : {s : String | s ≠ "" && validSource s}

stringifyPretty

Pretty-prints a Json value with 2-space indentation. Always non-empty.
def stringifyPretty (j : Json) : {s : String | s ≠ "" && validSource s}

writeFile

Writes a Json value to a file.
def writeFile (path : String | path ≠ "") (j : Json) : Unit

isNull

def isNull (j : Json) : {b : Bool | b = (kind j = 0)}

isBool

def isBool (j : Json) : {b : Bool | b = (kind j = 1)}

isNumber

def isNumber (j : Json) : {b : Bool | b = (kind j = 2 || kind j = 3)}

isString

def isString (j : Json) : {b : Bool | b = (kind j = 4)}

isArray

def isArray (j : Json) : {b : Bool | b = (kind j = 5)}

isObject

def isObject (j : Json) : {b : Bool | b = (kind j = 6)}

asBool

def asBool (j : Json | kind j = 1) : Bool

asInt

def asInt (j : Json | kind j = 2) : Int

asFloat

def asFloat (j : Json | kind j = 2 || kind j = 3) : Float

asString

def asString (j : Json | kind j = 4) : String

has

Returns true if j is an object containing key.
def has (j : Json) (key : String) : {b : Bool | b = containsKey j key && (b = false || kind j = 6)}

get

Looks up a key in a JSON object. Raises at runtime if j is not an object or key is absent.
def get (j : Json | kind j = 6) (key : String | containsKey j key) : Json

length

Returns the length of an array (or string/object). Raises if j has no len.
def length (j : Json | kind j = 4 || kind j = 5 || kind j = 6) : {x : Int | x ≥ 0 && x = size j}

at

Indexes into a JSON array. Raises at runtime if i is out of range.
def at (j : Json | kind j = 5) (i : Int | i ≥ 0 && i < size j) : Json

mkNull

def mkNull : {r : Json | kind r = 0}

mkBool

def mkBool (b : Bool) : {r : Json | kind r = 1}

mkInt

def mkInt (i : Int) : {r : Json | kind r = 2}

mkFloat

def mkFloat (f : Float) : {r : Json | kind r = 3}

mkString

def mkString (s : String) : {r : Json | kind r = 4}

mkArray

def mkArray (1 xs : (Array Json)) : {r : Json | kind r = 5 && size r = Array.size xs}

mkObject

An empty JSON object. Use setKey to populate.
def mkObject : {r : Json | kind r = 6 && size r = 0}

setKey

Returns a new object with key set to value. Raises at runtime if j is not an object.
def setKey (j : Json | kind j = 6) (key : String) (value : Json) : {r : Json | kind r = 6 && containsKey r key}

isInt

def isInt (j : Json) : {b : Bool | b = (kind j = 2)}

Uninterpreted

Functions declared as def f ... = uninterpreted: only their signature is known to the verifier; they have no body.

kind

uninterpreted
def kind : (j : Json) → Int

size

uninterpreted
def size : (j : Json) → Int

containsKey

uninterpreted
def containsKey : (j : Json) → (key : String) → Bool

validSource

uninterpreted
def validSource : (s : String) → Bool