Types that can be decoded from JSON.
Use deriving FromJson
to automatically generate an instance.
See ToJson
for details of auto-generated instances.
Instances
Types that can be encoded as JSON.
Use deriving ToJson
to automatically generate an instance.
The following encoding strategy is employed by auto-generated instances:
Basic types corresponding to JSON values are encoded as these values.
Boolis encoded astrue/false.Strings are encoded as JSON strings.Numeric types are encoded as JSON numbers, with the exception of:
UInt64andUSizewhich are encoded as JSON strings. This is because, although JSON numbers proper have unbounded range, in JavaScript they are parsed as Numbers and these can only represent integers up to $2^{53} - 1$ safely; so a roundtrip through JavaScript would result in truncation on these types. (We use these types in the JavaScript-based Lean infoview.)Special
Floats which are encoded as JSON strings:"NaN"/"Infinity"/"-Infinity".
Unitis encoded as{}(empty JSON object).Option.noneis encoded asnull, whereassome ahas the same encoding asa. Note that this impliesOption (Option α)does not roundtrip, sincenoneandsome noneboth becomenull.General
structures are encoded as JSON objects in the obvious way.Optionfields whose names end with?have special support: the question mark is omitted from the JSON field name, and such a field is omitted from the JSON object when its value isnone(as opposed to being encoded as{ "someField": null }).
General
inductivetypes are encoded on a per-constructor basis.An argument-free constructor is encoded as its name (a JSON string).
A constructor with named arguments only is encoded as the JSON object
{ "ctorName": { "arg1": argVal1, ..., "argN": argValN } }.A constructor with one unnamed argument is encoded as the JSON object
{ "ctorName": argVal }.A constructor with more than one unnamed argument is encoded as the JSON object
{ "ctorName": [argVal1, ..., argValN] }.
Certain other types have special handling: see the instances below.
- toJson : α → Json
Instances
Equations
- Lean.instFromJsonJson = { fromJson? := Except.ok }
Equations
- Lean.instToJsonJson = { toJson := id }
Equations
- Lean.instFromJsonJsonNumber = { fromJson? := Lean.Json.getNum? }
Equations
- Lean.instToJsonJsonNumber = { toJson := Lean.Json.num }
Equations
- One or more equations did not get rendered due to their size.
Equations
- Lean.instToJsonUnit = { toJson := fun (x : Unit) => Lean.Json.obj ∅ }
Equations
- One or more equations did not get rendered due to their size.
Equations
- Lean.instToJsonEmpty = { toJson := fun (a : Empty) => nomatch a }
Equations
- Lean.instFromJsonBool = { fromJson? := Lean.Json.getBool? }
Equations
- Lean.instToJsonBool = { toJson := fun (b : Bool) => Lean.Json.bool b }
Equations
- Lean.instFromJsonNat = { fromJson? := Lean.Json.getNat? }
Equations
- Lean.instToJsonNat = { toJson := fun (n : Nat) => Lean.Json.num (Lean.JsonNumber.fromNat n) }
Equations
- Lean.instFromJsonInt = { fromJson? := Lean.Json.getInt? }
Equations
- Lean.instToJsonInt = { toJson := fun (n : Int) => Lean.Json.num (Lean.JsonNumber.fromInt n) }
Equations
- Lean.instFromJsonString = { fromJson? := Lean.Json.getStr? }
Equations
- Lean.instToJsonString = { toJson := fun (s : String) => Lean.Json.str s }
Equations
- Lean.instFromJsonSlice = { fromJson? := Except.map String.toSlice ∘ Lean.Json.getStr? }
Equations
- Lean.instToJsonSlice = { toJson := fun (s : String.Slice) => Lean.Json.str s.copy }
Equations
- Lean.instFromJsonFilePath = { fromJson? := fun (j : Lean.Json) => System.FilePath.mk <$> j.getStr? }
Equations
- Lean.instToJsonFilePath = { toJson := fun (p : System.FilePath) => Lean.Json.str p.toString }
Equations
- Array.fromJson? (Lean.Json.arr a) = Array.mapM Lean.fromJson? a
- Array.fromJson? x✝ = throw (toString "expected JSON array, got '" ++ toString x✝ ++ toString "'")
Instances For
Equations
- Lean.instFromJsonArray = { fromJson? := Array.fromJson? }
Equations
- a.toJson = Lean.Json.arr (Array.map Lean.toJson a)
Instances For
Equations
- Lean.instToJsonArray = { toJson := Array.toJson }
Equations
Instances For
Equations
- Lean.instFromJsonList = { fromJson? := List.fromJson? }
Equations
- a.toJson = Lean.toJson a.toArray
Instances For
Equations
- Lean.instToJsonList = { toJson := List.toJson }
Equations
Instances For
Equations
- Lean.instFromJsonOption = { fromJson? := Option.fromJson? }
Equations
- none.toJson = Lean.Json.null
- (some a).toJson = Lean.toJson a
Instances For
Equations
- Lean.instToJsonOption = { toJson := Option.toJson }
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- Lean.instFromJsonProd = { fromJson? := Prod.fromJson? }
Equations
Instances For
Equations
- Lean.instToJsonProd = { toJson := Prod.toJson }
Equations
- Lean.instFromJsonName = { fromJson? := Lean.Name.fromJson? }
Equations
- Lean.instToJsonName = { toJson := fun (n : Lean.Name) => Lean.Json.str (toString n) }
Equations
- Lean.instFromJsonNameMap = { fromJson? := Lean.NameMap.fromJson? }
Equations
- m.toJson = Lean.Json.obj (Std.TreeMap.foldl (fun (n : Std.TreeMap.Raw String Lean.Json compare) (k : Lean.Name) (v : α) => n.insert k.toString (Lean.toJson v)) ∅ m)
Instances For
Equations
- Lean.instToJsonNameMap = { toJson := Lean.NameMap.toJson }
Equations
Instances For
Equations
- Lean.instFromJsonUSize = { fromJson? := USize.fromJson? }
Equations
- Lean.instToJsonUSize = { toJson := fun (v : USize) => Lean.bignumToJson v.toNat }
Equations
- Lean.instFromJsonUInt64 = { fromJson? := UInt64.fromJson? }
Equations
- Lean.instToJsonUInt64 = { toJson := fun (v : UInt64) => Lean.bignumToJson v.toNat }
Equations
- x.toJson = match Lean.JsonNumber.fromFloat? x with | Sum.inl e => Lean.Json.str e | Sum.inr n => Lean.Json.num n
Instances For
Equations
- Lean.instToJsonFloat = { toJson := Float.toJson }
Equations
- Float.fromJson? (Lean.Json.str "Infinity") = Except.ok (1.0 / 0.0)
- Float.fromJson? (Lean.Json.str "-Infinity") = Except.ok (-1.0 / 0.0)
- Float.fromJson? (Lean.Json.str "NaN") = Except.ok (0.0 / 0.0)
- Float.fromJson? (Lean.Json.num jn) = Except.ok jn.toFloat
- Float.fromJson? x✝ = Except.error "Expected a number or a string 'Infinity', '-Infinity', 'NaN'."
Instances For
Equations
- Lean.instFromJsonFloat = { fromJson? := Float.fromJson? }
Equations
- Lean.Json.Structured.fromJson? (Lean.Json.arr a) = pure (Lean.Json.Structured.arr a)
- Lean.Json.Structured.fromJson? (Lean.Json.obj o) = pure (Lean.Json.Structured.obj o)
- Lean.Json.Structured.fromJson? x✝ = throw (toString "expected structured object, got '" ++ toString x✝ ++ toString "'")
Instances For
Equations
- Lean.Json.instFromJsonStructured = { fromJson? := Lean.Json.Structured.fromJson? }
Equations
Instances For
Equations
Equations
Instances For
Equations
- j.getObjValAs? α k = Lean.fromJson? (j.getObjValD k)
Instances For
Equations
- j.setObjValAs! k v = j.setObjVal! k (Lean.toJson v)
Instances For
Parses a JSON-encoded structure or inductive constructor, assuming the tag has already been
checked and nFields is nonzero. Used mostly by deriving FromJson.
Equations
- One or more equations did not get rendered due to their size.